-5

I am trying to use some code that is stored as a settings option in WordPress. When I try to use the following code below however, it show up as plain text instead of executing. The option I have created contains the following PHP code:

echo get_option('profiles_templates_html'. $template .'');
6
  • 3
    You are trying to execute the PHP?
    – s_ha_dum
    Mar 31, 2014 at 15:25
  • 1
    Storing PHP code in the DB is so, SO wrong. Whatever you're trying to do, don't do it that way. Please.
    – Dan
    Mar 31, 2014 at 15:50
  • then how should I do it. I am trying to have a few different templates that I can use.
    – venn414
    Mar 31, 2014 at 16:34
  • Have you wrapped it in <?php ... ?>? Mar 31, 2014 at 16:44
  • as in in the option.. then yes. please help.
    – venn414
    Mar 31, 2014 at 17:48

1 Answer 1

1

If you really want to execute code stored in a database, a quick google search reveals that you can use the eval() function to do just that. However, many people do say that Eval is Evil...

Instead of storing the entire PHP code of an option in the database, try storing a simple boolean flag for your option and then test whether to get your template's option data from there:

<?php

// Somewhere else in your code, store the settings option upon saving settings
update_option( 'my_setting_for_template', 1 );

// Test if get_option returns 1 from the database
if( get_option( 'my_setting_for_template' ) === 1 )
{
    echo get_option('profiles_templates_html'. $template .'');
}

?>

References:

Your Answer

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

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