0

My plugin implements a shortcode respecting wp best practices, but a strange behavior appears. First, the shortcode works perfectly on the test site, but not in production, with the exact same plugin code, but a slightly different environnement. On production site, the problem is the following: If i add a parameter to the shorcode, the shortcode seems not being interpreted and parsed at all. So adding this to my post body:

[my_shortcode_tag category=who]
[my_shortcode_tag category="who"]
[my_shortcode_tag category='who']
[my_shortcode_tag category=]

results in front-end with the same display, no shortocde is parsed and interpreted. As soon as i add another one without parameter, like this:

[my_shortcode_tag category=who]
[my_shortcode_tag category="who"]
[my_shortcode_tag category='who']
[my_shortcode_tag category=]
[my_shortcode_tag]

All shortcode start to work!... Everyone is interpreted and the display is correct.

Here is the shortcode function code:

function my_shortcode_tag($atts = [], $content = null, $tag = '')
    {
        // normalize attribute keys, lowercase
        $atts = array_change_key_case((array)$atts, CASE_LOWER);

        // override default attributes with user attributes
        $sc_atts = shortcode_atts([
            'height' => '400px',
            'category' => 'all'
        ], $atts, $tag);

        $result = 'nothing';

        $category = $sc_atts['category'];
        $height = 'height="' . $sc_atts['height'] . '"';
        DOFF_Utils::getLogger()->info("doff_show_office_stats_fn");

        // here some code to change result, based on $category and $height values
        $result .= '[gfchartsreports gf_form_id="9" type="total" maxentries="10000" custom_search_criteria=\'{"status":"active","field_filters":{"0":{"key":"24","value":"user:dental_office_id"}}}\']';
        $result .= ' ' . __('questionnaires envoyés');
        $result .= '<br/>';
        $result .= '[gfchartsreports gf_form_id="8" type="total" maxentries="10000" custom_search_criteria=\'{"status":"active","field_filters":{"0":{"key":"295","value":"user:dental_office_id"}}}\']';


        return do_shortcode($result);
    }

Any idea what could be wrong here ?

  • you might need to add to your question the code of the shortcode function so it can be seen, especially what arguments are declared. – majick Apr 27 '18 at 9:16
  • Can you provide your shortcode function code? – Mat Apr 27 '18 at 10:58
0

Not enough code in the question to pinpoint the problem, but I would call what you are trying to do "shady". Usage of do_shortcode should be confined to extreme cases when it is impossible, or extremely hard, to do anything else, as using it is the less evil equivalent of using eval.

What you should do is to figure out the actual APIs the shorcodes call, and just call the API directly. Even if you don't care about the performance gain, it will be so much easier to debug things.

  • Thanks! inside code have been added, and it's a string composed of different shortcodes, that could be added manually yes, but that is easier to add like this. Would you then suggest to just add the shortcodes directly to the page ? – Contact OGO May 1 '18 at 6:54
  • I am 100% sure it is easier to manipulate strings, it is just so much harder to debug and you always have do worry about properly escaping everything – Mark Kaplun May 1 '18 at 7:57
0

Let's see what happens here. WordPress evaluates the post content (or a widget text field that has shortcodes enabled), which is essentially a string. Let's name that string $the_content. It calls do_shortcode to find and evaluate any shortcodes in $the_content. It finds your shortcode and calls my_shortcode_tag with attributes $atts, empty $content (because it is a self closing shortcode) and $tag which contains the name of your shortcode (presumably my_shortcode_tag).

Now, you start assembling a string called $result. Unfortunately, this part of your code is incomplete, so it is implossible to say what $result looks like. Anyway, normally a shortcode would simply return a html string, which would then be inserted in $the_content. You, however, parse $result through the shortcode evaluator once more (return do_shortcode($result) in stead of the customary return $result).

This is where things get interesting. As you can see from the source code of do_shortcode it will check $result for all known shortcodes and evaluate them. If $result contains a substring [my_shortcode_tag you will enter an infinite loop and it depends on your server settings when and how the process is killed. Also, you could inadvertedly be calling another shortcode from inside $result, which might give some really strange results.

  • Thanks! inside code have been added, and it's a string composed of different shortcodes, that could be added manually yes, but that is easier to add like this. Would you then suggest to just add the shortcodes directly to the page ? – Contact OGO May 1 '18 at 6:53
  • In general nested shortcodes should be no problem. They just make debugging more difficult, because they're difficult to follow for humans. – cjbj May 1 '18 at 8:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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