1

I am working on building some shortcodes for my blog. I can set a single parameter for my shortcode, but not sure how to set different parameter.

For example, I can use [myshortcode myvalue] to output a html block within the post content.

Here is what I am currently using:

function test_shortcodes( $atts ) {
    extract( shortcode_atts( array(
        'myvalue' => '<div class="shortcodecontent"></div>'

    ), $atts ) );

    return $myvalue;
}
add_shortcode( 'myshortcode', 'test_shortcodes' );

Now, how can I use [myshortcode myothervalue] to output a different block of html?

Please note that the shortcode is same, only the parameter is changed.

  • code examples of your shortcode might be helpful. At least the important parts. – Steve Feb 4 '13 at 14:28
  • You mean myvalue="something", right? – s_ha_dum Feb 4 '13 at 14:35
  • @Stephen Added my piece of code. s_ha_dum no, that's not what I mean. – Abhik Feb 4 '13 at 16:22
  • Can you give an example of how you're writing your shortcode in the page/post? The code doesn't make a lot of sense. – Steve Feb 4 '13 at 16:35
  • 1
    You need to look at the codex to understand shortcodes. See this documentation: codex.wordpress.org/Shortcode_API – Steve Feb 4 '13 at 16:42
6

Lets look at the shortcode

[SH_TEST var1="somevalue" var2="someothervalue"]THE SHORTCODE CONTENT[/SH_TEST]

the shortcode handler function accepts 3 paramters

  1. $atts - an array of attributes passed by the shortcode in our case:

    • $atts['var1'] is set to 'somevalue'
    • $atts['var2'] is set to 'someothervalue'
  2. $content - is a string of the value enclosed with in the shortcode tags, in our case: -$content is set to THE SHORTCODE CONTENT

  3. $tag - is a string of the shortcode tag, in our case: -$tag is set to SH_TEST

When I create a shortcode i usually define the default values and merge them with the values submitted by the shortcode tag ex:

add_shortcode('SH_TEST','SH_TEST_handler');
function SH_TEST_handler($atts = array(), $content = null, $tag){
    shortcode_atts(array(
        'var1' => 'default var1',
        'var2' => false
    ), $atts);

    if ($atts['var2'])
          return 'myothervalue';
    else
          return 'myvalue'; 
}
| improve this answer | |
1

If you use the shortcode like that atts[0] will contain the value:

add_shortcode( 'test', 'test_callback' );

function test_callback( $atts )
{
    return $atts[0];
}

Another way is calling the value with a name:

[myshortcode val="myvalue"]

function test_callback( $atts )
{
    return $atts["val"];
}
| improve this answer | |
  • Can you please elaborate the first process please? – Abhik Feb 4 '13 at 16:25
0

You're better off doing it like this:

function test_shortcodes( $atts ) {
    extract( shortcode_atts( array(
        'type' => 'myvalue'

    ), $atts ) );

    switch( $type ){
        case 'myvalue': 
            $output = '<div class="shortcodecontent"></div>';
            break;

        case 'myothervalue': 
            $output = '<div class="othershortcodecontent"></div>';
            break;

        default:
            $output = '<div class="defaultshortcodecontent"></div>';
            break;
    }

    return $output;
}
add_shortcode( 'myshortcode', 'test_shortcodes' );

Use it like this:

[myshortcode type="myvalue"] to output <div class="shortcodecontent"></div>

and

[myshortcode type="myothervalue"] to output <div class="othershortcodecontent"></div>

| improve this answer | |
0

This way works for me in all cases using [myshortcode type="myvalue"]

function test_shortcodes( $atts = array() ) {
extract( shortcode_atts( array(
    'type' => 'myvalue'

), $atts ) );

switch( $atts['type] ){
    case 'myvalue': 
        $output = '<div class="shortcodecontent"></div>';
        break;

    case 'myothervalue': 
        $output = '<div class="othershortcodecontent"></div>';
        break;

    default:
        $output = '<div class="defaultshortcodecontent"></div>';
        break;
}

return $output;
}
add_shortcode( 'myshortcode', 'test_shortcodes' );

And if you want to add another parameter all you need to do is this [myshortcode type="myvalue" other="somevalue"]

test_shortcodes( $atts = array() ) {
extract( shortcode_atts( array(
    'type' => 'myvalue', //Could be default
    'other' => 'somevalue' //Could be default

), $atts ) );

switch( $atts['other'] ){
    case 'somevalue': 
        $output = '<div class="shortcodecontent">somevalue</div>';
        break;

    case 'myothervalue': 
        $output = '<div class="othershortcodecontent"></div>';
        break;

    default:
        $output = '<div class="defaultshortcodecontent"></div>';
        break;
}

return $output;
}
add_shortcode( 'myshortcode', 'test_shortcodes' );

I hope this helps

| improve this answer | |
-2

First, you will have to define $atts item in your function because $atts is an array.

Here is the complete code to pass the variable in shortcode -

Let's suppose you need to show all the product of a category through shortcode, You need to do following code in your function file -

 function creative_writing_func($atts) {
 $args = array(
'post_type'      => 'product',
'posts_per_page' => 10,
'product_cat'    => $atts['categoryname']
);

$loop = new WP_Query( $args );

while ( $loop->have_posts() ) : $loop->the_post();
global $product;
echo '<br /><a href="'.get_permalink().'">' . 
woocommerce_get_product_thumbnail().' '.get_the_title().'</a>';
endwhile;

wp_reset_query();
}

add_shortcode('creative_writing_func_short', 'creative_writing_func');

Now you can simply paste the shortcode code in your template file or Wordpress default editor -

  [creative_writing_func_short categoryname="creative-writing-english-literature"]

Where we're passing the Category name (creative-writing-english-literature) in the shortcode.

Have tested it and it's working.

| improve this answer | |
  • 1
    You cannot use echo in a shortcode, the callback must return a string. – fuxia Jun 22 '18 at 13:01
  • Please don't post untested code. Particularly if you say it is tested and working! – Andy Macaulay-Brook Jun 22 '18 at 15:42
  • @fuxia You can use echo as well as HTML tags, It depends on your requirement, I've used this shortcode to use in the visual composer to shows the product name with thumbnail and link. And It's working perfectly. – Amritosh pandey Jun 23 '18 at 19:17
  • 1
    See wordpress.stackexchange.com/a/73083/73 for an explanation. If you use echo, the content might get printed out where you don't expect it. – fuxia Jun 23 '18 at 19:20
  • 1
    Any website that parses the_content, perhaps in an SEO plugin, before the template is processed will start throwing “headers already sent” errors, just as one example of what might go wrong if a shortcode uses echo. – Andy Macaulay-Brook Jun 23 '18 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.