WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Explode not working as i expect inside add_shortcode() callback funciton.

Here is the scenario:

My implemented sortcode is like this:

    name1 = value1;
    name2 = value2;
    name3 = value3

Which I make table using simple explode. Exploding ; to get each row then exploding = to put them in right column. But explode is not working right. It exploding the whitespace after ", - and so on. I have tested the function outside wordpress to check if it have problem working with string but it works fine.

Here is page where you see the table http://computercarebd.com/?product=dell-inspiron-4030 When you look at the table you will see the Display row is got broken (made another array element by explode();) after 14.0″

And here is the shortcode for it http://pastebin.com/ZYuuNb11

When you check the homepage you will see the features i have added on the product preview where i used the same method but its working fine. I have used get_the_content() to get the content then used the same explode method.

So, i believe I have to modify the contents on the the table_shortcode() callback function to make it work with explode.

Thanks and let me know if i was clear enough.

function table_shortcode($atts, $content = NULL){
    $content = strip_tags($content);
    $contents = explode(';', $content);
    extract(shortcode_atts(array('width' => '370px', 'align' => 'alignright'), $atts));

    $table = '<div class="table '.$align.'" style="width:'.$width.';">';
    foreach($contents as $content){
            $table .= '<div class="table_row">';
                $row_items = explode('=', $content);
                $table .= '<div class="table_left">'.(trim($row_items[0])).'</div>';
                $table .= '<div class="table_right">'.(trim($row_items[1])).'</div>';
                $table .= '<div class="clear"></div>';
            $table .= '</div>';
    $table .= '</div>';
    return $table;
share|improve this question

closed as off topic by Rarst Nov 30 '11 at 12:38

Questions on WordPress Development Stack Exchange are expected to relate to WordPress within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

This seems to work fine for me. Please add sample output and what is wrong with it to the question, I don't really get what explode is doing wrong (seemingly nothing). – Rarst Nov 30 '11 at 12:06
@Rarst Check the table link i gave – Sisir Nov 30 '11 at 12:08
Does any filter gets applied on the content of shortcode before it goes to the callback funciton? And also firstly i suspect that special characters might broken the table &nbsp;, &amp; as those has ; but this is not the case as i checked the function with other character to explode (#); – Sisir Nov 30 '11 at 12:19
This explodes on ; as you specified, the issue is that HTML-encoded entities have ; in them. Nothing WP-specific here. – Rarst Nov 30 '11 at 12:19
up vote 0 down vote accepted

Had to go with more complex characters for explode used {@}. As rarst said an using ; for exploding not a good idea because html encoded entities have ; on them.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.