Search code examples
phpwordpressshortcode

How do i call shortcode value from dashboard editor?


I have used shortcode like this:

<?php
    add_shortcode( 'myproduct', 'myproduct_func' );

    function myproduct_func( $atts ) {

        extract( shortcode_atts( array(
            'cols' => 'none',
            'data' => 'none',
        ), $atts ) );

        $cols = explode(',',$cols);
        $data = explode(',',$data);
        $total = count($cols);

        $output = "";
        foreach($cols as $col):
            $output .= "| {$col} ";
        endforeach;

        $output .= "<br>";

        $counter = 1;
        foreach($data as $datum):
            $output .= "| {$datum} ";
            if($counter%$total==0):
                $output .= "<br>";
            endif;
            $counter++;
        endforeach;

        return $output;

    }

    ?>

Do display the shortcode when I write the code :

echo do_shortcode('[myproduct cols="name,quantity,price" data="name1,5,2.00,name2,3,3.25"]');

In index.php it display table on my home page but:

Same code [myproduct cols="name,quantity,price" data="name1,5,2.00,name2,3,3.25"] when I write in my home page dashboard editor, it shows nothing on my home page. I want the value of cols and data is dynamic so it should be passed through dashboard.


Solution

  • Are you doing this in the visual or the text editor? For Shortcodes to work must be on the text editor in the tinymce on the right tabs.

    enter image description here

    Also you must include your myproduct_func in functions.php so it's available.