I am trying to create a 'Pie Chart' shortcode to use in WP. All works fine apart from the percentage number. If it is entered into the array it works fine, but if I remove that number (ie; 100 - as seen in the code below), any number that is entered on the front-end by the user returns empty?? Quite puzzling?
function piechart_inner_shortcode( $atts ) {
extract( shortcode_atts( array(
'data_percentage' => '100',
'title' => 'Title',
), $atts ) );
$output = '<div class="chart"><div class="percentage" data-percent="'. $data_percentage .'"><span>'.$data_percentage.'%</span></div><div class="label"><strong>'.$title.'</strong></div></div>';
return $output;
}
add_shortcode( 'piechart_inner', 'piechart_inner_shortcode' );
And this is the shortcode that needs to be entered on the front-end -
[piechart_inner data-percent="45" title="WordPress"][/piechart_inner]
Which outputs nothing for the data-percent, whatever value is entered?
Many thanks
You are using the wrong variable. You are giving data-percent when you have variable data_percentage
Your shortcode should look like this:
[piechart_inner data_percentage="45" title="WordPress"][/piechart_inner]
Or change the function to following:
function piechart_inner_shortcode( $atts ) {
extract( shortcode_atts( array(
'data-percent' => '100',
'title' => 'Title',
), $atts ) );
$output = '<div class="chart"><div class="percentage" data-percent="'. $data-percent .'"><span>'.$data-percent.'%</span></div><div class="label"><strong>'.$title.'</strong></div></div>';
return $output;
}
add_shortcode( 'piechart_inner', 'piechart_inner_shortcode' );
Related
I have created a custom checkbox field _is_group and i would like to be able to use this in the [products...] shortcode.
The currently used workaround is that I create the query with wc_get_products() first and then pass the returned IDs to the standard shortcode, with implode() function:
$group = wc_get_products([
'post_type' => 'product',
'meta_key' => '_is_group',
'meta_value' => 'yes',
'meta_compare' => 'IN',
'return' => 'ids'
]);
echo do_shortcode('[products ids="' . implode(',', $group) . '"]');
Sure it works, but it looks ugly, making unnecessarily two db queries instead of one. I think there is a better way to deal with it, something like that:
[products columns="3" is_group="yes"]
So I decided to ask how to append a custom meta field as a shortcode argument value in Woocommerce . Maybe the solution is to use woocommerce_shortcode_products_query or something else?
Note: I've seen other similar questions and solutions like using custom shortcode inside another shortcode, but that doesn't solve the problem. I care about optimization
You need two parts:
Support your own attribute for the [products] shortcode.
https://developer.wordpress.org/reference/hooks/shortcode_atts_shortcode/
// Add your own attribute
function filter_shortcode_atts_products( $out, $pairs, $atts, $shortcode ) {
// Isset and equal to yes
if ( isset ( $atts['is_group'] ) && $atts['is_group'] == 'yes' ) {
$out['is_group'] = true;
} else {
$out['is_group'] = false;
}
return $out;
}
add_filter( 'shortcode_atts_products', 'filter_shortcode_atts_products', 10, 4 );
Modify the query to use your $query_args
// Modify the query args
function filter_woocommerce_shortcode_products_query( $query_args, $atts, $type ) {
// Target
if ( $type == 'products' && $atts['is_group'] ) {
// Meta query
$query_args['meta_query'] = array(
array(
'key' => '_is_group',
'value' => 'yes',
'compare' => 'IN',
)
);
}
return $query_args;
}
add_filter( 'woocommerce_shortcode_products_query', 'filter_woocommerce_shortcode_products_query', 10, 3 );
SHORTCODE USAGE
In an existing page:
[products columns="3" is_group="yes"]
Or in PHP:
echo do_shortcode('[products columns="3" is_group="yes"]');
I saw in this question that is possible create a shortcode from my-orders page, I am trying create something similar to display the edit account page via shortcodes.
Reference: in woocommerce, is there a shortcode/page to view all orders?
function shortcode_my_orders( $atts ) {
extract( shortcode_atts( array(
'order_count' => -1
), $atts ) );
ob_start();
wc_get_template( 'myaccount/my-orders.php', array(
'current_user' => get_user_by( 'id', get_current_user_id() ),
'order_count' => $order_count
) );
return ob_get_clean();
}
add_shortcode('my_orders', 'shortcode_my_orders');
I created this shortcode to add the HTML contents of the Edit Account page in another page. I believe that's what you are asking for.
// Paste this in the function.php file of your active child theme or theme.
function wc_customer_edit_account_html_shortcode( $atts ) {
// Attributes
extract( shortcode_atts( array(
'text' => 'Edit Account' ), $atts ) );
return wc_get_template_html( 'myaccount/form-edit-account.php', array( 'user' => get_user_by( 'id', get_current_user_id() ) ) );;
}
add_shortcode( 'wc_customer_edit_account_html', 'wc_customer_edit_account_html_shortcode' );
You can also put this in a New Snippet in the Snippets plugin instead of editing the functions.php page.
You can display the edit account form, wherever you want, with this code:
function clket_edit_account_form(){
ob_start();
wc_get_template( 'myaccount/form-edit-account.php', array( 'user' => get_user_by( 'id', get_current_user_id() ) ) );
return ob_get_clean();
}
add_shortcode('clket_edit_account', 'clket_edit_account_form');
Shortcode: [clket_edit_account]
Ref: https://woocommerce.wp-a2z.org/oik_api/wc_shortcode_my_accountedit_account/
Having a bit of bother with the Wordpress Meta Box plugin, specifically with retrieving an image url from an image added to a custom post type.
I'm creating meta boxes in a custom plugin, like so:
add_filter( 'rwmb_meta_boxes', 'xxx_meta_register_meta_boxes' );
function xxx_meta_register_meta_boxes( $meta_boxes )
{
$prefix = 'xxx_meta_';
$meta_boxes[] = array(
'title' => esc_html__( 'Retailer Information', '' ),
'id' => 'advanced',
'post_types' => array( 'xxx_retailers' ),
'autosave' => true,
'fields' => array(
// PLUPLOAD IMAGE UPLOAD (WP 3.3+)
array(
'name' => esc_html__( 'Retailer Logo', '' ),
'id' => "{$prefix}plupload",
'type' => 'plupload_image',
'max_file_uploads' => 1,
),
// URL
array(
'name' => esc_html__( 'Link', '' ),
'id' => "{$prefix}url",
'desc' => esc_html__( 'Clicking the retailer logo will take the user to this URL', '' ),
'type' => 'url',
'std' => 'xxx',
),
)
);
return $meta_boxes;
}
So far so good, these boxes are relevant to a custom post type 'xxx_retailers'.
The problem comes with retrieving this data. I want to display my retailers in a widget. I've chopped and changed another piece of code I've used previously, but it's not returning the image URL, just the ID. Unfortunately I don't know enough php to figure out why.
// Create Retailers Widget
// Create the widget
class Retailers_Widget extends WP_Widget {
function __construct() {
parent::__construct(
// base ID of the widget
'retailers_widget',
// name of the widget
__('XXX Retailers List', '' ),
// widget options
array (
'description' => __( 'Shows a list of retailer logos', '' )
)
);
}
function widget( $args, $instance ) {
// kick things off
extract( $args );
echo $before_widget;
echo $before_title . 'Retailers' . $after_title;
// Pull through Retailers
$xxxretailers = get_posts(array(
'post_type' => 'xxx_retailers',
'orderby' => 'title',
'order' => 'asc',
));
// Display for each Retailer
foreach ($xxxretailers as $xxxretailer) {
$custom = get_post_custom($xxxretailer->ID);
$meta_ret_img = $custom["xxx_meta_plupload"][0];
$meta_ret_url = $custom["xxx_meta_url"][0];
// Display Retailers
echo "<li><a href='{$meta_ret_url}'><img src='{$meta_ret_img}' /></a></li>";
}
}
};
// Register widget
function register_retailers_widget() {
register_widget( 'Retailers_Widget' );
}
add_action( 'widgets_init', 'register_retailers_widget' );
The URLs are coming through correctly, so I know this is a problem with the line
$meta_ret_img = $custom["xxx_meta_plupload"][0];
But I can't figure out how to get the image URL from the data I presume is stored as an array. Any ideas?
Edit:
I should have mentioned, in a single post I can get a single image with:
$images = rwmb_meta( 'xxx_meta_plupload', 'size=medium' );
if ( !empty( $images ) ) {
foreach ( $images as $image ) {
echo "<img src='{$image['url']}' />";
}
}
But I want to show images from all my retailers post types, to create a list of logos.
Replace this statement:
$meta_ret_img = $custom["xxx_meta_plupload"][0];
with this:
$meta_ret_img_array = wp_get_attachment_image_src($custom["xxx_meta_plupload"][0]);
$meta_ret_img = $meta_ret_img_array[0];
also please remove all these curly braces from src and href attributes from your code.
if you are interested in any particular size of the image, then see the official document of wp_get_attachment_image_src() function here.
For e.g. for medium size image you can write it as:
wp_get_attachment_image_src($custom["xxx_meta_plupload"][0], 'medium');
I register a shortcode on my functions.php
function offer_intro_shortcode ($atts, $content=null) {
$offer = shortcode_atts( array (
'title' => '',
'text' => '',
), $atts );
extract ($offer);
return '<div class="center gap">
<h3>'.$title.'</h3>
<p class="lead">'.$text.'</p>
</div>';
}
add_shortcode ('offer', 'offer_intro_shortcode');
Then i write on wordpress post:
[offer title='What We Offer' text='Look at some of the recent projects we have completed for our valuble clients']
Then i query for this shortcode on my index.php like do_shortcode('[offer]')
But it is not working
try this.
echo do_shortcode("[offer title='What We Offer' text='Look at some of the recent projects we have completed for our valuble clients']");
check this is working or not.
After $atts add another parameter i.e shortcode so it would become.
$offer = shortcode_atts( array (
'title' => '',
'text' => '',
), $atts,'offer' );
Not sure what but the attributes from the shortcode aren't being passed to the handler function.
Shortcode in Wordpress Post
[k_recipe recipe="snack"]Here is some content[/k_recipe]
Shortcode Function
add_shortcode("k_recipe", "recipe_shortcode");
function recipe_shortcode($attributes, $content){
return "Hi " . $attributes["recipe"] . " Hi " . $content;
}
Shortcode Output
Hi Hi Here is some content
Why isnt the snack value being passed?? Any clue??
here is how to use shortcode with attributes
function bartag_func( $atts ) {
extract( shortcode_atts( array(
'foo' => 'something',
'bar' => 'something else',
), $atts ) );
return "foo = {$foo}";
}
add_shortcode( 'myshortcode', 'bartag_func' );
[myshortcode foo="bar" bar="bing"]
you are missing extract thing
Maybe you need to use the extract function like documentation says:
// [bartag foo="foo-value"]
function bartag_func( $atts ) {
extract( shortcode_atts( array(
'foo' => 'something',
'bar' => 'something else',
), $atts ) );
return "foo = {$foo}";
}
add_shortcode( 'bartag', 'bartag_func' );
http://codex.wordpress.org/Shortcode_API