Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
code examples of your shortcode might be helpful. At least the important parts. –  Stephen 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. –  Stephen 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 –  Stephen Feb 4 '13 at 16:42

3 Answers 3

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'; 
}
share|improve this answer

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

add_shortcode( 'test', 'test_callback' );

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

Another way is calling the value with a name:

[myshortcode val="myvalue"]

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

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 $myvalue;
}
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>

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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