WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Using PHP in Custom Fields (29 posts)

  1. vectyr
    Member
    Posted 4 years ago #

    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!

  2. esmi
    Forum Moderator
    Posted 4 years ago #

    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.

  3. vectyr
    Member
    Posted 4 years ago #

    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.

  4. vectyr
    Member
    Posted 4 years ago #

    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.

  5. esmi
    Forum Moderator
    Posted 4 years ago #

    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.

  6. vectyr
    Member
    Posted 4 years ago #

    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"); ?>

  7. esmi
    Forum Moderator
    Posted 4 years ago #

    <?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

  8. vectyr
    Member
    Posted 4 years ago #

    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?

  9. esmi
    Forum Moderator
    Posted 4 years ago #

    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) );
    }
    ?>
  10. vectyr
    Member
    Posted 4 years ago #

    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?

  11. vectyr
    Member
    Posted 4 years ago #

    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/?

  12. esmi
    Forum Moderator
    Posted 4 years ago #

    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

    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.

  13. vectyr
    Member
    Posted 4 years ago #

    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!

  14. esmi
    Forum Moderator
    Posted 4 years ago #

    How about:

    <?php file_exists( 'path_to'/ . $post->post_name ) include ( 'path_to'/ . $post->post_name );?>
  15. vectyr
    Member
    Posted 4 years ago #

  16. vectyr
    Member
    Posted 4 years ago #

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

  17. esmi
    Forum Moderator
    Posted 4 years ago #

    My bad - forgot the if statement. Doh!

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

  18. vectyr
    Member
    Posted 4 years ago #

    hmm, still throwing an error. Unexpected '.'?

  19. vectyr
    Member
    Posted 4 years ago #

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

  20. esmi
    Forum Moderator
    Posted 4 years ago #

    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 );?>

  21. vectyr
    Member
    Posted 4 years ago #

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

  22. vectyr
    Member
    Posted 4 years ago #

    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.

  23. esmi
    Forum Moderator
    Posted 4 years ago #

    Good point!

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

  24. vectyr
    Member
    Posted 4 years ago #

    <?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/" 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/

  25. esmi
    Forum Moderator
    Posted 4 years ago #

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

  26. vectyr
    Member
    Posted 4 years ago #

    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", then using "/pages" is going to look for "http://www.othersite.com/pages" instead of "http://www.fortquotes.com/pages", when "http://www.othersite.com" needs to pull the php files from "http://www.fortquotes.com/pages"

  27. esmi
    Forum Moderator
    Posted 4 years ago #

    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

    You'll get a few screens of info - including the SERVER_DOCUMENT_ROOT

  28. vectyr
    Member
    Posted 4 years ago #

    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!

  29. ThorHammer
    Member
    Posted 4 years ago #

    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...?

Topic Closed

This topic has been closed to new replies.

About this Topic