WordPress.org

Ready to get started?Download WordPress

Forums

Execute PHP within custom field (8 posts)

  1. mikejandreau
    Member
    Posted 6 years ago #

    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?

  2. peiqinglong
    Member
    Posted 6 years ago #

    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.

  3. mikejandreau
    Member
    Posted 6 years ago #

    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.

  4. peiqinglong
    Member
    Posted 6 years ago #

    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.

  5. mikejandreau
    Member
    Posted 6 years ago #

    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.

  6. peiqinglong
    Member
    Posted 6 years ago #

    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.

  7. mikejandreau
    Member
    Posted 6 years ago #

    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.

  8. peiqinglong
    Member
    Posted 6 years ago #

    Try this, not sure if it will work:

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

Topic Closed

This topic has been closed to new replies.

About this Topic