Exclude certain block from caching using Fragment Caching – doesn’t work [closed]

From the source:

Here (source) is the mfunc part in the 0.9.2.9 version of the W3TC plugin where the regular expression is:

$buffer = preg_replace_callback('~<!--\s*mfunc\s*' . W3TC_DYNAMIC_SECURITY . '(.*)-->(.*)<!--\s*/mfunc\s*' . W3TC_DYNAMIC_SECURITY . '\s*-->~Uis', array(
                &$this,
                '_parse_dynamic_mfunc'
            ), $buffer);

From this it looks like the setup should be

<!-- mfunc W3TC_DYNAMIC_SECURITY code1-->
code2
<!-- /mfunc W3TC_DYNAMIC_SECURITY -->

The mfunc callback is

function _parse_dynamic_mfunc($matches) {
    $code1 = trim($matches[1]);
    $code2 = trim($matches[2]);
    $code = ($code1 ? $code1 : $code2);

    if ($code) {
        $code = trim($code, ';') . ';';

        ob_start();
        $result = eval($code);
        $output = ob_get_contents();
        ob_end_clean();

        // ... cut ...

so we can see that it uses eval() on the code2 part if the code1 part isn’t set. Checking the PHP manual for this function:

http://php.net/manual/en/function.eval.php

it says:

The code mustn’t be wrapped in opening and closing PHP tags

Examples:

So I would think that a working example, where you have

define('W3TC_DYNAMIC_SECURITY', 'E7C5F12EBCDA5F83A41BF33D778ED' ); 

would look like this (untested) :

The code1 case:

<!-- mfunc E7C5F12EBCDA5F83A41BF33D778ED         
    echo "From code1: Here is a random number " . rand(0,1000);
-->
<!--/mfunc E7C5F12EBCDA5F83A41BF33D778ED -->

or the code2 case:

<!-- mfunc E7C5F12EBCDA5F83A41BF33D778ED -->
    echo "From code2: Here is a random number " . rand(0,1000);
<!--/mfunc E7C5F12EBCDA5F83A41BF33D778ED -->

Leave a Comment