Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Adding attributes to short code in wordpress

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 107
    Comment on it

    Welcome to Findnerd. We all knows the power of shortcodes. It is very simple way to create a new functonality. We are going to discuss the way how we can add attributes in shortcode. We can give any name to shortcode and then manage attributes using function shortcode_atts.Suppose you have a shortcode named sports and you can add attributes like below.

    [sports name="cricket" players="22"]
    

    In above example we addded two attributes name and players. We can manage the attributes with function shortcode_atts.

    add_shortcode('sports','callsports');
    function call($atts){
        $a = shortcode_atts( array(
        'name' => 'sport Title',
        'players' => '11',
        'fprmat' =>'twenty'
    ), $atts );
    return "foo = {$a['foo']}";
    } 
    

    In above example we have used the add_shortcode function which takes two parameters one is name of the shortcode and other is callback function. Callback function can take three parameters, first one is attributes array , second one is $content and last one is tag. Here we are using only one argument. we have set the default values of the attributes and can add extra attributes as well. If we don't set the arguments values then they will take their default values.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: