Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Shortcode Output Issue WordPress

  • SOLVED

Hi,
I'm using a modal window to add shortcodes into my posts. This part works fine, and adds the shortcode to the post like it's supposed to:

[message class=green-message]This is a message.[/message]

However, I'm having trouble getting this to output on the page properly. I have two other shortcodes built using the same method and they seem to work, however this one just won't output correctly.

This is the code I'm using to collect the shortcode info in a popup window: http://cl.ly/9RZE.

This is the code I'm using to output the shortcode on the page when published: http://cl.ly/9Q8N

I believe the issue is with this piece below (which is in the second link), but all the edits I've tried aren't working. It outputs the message but fails to output the class. Instead it display class=" ".


function message( $atts, $content = null )
{
extract( shortcode_atts( array(
'messcontent' => '',
'messtype' => '',
), $atts ) );

// convert the shortcode to this on the front end
return '<div class=" ' . $messtype . ' ">' . $content . '</div>';

}
add_shortcode('message', 'message');


Any help is appreciated!

Answers (3)

2011-08-19

Utkarsh Kukreti answers:

function message( $atts, $content = null )
{
extract( shortcode_atts( array(
'class' => '',
), $atts ) );

// convert the shortcode to this on the front end
return '<div class=" ' . $class . ' ">' . $content . '</div>';
}

add_shortcode('message', 'message');


Mike McAlister comments:

Perfect, Utkarsh! Thanks!

Mike

2011-08-19

Nilesh shiragave answers:


function message( $atts, $content = null )

{

extract( shortcode_atts( array(

'class' => '',

'messtype' => '',

), $atts ) );



// convert the shortcode to this on the front end

return '<div class=" ' . $class . ' ">' . $content . '</div>';



}

add_shortcode('message', 'message');

2011-08-19

Julio Potier answers:

Hello

You have to know how work a shortcode :

a) When you call "[message class=green-message]This is a message.[/message]"
b) "<em>message</em>" is the shortcode name, added by "add_shortcode( 'message' ...).
c) "<em>This is a message</em>" is the "$content" var (default null).
d) Also, "<em>class=green-message</em>" or "<em>aaa=bbb</em>" are attributes, it goes in the "atts" function in an array.
e) The ligne "<em>extract( ... )</em>" will perform some action to convert the attributes in easy php var.
so "<em>class=green-message</em>" will become "<em>$class = 'green-message'</em>", you can now use "$class" in your code.
f) Then in a shortcut you have to "<em>return</em>" a value, never echoes it.

In your actual code you wrote "<em>[message class=green-message]This is a message.[/message]</em>" Your shortcode does not use "<em>$class</em>" but "<em>$messtype</em>" !

<strong>2 solutions : </strong>
1. use this shortcode in place of the other.

function message( $atts, $content = null )
{
extract( shortcode_atts( array(
'class' => '',
), $atts ) );

// convert the shortcode to this on the front end
return '<div class=" ' . $class . ' ">' . $content . '</div>';
}
add_shortcode('message', 'message');


2. or call [message messtype=green-message]This is a message.[/message] in place of your shortcode.

See you !