Title: [ Question / Suggestion ? ] Custom PHP Function Sheet?
Last modified: August 31, 2016

---

# [ Question / Suggestion ? ] Custom PHP Function Sheet?

 *  Resolved [Leon Williams](https://wordpress.org/support/users/thes2life/)
 * (@thes2life)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/question-suggestion-custom-php-function-sheet/)
 * 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](https://wordpress.org/support/users/johan/)
   Steen for your awesome work. Wonderful plugin.

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

 *  [Johan Steen](https://wordpress.org/support/users/artstorm/)
 * (@artstorm)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/question-suggestion-custom-php-function-sheet/#post-7056166)
 * 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](https://wordpress.org/support/users/thes2life/)
 * (@thes2life)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/question-suggestion-custom-php-function-sheet/#post-7056169)
 * 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.

 * ![](https://ps.w.org/post-snippets/assets/icon-128x128.gif?rev=2545587)
 * [Post Snippets - Custom WordPress Code Snippets Customizer](https://wordpress.org/plugins/post-snippets/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/post-snippets/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/post-snippets/)
 * [Active Topics](https://wordpress.org/support/plugin/post-snippets/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/post-snippets/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/post-snippets/reviews/)

## Tags

 * [function](https://wordpress.org/support/topic-tag/function/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [sheet](https://wordpress.org/support/topic-tag/sheet/)
 * [snippet](https://wordpress.org/support/topic-tag/snippet/)

 * 2 replies
 * 2 participants
 * Last reply from: [Leon Williams](https://wordpress.org/support/users/thes2life/)
 * Last activity: [10 years, 3 months ago](https://wordpress.org/support/topic/question-suggestion-custom-php-function-sheet/#post-7056169)
 * Status: resolved