Title: Using PHP in Custom Fields
Last modified: August 19, 2016

---

# Using PHP in Custom Fields

 *  Resolved [vectyr](https://wordpress.org/support/users/vectyr/)
 * (@vectyr)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/using-php-in-custom-fields/)
 * Ok, so I have seen a couple of topics relating to this but none have really answered
   my question. I want to be able to just simply use php in a custom field. Like
   so:
 * `<?php include("term-sample.php"); ?>`
 * It’s not important to know WHY I want to do it, it’s just that I’ve explored 
   other options and this is how I want to handle it without creating more templates.
 * Are there any ideas out there? Thanks in advance!

Viewing 15 replies - 1 through 15 (of 28 total)

1 [2](https://wordpress.org/support/topic/using-php-in-custom-fields/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/using-php-in-custom-fields/page/2/?output_format=md)

 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/using-php-in-custom-fields/#post-1384143)
 * I don’t think you can use PHP in a custom field. Doesn’t mean you can’t use `
   term-sample.php`, though and simply grab the custom string when you’re ready 
   to include a file.
 *  Thread Starter [vectyr](https://wordpress.org/support/users/vectyr/)
 * (@vectyr)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/using-php-in-custom-fields/#post-1384146)
 * I’m not familiar with custom strings. I have a series of about 30 different forms.
   I need to have each form appear on a specific page. I also need to export the
   xml file for this site and be able to upload it to another site and have the 
   forms visible there.
 * PHP code in a custom field would be perfect for this since custom fields are 
   saved in the xml export file.
 *  Thread Starter [vectyr](https://wordpress.org/support/users/vectyr/)
 * (@vectyr)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/using-php-in-custom-fields/#post-1384150)
 * I need to be able to replicate a particular site an infinite amount of times 
   and be able to change the forms across all of the sites via a single php file
   that they can all link to.
 * I want each site to pull the form via php. I think that makes the most sense,
   unless there is another secret way that is awesome.
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/using-php-in-custom-fields/#post-1384167)
 * If you could use PHP in a custom field, how would you be grabbing that field 
   and using it? I’m pretty sure that there will be a simple workaround.
 *  Thread Starter [vectyr](https://wordpress.org/support/users/vectyr/)
 * (@vectyr)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/using-php-in-custom-fields/#post-1384173)
 * ok another question. I think I found a workaround.
 * This grabs the page slug:
    <?php echo $post->post_name; ?>
 * How would I do an if/else statement to look for a page slug and then include 
   a php file on the page?
 * <?php include(“term-life-insurance.php”); ?>
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/using-php-in-custom-fields/#post-1384178)
 *     ```
       <?php
       if( is_page( 'life-insurance') ) include(TEMPLATEPATH. 'term-life-insurance.php');?>
       ```
   
 * assuming that your included file is within the theme folder.
 * [http://codex.wordpress.org/Conditional_Tags#A_PAGE_Page](http://codex.wordpress.org/Conditional_Tags#A_PAGE_Page)
 *  Thread Starter [vectyr](https://wordpress.org/support/users/vectyr/)
 * (@vectyr)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/using-php-in-custom-fields/#post-1384193)
 * Dude…you ROCK.
 * Ok, this works but I’m not sure it’s the best way to go about it. I’m going to
   place about 30 of these statements in my subpage template file, so that if it
   detects that it’s on a certain page, it will load the particular php file which
   contains that particular form.
 * I think this should work great. Any cons to doing it this way?
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/using-php-in-custom-fields/#post-1384209)
 * 30 if/else statements is rather a lot. Can’t help wondering if there might not
   be a more elegant way of doing this. Your original idea of using a custom field
   might be worth investigating further. Assuming that this field was called “template”
   and simply held the template file name, perhaps you could use something like:
 *     ```
       <?php
       if( get_post_meta($post->ID, 'template', true) ) {
       	$template = get_post_meta($post->ID, 'template', true);
       	include ( TEMPLATEPATH . '/' .get_post_meta($post->ID, 'template', true) );
       }
       ?>
       ```
   
 *  Thread Starter [vectyr](https://wordpress.org/support/users/vectyr/)
 * (@vectyr)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/using-php-in-custom-fields/#post-1384213)
 * That’s a pretty good idea. With the way I’m doing this now, there isn’t going
   to be anything in the editor. Is there a way to use php in the editor?
 *  Thread Starter [vectyr](https://wordpress.org/support/users/vectyr/)
 * (@vectyr)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/using-php-in-custom-fields/#post-1384215)
 * Now, I’m naming the php files exactly the same as the page slug. Could I put 
   some php code in the editor that would look for the page slug and drop in the
   corresponding php file if the files were in a root folder such as /pages/?
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/using-php-in-custom-fields/#post-1384235)
 * What do you mean by “the editor”? If you mean Admin/Appearance/Editor, then you
   can use PHP. If you mean the HTML/Visual editors in the Edit Post/Page, then 
   no. Not without an additional runphp type plugin. And I’m not 100% sure how well
   the current ones work with 2.9.1.
 * [http://wordpress.org/extend/plugins/tags/exec](http://wordpress.org/extend/plugins/tags/exec)
 * Personally I edit the templates offline and then upload them.
 * As for the include path, yes – you could pull in external files from a folder
   such as /pages/ but you’d need to include the full path from the document root.
 *  Thread Starter [vectyr](https://wordpress.org/support/users/vectyr/)
 * (@vectyr)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/using-php-in-custom-fields/#post-1384244)
 * Sorry for the confusion, but yes, I meant the WordPress Editor. I’m just trying
   to figure out how to tie a php include to a certain page slug using **one** template.
 * The only way I can conceive of this at this point is either to make 30 templates,
   or to make one with 30 if/else statements. OR, if there was a way to use ONE 
   if/else php statement that checked for the page slug and then substituted the
   pageslug for the php file. Let me see if I can crudely diagram this:
 * If pageSlug = “<code that get’s the page slug>”
    then include = (“/pages/<code
   that get’s the page slug>.php”)
 * I hope that makes sense! I’m a php n00b here, just doing my best!
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/using-php-in-custom-fields/#post-1384256)
 * How about:
 *     ```
       <?php file_exists( 'path_to'/ . $post->post_name ) include ( 'path_to'/ . $post->post_name );?>
       ```
   
 *  Thread Starter [vectyr](https://wordpress.org/support/users/vectyr/)
 * (@vectyr)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/using-php-in-custom-fields/#post-1384257)
 * [http://fortquotes.com/get-a-quote/life-insurance-quote/return-on-premium-quote/](http://fortquotes.com/get-a-quote/life-insurance-quote/return-on-premium-quote/)
 * Hmm, throwing an error.
 *  Thread Starter [vectyr](https://wordpress.org/support/users/vectyr/)
 * (@vectyr)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/using-php-in-custom-fields/#post-1384260)
 * I see where you are going and I think you’re getting close.
 * Would it be something like:
 * <?php file_exists( ‘[http://www.fortquotes.com/pages’/](http://www.fortquotes.com/pages’/).
   $post->post_name ‘.php’ ) include ( ‘[http://www.fortquotes.com/pages’/](http://www.fortquotes.com/pages’/).
   $post->post_name ‘.php’ ); ?>

Viewing 15 replies - 1 through 15 (of 28 total)

1 [2](https://wordpress.org/support/topic/using-php-in-custom-fields/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/using-php-in-custom-fields/page/2/?output_format=md)

 The topic ‘Using PHP in Custom Fields’ is closed to new replies.

## Tags

 * [page slug](https://wordpress.org/support/topic-tag/page-slug/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [slug](https://wordpress.org/support/topic-tag/slug/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 28 replies
 * 3 participants
 * Last reply from: [ThorHammer](https://wordpress.org/support/users/thorhammer/)
 * Last activity: [16 years, 6 months ago](https://wordpress.org/support/topic/using-php-in-custom-fields/page/2/#post-1384541)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
