Title: Moving template code to function in a plugin prevents code from running
Last modified: August 30, 2016

---

# Moving template code to function in a plugin prevents code from running

 *  [Rose](https://wordpress.org/support/users/eos-rose/)
 * (@eos-rose)
 * [10 years, 7 months ago](https://wordpress.org/support/topic/moving-template-code-to-function-in-a-plugin-prevents-code-from-running/)
 * I have the following code in a custom plugin, since it’s code I will want to 
   be able to call in all theme templates:
 *     ```
       function spoiler_statement() {
         if (get_post_meta( $post->ID, 'spoiler_statement', true)) {
           echo '<br /><b>Spoilers:</b> <div class="spoilers">' . get_post_meta( $post->ID, 'spoiler_statement', true) . '</div>';
         }
       }
       ```
   
 * When I insert `<?php spoiler_statement(); ?>` into my template to call the function,
   nothing happens.
 * When I insert the meat of my function into my template, it works exactly as expected:
 *     ```
       if (get_post_meta( $post->ID, 'spoiler_statement', true)) {
           echo '<br /><b>Spoilers:</b> <div class="spoilers">' . get_post_meta( $post->ID, 'spoiler_statement', true) . '</div>';
         }
       ```
   
 * I’m at a loss for my next step. I need to create too many functions. The entirety
   of my content is stored in taxonomies and custom fields. I really don’t want 
   to have to continue to add and update 500+ lines of code to my theme templates
   any time I change my theme.

Viewing 1 replies (of 1 total)

 *  [stephencottontail](https://wordpress.org/support/users/stephencottontail/)
 * (@stephencottontail)
 * [10 years, 7 months ago](https://wordpress.org/support/topic/moving-template-code-to-function-in-a-plugin-prevents-code-from-running/#post-6637011)
 * What’s probably happening is that you have to explicitly make the global `$post`
   variable available to your function:
 *     ```
       function spoiler_statement() {
         global $post;
   
         if (get_post_meta( $post->ID, 'spoiler_statement', true)) {
           echo '<br /><b>Spoilers:</b> <div class="spoilers">' . get_post_meta( $post->ID, 'spoiler_statement', true) . '</div>';
         }
       }
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘Moving template code to function in a plugin prevents code from running’
is closed to new replies.

## Tags

 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)
 * [functions](https://wordpress.org/support/topic-tag/functions/)
 * [templates](https://wordpress.org/support/topic-tag/templates/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 2 participants
 * Last reply from: [stephencottontail](https://wordpress.org/support/users/stephencottontail/)
 * Last activity: [10 years, 7 months ago](https://wordpress.org/support/topic/moving-template-code-to-function-in-a-plugin-prevents-code-from-running/#post-6637011)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
