Shortcode with custom content attribute?

The second parameter of your shortcode function is the content. So rewrite the function:

function shortcode_call_to_action( $attributes = NULL, $content="" ) 
{ 
    $stylesheetdir = get_bloginfo('stylesheet_directory');

    if ( '' === $content )
    {   // use a default text if there is no content
        $content="default text";
    }

    return "<div id='call_to_action'> <img src="https://wordpress.stackexchange.com/questions/58438/$stylesheetdir/images/call_to_action.jpg" alt="" /> 
        <p>$content</p>";
}

If the user writes …

[call_to_action]Custom text[/call_to_action]

… the parameter $content in your function will be set to that text.

For more details see the Shortcode API.

Leave a Comment