• Resolved rbarber218

    (@rbarber218)


    Hello all,

    I have been trying to create a shortcode, but it seems that the shortcode is always using the default values. It does not render the values that were passed up.

    Ex.

    functions.php has the following code:

    function portfolio_shortcode( $atts ) {
    $atts = shortcode_atts(
    array(
    ‘actionText’ => ‘Portfolio’,
    ),
    $atts, ‘portfolioAction’
    );

    $actionText = esc_html($atts[‘actionText’]);

    return $actionText ;
    }
    add_shortcode( ‘portfolioAction’, ‘portfolio_shortcode’ );

    Now when I try to enter the following code in a post, it will always render “Portfolio” (the default value), instead of the past in value:

    [portfolioAction actionText=”Some Text”]

    Any help as to why this does not render “Some Text”? Thank You

Viewing 3 replies - 1 through 3 (of 3 total)
  • Try this and see what it comes back with…

    echo "<pre>";
    var_export ($atts);
    echo "</pre>";
    
    $atts = shortcode_atts(
        array(
            'actionText' => 'Portfolio',
        ),
        $atts, 'portfolioAction'
    );
    
    echo "<pre>";
    var_export ($atts);
    echo "</pre>";

    That will show you if you’re getting the right values from the functions arguments or not, and if they are being populated correctly in the resulting array.

    From what you’ve got there, there’s nothing obviously wrong, so it’s hard to say if there’s a problem with the code.

    Hi @rbarber218

    Seems you shouldn’t use the attribute as uppercase in shortcode because as they are suggesting shortcode and shortcode attribute names are lowercase even if you make it uppercase also, from the code they change it to lowercase, in this case you trying to return the value as actionText from extracting shortcode it will shown name as actiontext not actionText.

    From the Codex of WordPress.org https://codex.wordpress.org/Shortcode_API
    Attribute names are always converted to lowercase before they are passed into the handler function. Values are untouched.[myshortcode FOO="BAR"] produces $atts = array( 'foo' => 'BAR' ).

    Hopefully It make sense.

    Thanks.

    Thread Starter rbarber218

    (@rbarber218)

    Hello @viky081,

    You are correct. Using all lowercase did the trick.

    Thank you for your help. Thank you as well @catacaustic.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Shortcode not reading passed up values.’ is closed to new replies.