• Resolved vectyr

    (@vectyr)


    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)
  • esmi

    (@esmi)

    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

    (@vectyr)

    hmm, still throwing an error. Unexpected ‘.’?

    Thread Starter vectyr

    (@vectyr)

    Thanks for all your help by the way. I think others reading this can benefit from it.

    esmi

    (@esmi)

    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

    (@vectyr)

    <?php if( file_exists( ‘https://www.fortquotes.com/pages/&#8217; . $post->post_name ) ) include ( ‘https://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: https://fortquotes.com/get-a-quote/life-insurance-quote/return-on-premium-quote/

    Thread Starter vectyr

    (@vectyr)

    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

    (@esmi)

    Good point!

    <?php if( file_exists( 'path_to/' . $post->post_name ) ) include ( 'path_to/' . $post->post_name .'.php' );?>

    Thread Starter vectyr

    (@vectyr)

    <?php if( file_exists( 'https://www.fortquotes.com/pages/' . $post->post_name ) ) include ( 'https://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: “https://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.

    https://fortquotes.com/get-a-quote/life-insurance-quote/return-on-premium-quote/

    esmi

    (@esmi)

    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 (https://) with an include statement.

    Thread Starter vectyr

    (@vectyr)

    ahhh. How do I go about finding the server document root?

    The problem is if other sites are set up like “https://www.othersite.com&#8221;, then using “/pages” is going to look for “https://www.othersite.com/pages&#8221; instead of “https://www.fortquotes.com/pages&#8221;, when “https://www.othersite.com&#8221; needs to pull the php files from “https://www.fortquotes.com/pages&#8221;

    esmi

    (@esmi)

    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:

    https://your_domain/tellme.php

    You’ll get a few screens of info – including the SERVER_DOCUMENT_ROOT

    Thread Starter vectyr

    (@vectyr)

    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!

    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)
  • The topic ‘Using PHP in Custom Fields’ is closed to new replies.