0

I have created a custom admin page, which has a bunch of option fields available. One of them I use to enter some HTML code, and I was wondering if it was possible to also add php into that field, for it to be executed when that option is called in my template?

Thanks for any pointers.

2 Answers 2

0

In principle this is easyliy possible, if you tell your template to evaluate the contents of the field. But this way you are probably going to introduce lots of security problems into your code. In most cases it is not necessary to store php code in db and there is most certainly a better way of solving your problem.

0

As another poster pointed out, this is almost always a bad idea for security reasons. If you insist on doing it this is how. Since you are using a custom field that can have both php and html, your best bet is to use a shortcode. Something like this will work:

In the file that holds your functions:

function wpsc_do_php($atts, $content){
    /*Do some basic validation of the $content field 
     *to ensure that it is valid php code and that it
     *is only using functions that you allow.
     */

    return eval($content);
}
add_shortcode('php','wpsc_do_php');

In your custom field, anywhere you want to do php, wrap it in [php][/php] instead of <?php...?>, like this:

<html>
    <p>Some html formated text with some php content like [php]date('Y-m-d')[/php]</p>
</html>

Then in your theme (or wherever you want to display your custom field), use this code:

echo do_shortcode($my_custom_field_contents);

Again, while this should work, it is a bad idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.