How to Fetch the Shortcode Parameters Value in WordPress? - wordpress

[amazon_s3 bucket=my_bucket_name object=my_file_name.ext]
Hi all i need to know how to fetch the parameter value in the above shortcode. For Example: Object is the attributes then how can i fetch the object value my_file_name.ext. I am using the woocommerce s3 plugin. I am not sure i customized the woocommerce to fetch the file name show in my account page here is the code.
function filename_wc_downloads( $link, $download )
{
$order = new WC_Order( $download['order_id'] );
$download_file_urls = $order->get_downloadable_file_urls(
$download['product_id'],
null,
$download['download_id']
);
foreach( $download_file_urls as $key => $value )
{
if( $value == $download['download_url'] )
{
$url_parts = explode( '/', parse_url( $key, PHP_URL_PATH ) );
$file_name = end( $url_parts );
$link = '<a href="'
. esc_url( $download['download_url'] )
. '">'
. $download['download_name']
. '</a> <small>( '
. $file_name
. ' )</small>';
}
}
return $link;
}
In a Woocommerce all products are uploaded into the media library of upload folder. The above code is to fetch the filename show in my account page if they using direct file path. If i pasted the above shortcode in the product url, that above code doesn't help to fetch the filename. so i need to know from the shortcode how can i get the object value based on this to show the file name.

