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 13 replies - 16 through 28 (of 28 total)

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

 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/using-php-in-custom-fields/page/2/#post-1384261)
 * My bad – forgot the `if` statement. Doh!
 * `<?php if( 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/page/2/#post-1384262)
 * hmm, still throwing an error. Unexpected ‘.’?
 *  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/page/2/#post-1384266)
 * Thanks for all your help by the way. I think others reading this can benefit 
   from it.
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/using-php-in-custom-fields/page/2/#post-1384267)
 * I’ll try getting the single quotes in the right place now…
 * `<?php if( 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/page/2/#post-1384273)
 * <?php if( file_exists( ‘[http://www.fortquotes.com/pages/&#8217](http://www.fortquotes.com/pages/&#8217);.
   $post->post_name ) ) include ( ‘[http://www.fortquotes.com/pages/&#8217](http://www.fortquotes.com/pages/&#8217);.
   $post->post_name ); ?>
 * Does this look right? If so, then I think something is wrong. I have a folder
   called “pages” in my root (/pages/) and a file inside called “return-on-premium-
   quote.php”
 * This is the page I am testing: [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/)
 *  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/page/2/#post-1384276)
 * Does ‘file_exists’ ignore the file extension (.php)? Does that need to be specified
   within the code? Dang it looks like it’s soooo close here.
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/using-php-in-custom-fields/page/2/#post-1384294)
 * Good point!
 * `<?php if( file_exists( 'path_to/' . $post->post_name ) ) include ( 'path_to/'.
   $post->post_name .'.php' );?>`
 *  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/page/2/#post-1384301)
 * `<?php if( file_exists( 'http://www.fortquotes.com/pages/' . $post->post_name))
   include ( 'http://www.fortquotes.com/pages/' . $post->post_name .'.php' );?>`
 * Am I supposed to be replacing ‘path_to/’ with the actual path? I need this code
   to target this exact path: “[http://www.fortquotes.com/pages/&#8221](http://www.fortquotes.com/pages/&#8221);
   because other sites will need to pull the php file from this one location. Everything
   looks right, but it still throws an error.
 * [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/)
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/using-php-in-custom-fields/page/2/#post-1384304)
 * > Am I supposed to be replacing ‘path_to/’ with the actual path?
 * Yes – you’ll need the full path from the server document root. You can’t use 
   a url ([http://](https://wordpress.org/support/topic/using-php-in-custom-fields/page/2/?output_format=md))
   with an include statement.
 *  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/page/2/#post-1384306)
 * ahhh. How do I go about finding the server document root?
 * The problem is if other sites are set up like “[http://www.othersite.com&#8221](http://www.othersite.com&#8221);,
   then using “/pages” is going to look for “[http://www.othersite.com/pages&#8221](http://www.othersite.com/pages&#8221);
   instead of “[http://www.fortquotes.com/pages&#8221](http://www.fortquotes.com/pages&#8221);,
   when “[http://www.othersite.com&#8221](http://www.othersite.com&#8221); needs
   to pull the php files from “[http://www.fortquotes.com/pages&#8221](http://www.fortquotes.com/pages&#8221);
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/using-php-in-custom-fields/page/2/#post-1384312)
 * Ask your hosts. Or see if it’s somewhere on your hosting account/management panel.
   If all fails, upload a phpinfo file. Create a new plain text file and add:
 *     ```
       <?php
       phpinfo();
       ?>
       ```
   
 * Save the file as tellme.php and upload it to the root folder of your server, 
   Then navigate to that file in your web browser:
 * [http://your_domain/tellme.php](http://your_domain/tellme.php)
 * You’ll get a few screens of info – including the SERVER_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/page/2/#post-1384322)
 * Ok, it works now. Here is the final code:
 * *SERVER_DOCUMENT_ROOT = the actual server document root which I discovered using
   your tellme.php technique.
 * `<?php if(file_exists('SERVER_DOCUMENT_ROOT'.$post->post_name.'.php')) { include'
   SERVER_DOCUMENT_ROOT' . $post->post_name .'.php' ; } ?>`
 * The code was missing some brackets but it works! SO the idea behind this code
   is to look at the page slug and then include the content from another .php file
   on the server with the same name as the page slug.
 * Doing so will allow me to replicate a website and control the content across 
   all of the other sites with ONE template and some master .php files.
 * Through a similar template, it will utilize the loop and allow the admin of the
   other sites to add custom content to certain pages. This should work out just
   fine. Thanks for all your great work Esmi, I hope this information helps others!
 *  [ThorHammer](https://wordpress.org/support/users/thorhammer/)
 * (@thorhammer)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/using-php-in-custom-fields/page/2/#post-1384541)
 * hm. this sounds like an answere for another of my challenges. I want to include
   a file in my post-template depending on the value of a custom field. like..
 *     ```
       <?php $value_for_template_1=get_post_meta($post->ID,"value_for_template_1",false); ?>
          <?php foreach($value_for_template_1 as $template_insert)
            {?>
       	<?php include ('$template_insert' '.php') 			<?
            }?>
       ```
   
 * Or something this way…?

Viewing 13 replies - 16 through 28 (of 28 total)

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

 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
