Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

Here is the scenario:

My implemented sortcode is like this:

[table]
    name1 = value1;
    name2 = value2;
    name3 = value3
[/table]

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){
        if(!empty($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
    
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
add comment

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.

1 Answer

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
add comment

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