• Hi- I’m having trouble getting the syntax correct for using fragment caching with W3 Total Cache.. Anyone been able to get this working properly?

    Here’s what I’ve got right now:

    <!-- mfunc -->
    <?php
    
    	$file = '/path/to/file.csv';
    	$fp = fopen($file, 'r');
    	$contents = fread($fp, filesize($file));
    	fclose($fp);
    
    	$contents = explode(",", $contents);
    
    	$var1 = $contents[0];
    	$var2 = $contents[1];
    	$var3 = $contents[2];
    
    	echo "";
    
    ?>
    <!-- /mfunc -->

    I’m then attempting to output those 3 variables later on down the page. I’d seen that mfunc needs an echo inside of it, so I dropped in that empty echo, but no good.

    Everything I’ve tried throws the error: Inavalid mfunc tag syntax. Correct: <!-- mfunc PHP code --><!-- /mfunc --> or <!-- mfunc -->PHP code<!-- /mfunc -->.

    Thanks!
    -Rob

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Contributor Frederick Townes

    (@fredericktownes)

    Do not use <?php ?> tags inside the mfunc comments.

    I was having the same problem. When I remove the <?php ?> tags, the code does not execute.

    My ‘solution’ is to comment out the line $output = htmlspecialchars('Invalid mfunc tag syntax. The correct format is: <!-- mfunc PHP code --><!-- /mfunc --> or <!-- mfunc -->PHP code<!-- /mfunc -->.'); on line 1292 in PgCache.php.

    I’m sure this is not a recommended solution, but it’s the only one that has worked for me – in previous versions of the plugin as well as in the current.

    I’m having the same problem, is there a better way to do this?

    If I place the <!– mfunc –> comment inside a <?php ?> block I get an unexpected character ‘<‘ error.

    Not sure what else to do ??

    I have to say I am puzzled by Frederick’s comment. He says to start PHP before MFUNC and to end PHP after MFUNC. But this obviously will result in a parse error, because <!-- mfunc --> is invalid PHP code. PHP uses a different syntax for comments.

    Yea I haven’t been able to find any great documentation on the fragment caching with W3TC, I’d love to see some real examples other than the out of context snippets in the FAQ.

    It seems to at least be processing the MFUNC comment when it throws back the “Invalid mfunc tag syntax” error but the php syntax error kills the whole thing

    I’ve been struggling with the docs on this as well. Essentially the <!– mfunc –> tags appear to be replacing the <?php ?> tags.

    What I believe happens is that the cache parser hunts through the code for mfuncs in the saved cache file then runs php against their contents at run time by throwing them into an output block.

    So essentially you need to think of it as outputting your dynamic php code into the contents of an html comment tag, the cache engine will find the comment block in the cached page and turn it back into php at run time.

    So if you have the following in your template:

    <!-- mfunc echo "Hello World 1<br/>"; --><!-- /mfunc -->
    <?php echo "Hello World 2<br/>"; ?>

    When caching is enabled you’ll get:
    Hello World 1
    Hello World 2
    when it’s disabled you’ll just get:
    Hello World 2.

    What I can’t figure out is a way to avoid writing the code twice, you have to do it once in the mfunc for caching and then again outside the mfunc for un-cached versions – and if caching is enabled but the page is not cached (eg if you are logged in as Admin) then both blocks will appear on the page!!!

    @benz001
    Put your function inside the conditionals, like so:

    <!-- mfunc echo "This happens when caching ON"; --><?php
        echo "This happens when caching OFF";
    ?><!-- /mfunc -->

    Hi Shush, that’s a definite improvement over my example – but still a great demonstration of the problem I’m trying to describe.

    When I turn caching on I no longer see “This happens when caching OFF”, so again the code has to be be duplicated to get the expected output – you write it once inside the opening mfunc bracket and then a second time between the mfunc wrappers.

    This isn’t a drama for trivial statements but for longer code blocks it makes testing, debugging and change management a nightmare – particularly as the mfunc called environment isn’t the same as the regular wordpress environment – so the same code may or may not work.

    I’ve been trying to think of a combination of comments and echos that would get the same result with only one php statement but I just don’t think it can be done.

    For complicated stuff I’d try to just call a single self-contained function — <!-- mfunc my_sweet_function(); --> etc… — or perhaps have an include to a whole bunch of code. At least that way I only have one block of code to maintain, even if I have to reference it twice.

    The thing that I’m not stoked with is that when caching is OFF your code within the mfunc conditionals is treated by the browser as a normal old HTML comment, so anyone can click “view source” and see your php. Not so cool. So calling a function in there gives a degree of privacy too.

    I’ll admit that I didn’t even consider the difference the mfunc environment might make. I have no idea of the limitations.

    Agreed, I’m not fond of littering html comments with php code.

    I know the call environment issue is why WP Super Cache has a late initialisation mode flag, so it can load the WP stack before running the mfunc blocks, haven’t found out how total cache deals with this yet – but it’s obviously a double edged sword as the entire point of caching is to not have to run the entire stack…

    I think the only real answer will have to be lazy loading fragments via ajax and abandon mfunc all together.

    In my particular use case I’m getting a really decent perf increase from W3 Total Cache with a single mfunc use that updates a rating/counter thing on each page.

    If you dig up any docs on mfunc, Total Cache, and the WP stack, let me know!

    Cheers

    Plugin Contributor Frederick Townes

    (@fredericktownes)

    Late init’ will be added in an upcoming release.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Invalid mfunc tag syntax’ is closed to new replies.