WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Using PHP in Custom Fields

[Resolved] Using PHP in Custom Fields

  • vectyr
    Participant

    @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 15 replies - 1 through 15 (of 28 total)
  • esmi
    Forum Moderator

    @esmi

    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.

    vectyr
    Participant

    @vectyr

    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.

    vectyr
    Participant

    @vectyr

    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
    Forum Moderator

    @esmi

    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.

    vectyr
    Participant

    @vectyr

    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
    Forum Moderator

    @esmi

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

    vectyr
    Participant

    @vectyr

    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
    Forum Moderator

    @esmi

    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) );
    }
    ?>
    vectyr
    Participant

    @vectyr

    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?

    vectyr
    Participant

    @vectyr

    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
    Forum Moderator

    @esmi

    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.

    vectyr
    Participant

    @vectyr

    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
    Forum Moderator

    @esmi

    How about:

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

    @vectyr

    vectyr
    Participant

    @vectyr

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

Viewing 15 replies - 1 through 15 (of 28 total)
  • The topic ‘[Resolved] Using PHP in Custom Fields’ is closed to new replies.