• Resolved nickk40

    (@nickk40)


    I’m trying to migrate from another plugin to WPCode. I have a PHP snippet that works if I select Auto insert in combination with Run Frontend Only.

    However, I would like to use the Shortcode option instead of Auto insert. So I select Shortcode, which gives me [wpcode id=”4334″], which I insert as a shortcode (at the top) on one of my pages.

    But then the code snippet no longer works. I have set the priority of the code snippet to 1 and use no conditional logic.

    The snippet looks something like:

    echo("hello");
    add_action( 'wp_head', function () {
    	?><style>.foo {display:none!important;}</style><?php
    	...

    The hello gets printed, but the styling is not adjusted (which it is with the Auto insert option).

    Any idea what might be the problem?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter nickk40

    (@nickk40)

    To be more precise, if I convert the php snippet to:

    echo("hello1");
    add_action( 'wp_head', function () {
        echo("hello2");
    });

    Then using the snippet with Auto insert, it prints hello2 on the page. And using it as a Shortcode on the page, it prints hello1 without hello2.

    Plugin Author Mircea Sandu

    (@gripgrip)

    Hi @nickk40,

    From what I can see, I suspect that when you add the shortcode to the page the action “wp_head” has already been executed so there’s no way to add that output to it at that time.
    That’s why, if you use the auto-insert option, it works, as the code gets executed before the page is printed and the wp_head action is executed.

    You can probably try using “wp_footer” instead of “wp_head” and that will probably work but it will load the custom styles after most of the page is loaded so they might not be applied visually right away.

    Alternatively, you could simply use a HTML snippet and add your custom styles there, select auto-insert in the Site-Wide Header and use the conditional logic to target just that page. This way you don’t need to worry about the PHP part.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Shortcode for php snippet not working’ is closed to new replies.