0

I am trying to get 2 shortcodes (single-include and single-factsheet) on one page. The Problem is, that only the first shortcode is getting executed. The second shortcode on a page simple returns the shortcode string.

This code is within the plugin:

add_filter('the_content', 'initQmediaRechnerSnippet');
function initQmediaRechnerSnippet($content)
{
    $pattern = "/\[([^=]*)=?(.*)?\]/";

    if (preg_match($pattern, $content, $treffer))
    {
        $snippetName = $treffer[1];
        $snippet = '';
        switch ($snippetName) {
            case 'kreditkarte':
                $snippet = qRechnerInclude(API_CALC_KREDITKARTE, '');
                $content = str_replace('[' . $snippetName . ']', $snippet, $content);
                break;
            case 'single-include':
                list($ids, $product) = explode(":", $treffer[2]);
                $snippet = qRechnerInclude(str_replace('$PRODUCT$', $product, API_CALC_SINGLEINCLUDE . $ids), '');
                $content = str_replace('[' . $snippetName . '=' . $ids . ':' . $product . ']', $snippet, $content);
                break;
            case 'single-factsheet':
                list($ids, $product) = explode(":", $treffer[2]);
                $snippet = qRechnerInclude(str_replace('$PRODUCT$', $product, API_CALC_SINGLEFACTSHEET . $ids), '');
                $content = str_replace('[' . $snippetName . '=' . $ids . ':' . $product . ']', $snippet, $content);
                break;
            default:
                ;
                break;
        }
    }
    return $content;
}

How can I get this working? (removing the breaks did not work) Replacing preg_match with preg_match_all stops the execution of all shortcodes.

5
  • 3
    Why aren't you using the shortcode API?
    – cjbj
    Commented Sep 25, 2016 at 15:56
  • 1
    no idea what is it that you are doing but it is definatly not a shortcode Commented Sep 25, 2016 at 16:00
  • I haven't developed this plugin, I am just trying to extend it with single-factsheet. The rest of this plugin is connecting to a solr database and gets a lot of data. It would be that ease to create a new plugin just for this shortcodes.
    – Erkan Boga
    Commented Sep 25, 2016 at 16:04
  • 1
    there is little point in maintaining junk code, rewrite it Commented Sep 25, 2016 at 16:23
  • 1
    Of course when using preg_match_all you have to execute the code for each match in a loop. But I doubt it would work as expected since the regular expression used isn't designed for this. You're lucky it even worked for the first shortcode so far. Replacing this content filter with proper shortcode filters would probably solve a lot of problems.
    – Robert
    Commented Sep 25, 2016 at 18:46

1 Answer 1

3

As indicated in the comments this code has nothing to do with shortcodes in the WordPress sense, but it is easy to see why it will never evaluate more than one pattern.

preg_match returns true if there is at least one instance of the needle found in the haystack. So, no matter how many "shortcodes" it finds, PHP will only execute the following code once, replacing one string with the other.

1
  • I vote to say that this answer is usefull and a found usefull too the comment of @Robert : "Replacing this content filter with proper shortcode filters would probably solve a lot of problems"
    – mmm
    Commented Sep 25, 2016 at 21:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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