• Resolved lutechi

    (@lutechi)


    Hello,

    Thanks so much for this plugin, I have been using it for awhile now with html and JS and recently I tried to do some customization with PHP for some projects.

    I was receiving the following error when I tried to echo a variable for an array:

    Fatal error: Call to a member function getContent() on a non-object in /wp-content/plugins/post-snippets/src/PostSnippets/Shortcode.php(97) : eval()’d code on line 1

    I already have the variable array on the template’s head, I just wanted to echo the function as a snippet and it was producing the error. However if I included the variable and echo on the same snippet the php will run fine.

    Thanks in advance.

    https://www.ads-software.com/plugins/post-snippets/

Viewing 1 replies (of 1 total)
  • Hi,

    That’s the nature of PHP, the PHP snippet gets evaluated in it’s own scope, and does not have access to variables outside the scope.

    As a workaround, what you can do is this, make the variables you need to access from a snippet to a global variable. Ie, for instance if you want to assign a value to a variable in your template, make it a global variable, like this:

    $GLOBALS['foo'] = 'bar';

    Then to access the variable in a PHP snippet, you have two options, either

    global $foo;
    echo $foo;

    or

    echo $GLOBALS['foo'];

    Hope this helps.

    Cheers,
    Johan

Viewing 1 replies (of 1 total)
  • The topic ‘PHP allow splitting code’ is closed to new replies.