amazon_s3 is your shortcode containing $atts bucket and object
when you use wordpress function add_shortcode('amazon_s3', 'your_function_name');
it automatically converts your attributes defined in [amazan_s3 .... to $atts
e.g.
function your_funnction_name($atts) {
extract(shortcode_atts(array(
'bucket' => '',
'object' => ''
), $atts));
return $object;
}

Related

Replace space with hyphen for image filename on upload

I'm using the code bellow (I found it here on stack overflow from this post) to auto rename image filename and fill alt and title field based on post title.
The good thing is it's working, it's doing its job but problem is with filename : it's using post title with spaces, comas, etc. as filename, which is really not good. It's perfect for filling the title and alt fields but not for a filename.
So, just for the filename, I would like to add something to replace spaces by hyphen (and if possible remove potentials comas or other punctuation)
I thought that the part add_filter( 'sanitize_file_name', 'file_renamer', 10, 1 ); would to the job, but it's not.
But as I'm really not sure what I'm doing, and as I have poor PHP knowledge, I would be very grateful if you could teach me how to make it work :
function file_renamer( $filename ) {
$info = pathinfo( $filename );
$ext = empty( $info['extension'] ) ? '' : '.' . $info['extension'];
$name = basename( $filename, $ext );
if( $post_id = array_key_exists("post_id", $_POST) ? $_POST["post_id"] : null) {
if($post = get_post($post_id)) {
return $post->post_title . $ext;
}
}
$my_image_title = $post;
$file['name'] = $my_image_title . - uniqid() . $ext; // uniqid method
// $file['name'] = md5($name) . $ext; // md5 method
// $file['name'] = base64_encode($name) . $ext; // base64 method
return $filename;
}
add_filter( 'sanitize_file_name', 'file_renamer', 10, 1 );
/* Automatically set the image Title, Alt-Text, Caption & Description upon upload */
add_action( 'add_attachment', 'my_set_image_meta_upon_image_upload' );
function my_set_image_meta_upon_image_upload( $post_ID ) {
// Check if uploaded file is an image, else do nothing
if ( wp_attachment_is_image( $post_ID ) ) {
// Get the parent post ID, if there is one
if( isset($_REQUEST['post_id']) ) {
$post_id = $_REQUEST['post_id'];
} else {
$post_id = false;
}
if ($post_id != false) {
$my_image_title = get_the_title($post_id);
} else {
$my_image_title = get_post( $post_ID )->post_title;
}
// Sanitize the title: remove hyphens, underscores & extra spaces:
$my_image_title = preg_replace( '%\s*[-_\s]+\s*%', ' ', $my_image_title );
// Create an array with the image meta (Title, Caption, Description) to be updated
// Note: comment out the Excerpt/Caption or Content/Description lines if not needed
$my_image_meta = array(
'ID' => $post_ID, // Specify the image (ID) to be updated
'post_title' => $my_image_title, // Set image Title to sanitized title
'post_content' => $my_image_title, // Set image Description (Content) to sanitized title
);
// Set the image Alt-Text
update_post_meta( $post_ID, '_wp_attachment_image_alt', $my_image_title );
// Set the image meta (e.g. Title, Excerpt, Content)
wp_update_post( $my_image_meta );
}
}
Thanks for your help !
So solution was replacing post_title by post_name in file_renamer function.
Exactly this part : change return $post->post_title . $ext; and replace it with return $post->post_name . $ext;
And it's working. On upload, this function is renaming the file name with hyphens and filling title, alt text and description fields based on Post Title (without hyphens).

Wordpress Updating Attachment Data

I'm attempting to add a feature to a plugin that extends media management. This feature would allow you to rename an attachment file. I've been able to complete this with the following code.
public function update_attachment_filename( $post_ID ) {
// Get path to existing file
$file = get_attached_file( $post_ID );
$path = pathinfo( $file );
// Generate new file name
$file_updated = $path['dirname'] . '/' . $_POST['update_filename'] . '.' . $path['extension'];
// Update the name and reference to file
rename( $file, $file_updated );
update_attached_file( $post_ID, $file_updated );
}
While the original file gets renamed using the above method, all additional image sizes defined in the plugins/theme are not updated. I'm struggling to figure out the best way to accomplish this task.
I've looked into wp_update_attachment_metadata() and wp_generate_attachment_metadata() but am unsure whether they will give me the desired functionality.
Additionally I've looked into something such as:
$file_meta = wp_get_attachment_metadata( $post_ID );
foreach( $file_meta['sizes'] as $image ) {
// Do something
}
Any nudge in the right direction would be greatly appreciated.
Thanks!
I was able to utilize both the wp_generate_attachment_metadata() and the wp_update_attachment_metadata() function to achieve the desired end result.
public function update_attachment_filename( $post_ID ) {
if( isset( $_POST['update_filename'] ) && ! empty( $_POST['update_filename'] ) ) {
// Get path to existing attachment
$file = get_attached_file( $post_ID );
$path = pathinfo( $file );
// Create new attachment name
$file_updated = $path['dirname'] . '/' . $_POST['update_filename'] . '.' . $path['extension'];
// Update the attachment name
rename( $file, $file_updated );
update_attached_file( $post_ID, $file_updated );
// Update attachment meta data
$file_updated_meta = wp_generate_attachment_metadata( $post_ID, $file_updated );
wp_update_attachment_metadata( $post_ID, $file_updated_meta );
}
}

WordPress: post_type->rewrite['slug'] returns post_type, not the slug

In my application I need to create a widget in admin dashboard which will display a section of all post_types associated with the number of posts each of them has.
To accomplish the above I added the following code block in my functions.php file:
add_action('wp_dashboard_setup', 'tp_post_counts_reference');
function tp_post_counts_reference() {
global $wp_meta_boxes;
wp_add_dashboard_widget('custom_help_widget', 'Posts in all post types', 'custom_dashboard_help');
}
function custom_dashboard_help() {
$types = get_post_types();
foreach( $types as $type )
{
if($type != 'travelog' && $type != 'package_tour' && $type != 'hotel-info') continue;
$typeobj = get_post_type_object( $type );
echo '' . $typeobj->labels->name . ': ' . wp_count_posts( $type )->publish . '<br />';
}
}
But $typeobj->rewrite['slug'] is actually outputting the post_type rather than its corresponding slug.
For example:
I have the following custom post types
Travelog (name: Travelog, post_type: travelog, slug: travelogs)
Hotel Info (name: Hotel Info, post_type: hotel-info, slug: hotels)
The actual output for
'' . $typeobj->labels->name . ': ' . wp_count_posts( $type )->publish
are
Travelog: 6
and
Hotel: 11
when I expect them to output:
Travelog: 6
and
Hotel: 11
Please tell me what I did wrong :(
NB: My WP version is 4.7.5
Rather than trying to manually build your post type URL, I would recommend that you instead leverage the built-in WordPress function get_post_type_archive_link.
That would look like this:
function custom_dashboard_help() {
$types = get_post_types();
// Alternate method for testing if custom type.
$custom_types = array( 'travelog', 'package_tour', 'hotel-info' );
foreach( $types as $type ) {
// If not a custom post type, don't render the link
if( ! in_array( $type, $custom_types ) ) {
continue;
}
$typeobj = get_post_type_object( $type );
// Use get_post_type_archive_link function to get URL
echo '' . $typeobj->labels->name . ': ' . wp_count_posts( $type )->publish . '<br />';
}
}

Woocommerce - Display single product attribute(s) with shortcodes in Frontend

i've read many Q/A's here during the last few days, but unfortunately none of them solved my issue.
I'm trying to fetch product attributes and display them on the frontend with a shortcode. I have managed to display ALL available attributes and display them in a list, but i need to select only one or two of them in different locations (thats why using shortcodes). For example like [shortcode_attribute name="brand"].
Any help is highly appreciated!
my code so far:
function tutsplus_list_attributes( $product ) {
global $product;
global $post;
$attributes = $product->get_attributes();
if ( ! $attributes ) {
return;
}
foreach ( $attributes as $attribute ) {
// Get the taxonomy.
$terms = wp_get_post_terms( $product->id, $attribute[ 'name' ], 'all' );
$taxonomy = $terms[ 0 ]->taxonomy;
// Get the taxonomy object.
$taxonomy_object = get_taxonomy( $taxonomy );
// Get the attribute label.
$attribute_label = $taxonomy_object->labels->name;
// Display the label followed by a clickable list of terms.
echo get_the_term_list( $post->ID, $attribute[ 'name' ] , '<div><li class="bullet-arrow">' . $attribute_label . ': ' , ', ', '</li></div>' );
}
}
add_action( 'woocommerce_product_meta_end', 'tutsplus_list_attributes' );
add_shortcode('display_attributes', 'tutsplus_list_attributes');
While I am still not 100% clear what you're after, what I'm going to create is a shortcode that creates a list of all terms in all attributes. To make it more flexible I'll add support for a shortcode parameter so you can create a list of specific attribute terms.
One major thing you need to alter from your code, is that shortcodes need to RETURN a string and not ECHO out html. Echoing out shortcodes can result in unexpected weirdness.
/**
* Attributes shortcode callback.
*/
function so_39394127_attributes_shortcode( $atts ) {
global $product;
if( ! is_object( $product ) || ! $product->has_attributes() ){
return;
}
// parse the shortcode attributes
$args = shortcode_atts( array(
'attributes' => array_keys( $product->get_attributes() ), // by default show all attributes
), $atts );
// is pass an attributes param, turn into array
if( is_string( $args['attributes'] ) ){
$args['attributes'] = array_map( 'trim', explode( '|' , $args['attributes'] ) );
}
// start with a null string because shortcodes need to return not echo a value
$html = '';
if( ! empty( $args['attributes'] ) ){
foreach ( $args['attributes'] as $attribute ) {
// get the WC-standard attribute taxonomy name
$taxonomy = strpos( $attribute, 'pa_' ) === false ? wc_attribute_taxonomy_name( $attribute ) : $attribute;
if( taxonomy_is_product_attribute( $taxonomy ) ){
// Get the attribute label.
$attribute_label = wc_attribute_label( $taxonomy );
// Build the html string with the label followed by a clickable list of terms.
// Updated for WC3.0 to use getters instead of directly accessing property.
$html .= get_the_term_list( $product->get_id(), $taxonomy, '<li class="bullet-arrow">' . $attribute_label . ': ' , ', ', '</li>' );
}
}
// if we have anything to display, wrap it in a <ul> for proper markup
// OR: delete these lines if you only wish to return the <li> elements
if( $html ){
$html = '<ul class="product-attributes">' . $html . '</ul>';
}
}
return $html;
}
add_shortcode( 'display_attributes', 'so_39394127_attributes_shortcode' );
Usage: This would be used in 1 of 2 ways.
First, to display specific attributes use:
[display_attributes attributes="color|material"]
Second, to display all attributes use:
[display_attributes]
Edit
Here's an always single display edition:
/**
* Attribute shortcode callback.
*/
function so_39394127_singular_attribute_shortcode( $atts ) {
global $product;
if( ! is_object( $product ) || ! $product->has_attributes() ){
return;
}
// parse the shortcode attributes
$args = shortcode_atts( array(
'attribute' => ''
), $atts );
// start with a null string because shortcodes need to return not echo a value
$html = '';
if( $args['attribute'] ){
// get the WC-standard attribute taxonomy name
$taxonomy = strpos( $args['attribute'], 'pa_' ) === false ? wc_attribute_taxonomy_name( $args['attribute'] ) : $args['attribute'];
if( taxonomy_is_product_attribute( $taxonomy ) ){
// Get the attribute label.
$attribute_label = wc_attribute_label( $taxonomy );
// Build the html string with the label followed by a clickable list of terms.
// Updated for WC3.0 to use getters instead of directly accessing property.
$html .= get_the_term_list( $product->get_id(), $taxonomy, $attribute_label . ': ' , ', ', '' );
}
}
return $html;
}
add_shortcode( 'display_attribute', 'so_39394127_singular_attribute_shortcode' );
This removes all HTML markup, and so you'd need to provide your own... which would happen if you are making a custom list.
<ul>
<li class="arrow">Static List Item</li>
<li class="arrow">[display_attribute attribute="color"]</li>
<ul>
EDIT: This code can be added to your theme's functions.php, but preferably either in a site-specific plugin or via the Code Snippets plugin.
If anyone else just wants to simply output the attribute value as text, replace the $html .= line with this:
$html .= strip_tags(get_the_term_list($product->get_id(), $taxonomy));
#helgatheviking There must be a more elegant way than this! :)
Needed this recently - found https://wordpress.org/plugins/wcpas-product-attributes-shortcode/ does the trick.
With this plugin you can set an "attribute" parameter to match one of the product attributes you have setup.
For example if you have a brand attribute you can use:
[wcpas_product_attributes attribute="brand"]
This will output a <ul> list containing all brands.
There are also a number of parameters you can use alongside the attribute including:
orderby (order the list by an orderby value)
order (order asc, desc, rand, etc)
hide_empty (hide empty terms)
show_counts (show the total number of products in the brand)
archive_links (use an archive based URL instead of a filter based URL)
min_price (When using archive_links = 0 you can have a price filter based URL off a minimum price)
max_price (When using archive_links = 0 you can have a price filter based URL off a maximum price)

How to print field created by 'WooCommerce Custom Order Data' Plugin to email

I installed WooCommerce Custom Order Data PlugIn to create a custom field after checkout is completed. In a custom plugin I use the woocomerce_thankyou-hook to collect some data from the order and save it as a string to $order->custom->officer_text.
I need to print that custom data to the admin-new-order-mail, but I don't know how to do that.
echo $order->custom->officer_text
in admin-new-order-mail.php doesn't work.
I can print_r the data on the thank you-page, so I know, it's there. But no luck in the email-template.
How do I get it to work?
Edit:
I forgot to post the code of my custom plugin.
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
global $woocommerce;
// Action after order is submitted
add_action('woocommerce_thankyou', 'woocommerce_officer_data');
// function to populate a custom field in $order
function woocommerce_officer_data($order_id) {
$order = new WC_Order( $order_id );
// create custom field
WC_CustomOrderData::extend($order);
$order->custom->officer_text = '';
$officer = '';
// get date and format
$date = DateTime::createFromFormat('Y-m-d H:i:s', $order->order_date);
$orderdate = $date->format('d.m.Y');
$ordertime = $date->format('H:i:s');
$officer .= '##Officer-START##<br>Datum:' . $orderdate . '<br>Zeit:' . $ordertime . '<br>';
$officer .= $order_id . '|' . $order_id . '|' . $order->billing_first_name . '|' . $order->billing_last_name . '|';
$officer .= $order->billing_country . '|' . $order->billing_postcode . '|' . $order->billing_city . '|' ;
$officer .= $order->shipping_address_1 . '|' . $order->billing_email . '|' . $order->billing_phone . '|' ;
$order->custom->officer_text = $officer;
$order->custom->save();
echo $order->custom->officer_text;
}
}
The field is printed right after the ul.order_details.bacs_details
But if I print_r($order) on thankyou.php, the custom data is not there, .
print_r($order->custom) in the plugin gives me this:
WC_CustomOrderData Object
(
[order_id:WC_CustomOrderData:private] => 241
[fields:WC_CustomOrderData:private] => Array
(
[officer_text] => ##Officer-START##
Datum:09.10.2013
Zeit:12:00:38
241|241|Peter|Petersen|DE|11111|Xtown|Ystreet 53|foo#example.de||01xx 654 xxx xx|
)
)
I'm happy, I got so far, because I'm not a real coder, but I have no idea, how to control the output of my first little plugin. So, if someone could show me a 'best practise' solution, it would be great.
For anyone searching in the future, this gist will print certain meta keys in any email template that features the woocommerce_email_order_meta hook.
_my_field_1 and _my_field_2 are the meta keys for the Order's custom data that you are trying to display. I am not familiar with the Custom Order Data plugin, but the meta key for the OP may be _officer_text. In my example, I will use the meta key _some_field.
/**
* Add the field to order emails
**/
add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys');
function my_custom_checkout_field_order_meta_keys( $keys ) {
$keys['Some Field'] = '_some_field;
return $keys;
}
For versions 2.3 and newer of WooCommerce:
// WooCommerce 2.3+
function kia_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
$fields['some_field'] = array(
'label' => __( 'Some field' ),
'value' => get_post_meta( $order->id, '_some_field', true );
);
return $fields;
}
add_filter('woocommerce_email_order_meta_fields', 'kia_email_order_meta_keys', 10, 3 )
;

Resources