• Hello folks,

    I’d like to know how I can do something like that:

    add_shortcode(('shortcode' or 'sc')), 'shortcode_func');

    like, giving the shortcode another option, without doing it like that:

    add_shortcode('shortcode', 'shortcode_func');
    add_shortcode('sc', 'shortcode_func');

    possible?

Viewing 5 replies - 1 through 5 (of 5 total)
  • I assume you want to pass multiple attributes to the same shortcode. With that in mind here is a sample shortcode that takes two attributes ( name and greeting ). It also sets the default value for each att. The shortcode simply prints out a greeting using the greeting and name wrapped in a H3 tag. The Codex has a good article on the shortcode API

    add_shortcode( 'my-shortcode', 'my_shortcode_function' );
    
    function my_shortcode_function( $atts, $content = null )
    {
    	extract( shortcode_atts( array( 'name' => 'No Name', 'greeting' => 'Hello' ), $atts ) );
    	$print_greeting = "<h3>".$greeting.' '.$name."</h3>";
    	return $print_greeting;
    }

    You would add the shortcode like this:
    [my-shortcode name="Michelle" greeting="Bonjour"]

    Thread Starter dLICIOUS

    (@dlicious)

    That’s not exactly what I was looking for ??

    I want to use my shortcode like this:

    [shortcode]Content goes here...[/shortcode]

    but also I’d like to use

    [sc]Content goes here...[/sc]

    which runs the same function as “shortcode”.
    “sc” is an alias for “shortcode” in this example.

    Sorry about that. I misunderstood the question. In that case, I do not know how you would do what you wish. Out of curiosity why do you want two shortcodes that do the same thing?

    Thread Starter dLICIOUS

    (@dlicious)

    I just want some aliases for my shortcodes, because they are faster to enter.

    Thanks anyway for your time and help!

    Is there a reason you wan’t to make two shortcodes that use the same function without multiple calls to add_shortcode()?

    add_shortcode('shortcode', 'shortcode_func');
    add_shortcode('sc', 'shortcode_func');

    I don’t think add_shortcode() is built to accept an array in the $tag parameter, so it seems like this is the only way.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘add_shortcode question’ is closed to new replies.