views:

77

answers:

2

I'm trying to figure out how to put PHP code into a custom field, but so far all that happens is that it displays the code literally, instead of executing it. So, how would I go about getting the PHP I'm inserting into a custom field to actually execute?—rather than display it like it was plain text.

+2  A: 

You'd probably have to eval() the field contents in your template. eval() is considered problematic for a number of reasons. So be very careful with this if you're not the only administrator of the blog.

I would also consider whether there are not better ways to achieve what you want, e.g. by having the PHP code in a library file in your WP template, and having the code deal with the custom fields and execute actions according to their value. This would make eval() unnecessary, limiting the potential risk of harmful commands written into the custom field.

Pekka
A: 

You could have a look at a plugin such as Exec-PHP, not too sure if it executes Custom Fields by default but might be a starting point.

Additionally you may find it easier to just pass the data you want through the custom field and have the PHP code execute it in the actual template file (single.php or page.php etc)

Stoosh