• Resolved Leon Williams

    (@thes2life)


    If I have a snippet that contains a PHP function and immediately uses that function, when I use the snippet, I am

    1. Declaring a function
    2. Calling said function

    Alright, cool. The problem is… if I want to use that snippet a second time, I am

    1. Redeclaring a function (Why, hello there, fatal error.)
    2. Calling said function

    So, I haven’t tried it yet, but I figure I can probably make all my functions on a file somewhere else and just have my snippets call for them as I need them–which is doable enough.

    My question is, is there a better way I should or could go about this? Is there somewhere I can just list out my snippet functions and somehow have them exported/imported with the snippets or something to that effect so I can keep them together?

    Thanks to anyone in advance for your help/input, and thanks @johan Steen for your awesome work. Wonderful plugin.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi,

    What you’re seeing is normal. If you declare a PHP function in a snippet, and use that snippet twice in a page, would be the same as writing the same function twice in a PHP file. Which PHP does not allow.

    An idea how you can work around this, depending on scenario, if you want to keep the functions in the snippet library so they can export/import with the rest of the snippets.

    First, create a PHP snippet that will only contain the functions you want to have. Let’s say you call that snippet functions, then it will look something like this.

    function foo()
    {
        echo 'foo';
    }
    function bar()
    {
        echo 'bar';
    }

    And then let’s create a PHP snippet that uses a function, let’s call that snippet foo.

    foo();

    Now when you write a page, you can use the snippet foo multiple times by first using the functions snippet. So the page would look something like this.

    [functions]
    some text
    [foo]
    some more text
    [foo]

    This solution has one gotcha though. And that is if you use this in posts, and output those posts in a loop, functions might be called twice, and then you get the redeclare problem again. For single pages this solution works though. If you want to be able to use it in a loop for instance. Then you can do like this instead. Never call the functions snippet from a page or post, instead use the getSnippet() method in the plugin. And add this line of code to your theme’s functions.php

    eval(PostSnippets::getSnippet('functions'));

    And all functions you have declared in your functions snippet will be available too all other php snippets whenever you insert them in posts and pages.

    Hope this helps.

    Cheers,
    Johan

    Thread Starter Leon Williams

    (@thes2life)

    Ah, I see. This helps indeed. Thank you very much for the detailed explanation.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[ Question / Suggestion ? ] Custom PHP Function Sheet?’ is closed to new replies.