Support » Themes and Templates » Execute PHP within custom field

  • I’m trying to execute a small bit of PHP on a page. I can add it to the template, but that won’t work, seeing as that each page will have a different ID.

    The code I’m including is
    <?php echo rating_bar('unique','5'); ?>

    The “unique” will be different in each page/post. I could use the code within the post/page itself, but that can get sloppy in regards to my template, theme, and will be difficult to put the code in the same spot, for everyone posting.

    I tried adding a custom field. It gets output into the template where I called it, but doesn’t actually execute the PHP, it just gets output as text.

    Is there a way I can force the custom field to execute the PHP, instead of just dumping it into the template?

Viewing 7 replies - 1 through 7 (of 7 total)
  • I believe the custom field is subject to the kses.php filtering so it strips out any “unallowed” codes and tags.

    I personally think it is better to code it into the theme.

    Fair enough. Any suggestions on how to code it into the theme, if it needs to be different for every post/page?

    <?php echo rating_bar('unique','5'); ?>

    I tried changing the ‘unique’ to echo the variable I enter into the custom field, but that didn’t work.

    I would use a if statements. Example:

    <?php (if is_page('some page');) { echo rating_bar('unique','5'); } else { }; ?>

    That should show the rating bar on Some Page and if you on Another Page, it won’t show it.

    Thanks peiqinglong. That seems like it’d work.

    The only issue is, it’ll be going on literally thousands of pages (movie reviews). I need each page to have the same call, but with a different ‘unique’ part. That way each page’s ratings get kept separately in the database.

    You have to create an auto-increment to the unique. So do:

    <?php (is_page()) {echo rating_bar('some php code','5'); } ?>

    I don’t know what to code for the auto-increment, but at least this will put you in the right path.

    Thanks again, it seems like a logical solution, I just don’t know enough to replace your ‘some php code’ with something that’ll auto increment the number.

    Is there a way I could easily just echo the post ID in that space? that’s a number, and should work for my purposes, I just don’t know how to make it happen.

    Try this, not sure if it will work:

    <?php (is_page()) {echo rating_bar('the_ID()','5'); } ?>

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Execute PHP within custom field’ is closed to new replies.