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.

I have a one form, and I'm inserting it on page footer from a function file. I need to display a random form to each visitors and thus have a function developed to support this. However, with caching enabled the randomness doesn't work.

I'm using W3 Total Cache Plugin for caching.

So here, I need to exclude certain section of code from caching.

I tried to put the function inside Fragment Caching which should do the trick, but it doesn't.

We need to pass a random integer from a given array and based on that it loads random form in the footer.

Here is a code snippet for you to review:

<!--MFUNC {E7C5F12EBCDA5F83A41BF33D778ED} -->
   <?php
        //echo rand();
        $surveyforms=array("10","11");
        $ra=$surveyforms[array_rand($surveyforms)];
        echo($ra);
    ?>
<!--/mfunc {E7C5F12EBCDA5F83A41BF33D778ED} -->

Just This should print a random number from an array collection given every time a page is refreshed, however due to cache the result. If we disable caching then it works fine, however as you know caching is very important for performance so we can not disable it.

The same code also moved to the footer.php but doesn't work there as well.

Any idea, how to use fragment caching on function file? I am fine with any other idea that works.

Looking forward to your response.

Thanks

share|improve this question
    
is this still unsolved? –  birgire Jun 10 '13 at 9:32
    
Yes, this is still unresolved. –  Krunal Jun 10 '13 at 10:23

1 Answer 1

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 -->
share|improve this answer
    
@ialocin, thanks for the edit, but it's not a misprint if I recall correctly ;-) In the first example I'm showing how to use code1: <!-- mfunc W3TC_DYNAMIC_SECURITY code1--><!-- /mfunc W3TC_DYNAMIC_SECURITY--> and then in the latter one I'm showing the code2 example: <!-- mfunc W3TC_DYNAMIC_SECURITY-->code2<!-- /mfunc W3TC_DYNAMIC_SECURITY-->. –  birgire Jun 24 at 11:24
    
ok, I actually just handled a suggested edit here and - wrongfully it seems - assumed that the line echo "From code1: Here is a random number " . rand(0,1000); from code1 shouldn't be commented. Should I withdraw the edit or are you doing it? –  ialocin Jun 24 at 11:28
1  
No problem. I think the answer wasn't very clear on that, so let my try to improve it a little bit (hopefully) ;-) –  birgire Jun 24 at 11:33
1  
ok, so I leave it up to you :) –  ialocin Jun 24 at 11:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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