Shortcode Attributes not being passed to handler function - php

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

Related

woocommerce edit-account shortcode

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/

Getting shortcode with multiple attributes

I want to create shortcode with multiple attributes.
But I am only able to do with single not multiple attributes.
I am able only this :
add_shortcode('test','get_shortcode');
But I want to create shortcode like this :
add_shortcode('test id=1 title=test','get_shortcode');
and I would like to call this shortcode in my page like this.
do_shortcode('[test id="1" title="test"]');
Hello add in your functions $atts like that
function bartag_func( $atts ) {
$atts = shortcode_atts(
array(
'foo' => 'no foo',
'bar' => 'default bar',
), $atts, 'bartag' );
return 'bartag: ' . $atts['foo'] . ' ' . $atts['bar'];
}
add_shortcode( 'bartag', 'bartag_func' );
It's display result like [bartag foo="koala" bar="bears"]

WordPress Shortcode - Very puzzling issue?

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' );

WordPress: Use shortcode_atts in other function

Is it possible to use variables from shortcode_atts in another function? Here is my idea:
Posting
[gallery ids="1,2,3,...n"]
Function get_gallery_ids()
//get the gallery-ID's from post
function get_gallery_ids($atts) {
extract(shortcode_atts(array(
'ids' => ''
), $atts));
return $ids;
}
Function explode_ids()
//example function with ids
function explode_ids($ids) {
$ids = explode(',' $ids);
}
How do I implement it? The return just echos.
Update
The code above is a part of my own new gallery_shortcode.
remove_shortcode('gallery', 'gallery_shortcode');
add_shortcode('gallery', 'get_gallery_ids');
I have an Idea:
I use examples from the WordPress Codex
http://codex.wordpress.org/Shortcode_API
function bartag_func( $atts ) {
global $post; // this is new
extract( shortcode_atts( array(
'foo' => 'something',
'bar' => 'something else',
), $atts ) );
update_post_meta($post->ID, "gallery_used_atts", $atts); // this is new
return "foo = {$foo}";
}
add_shortcode( 'bartag', 'bartag_func' );
now you are able to get the $atts you are using on a specific post or page by
$att_values = get_post_meta( $post_id, "gallery_used_atts", true );
and then you can use them for whatever you want.

If attribute's value is one thing, echo another in shortcode

Here's a shortcode of mine:
function sc_link( $atts ) {
extract( shortcode_atts(
array(
'page' => '',
'style' => 'button',
'window' => 'self',
'label' => 'Missing Label Tag: label=""',
), $atts )
);
return '' . $label . '';
}
add_shortcode( 'mylink', 'sc_link' );
What I want to be able to do is a conditional before the return: if $window = 'new' then echo 'blank'.
Think this is what your trying to do what id suggest doing is leaving window blank and just doing an if statement on that
How you want it
function sc_link( $atts ) {
extract( shortcode_atts(
array(
'page' => '',
'style' => 'button',
'window' => 'self',
'label' => 'Missing Label Tag: label=""',
), $atts )
);
if($window == "new"){
$link = '' . $label . '';
}else{
$link = '' . $label . '';
}
return $link;
}
add_shortcode( 'mylink', 'sc_link' );
How it should be
function sc_link( $atts ) {
extract( shortcode_atts(
array(
'page' => '',
'style' => 'button',
'window' => '',
'label' => 'Missing Label Tag: label=""',
), $atts )
);
if(!$window){
$link = '' . $label . '';
}else{
$link = '' . $label . '';
}
return $link;
}
add_shortcode( 'mylink', 'sc_link' );
The your shortcode will be link
[shorcode page="" window="" label=""]
Here is an easy short code conditional output example. I would recommend being sparing with this type of syntax because it can get lost in the fray.
echo ($window === 'new') ? $window : '';
This format is conditional ? what to do if true : what to do if false

Categories