Change an option string to a function [closed]

To do something like that, you would most likely have to use eval() on the text provided … which is a really bad idea.

You would be much better off providing a list of check boxes that have different values so the user can choose which items to show your sidebar on.

For example:

<input type="checkbox" name="condition[]" value="single" /> Single Post
<input type="checkbox" name="condition[]" value="page" /> Single Page
<input type="checkbox" name="condition[]" value="blog" /> Blog

etc.

Personally, I’d highly recommend trying to keep things as simple as possible for users. Maybe have something like the following instead on your theme options:

Show sidebar on blog? — None — (drop down box with list of available sidebars)

Show sidebar on single posts? — None —

Show sidebar on category archives? — None —

Show sidebar on tag archives? — None —

What you are trying to do can be made to work … but consider the average skill of your users and you might find that it is better to give them a list of options to choose for and then reference those options when drawing content.

For example, on your template, you might do the following.

$theme_options = get_option('my_theme_options', get_default_theme_options() );
if ( array_key_exists( 'sidebar_positions', $theme_options ) ) {
   if ( is_singular('post') ) {
       if ( array_key_exists( 'singular', $theme_options['sidebar_positions'] ) ) {
           if ( !empty( $theme_options['sidebar_positions']['singular'] ) ) {
               # Draw the specified sidebar
           }
       }
   }
}

Anywise, I strongly recommend against using eval and user input as that is an easy way to let people accidentally destroy things … or cause serious problems.

Limit what they can do, give them nice clean options, and then work off of those and your theme will be stronger for it.

(edit)
Since you want to provide real flexibility, you might do one of the following:

1. Write a meta language for your users.

For example, you might define a set of commands that are allowed.

Instead of is_single(), have them enter:
single

Or, for just a particular post:
single:12

Or, for a few posts:
single:12,24,25

Or, for their blog:
blog

If you want multiples chained together, maybe they enter:
single:12,24,25|blog

Then, you parse the entered value:

$entered_value = stripcslashes($_POST['whatever']);

And you write a small toolset to properly determine what they entered and what to do with it.

In the above example, your meta language has the following:

  • If there is a |, you need to break it into parts on the pipe symbol and you know that each part should be telling you what kind of thing to show on and possibly which specific ones to show on.

You might use explode(‘|’, $entered_value) to grab all of these

  • For each piece separated by |, you should have either item_type or item_type:list_of_ids

You can then explode each piece by : (if it is found) to grab the type of item and specific ID values (spliting them by commas and stripping white space) or just store the entire thing (if no : is found).

Once done, you could then build an options array noting where to show the sidebar. For the above example, the final option value might look like:

array(
    'single' => array( 12, 24, 25 ),
    'blog' => true
)

You could then query your custom options to display your sidebar … without the nightmare that is eval. You could also check your language logic and report to the user when they made some kind of error.

An alternative would be to use filters:

$show_sidebar = apply_filters(‘sidebar_on_blog’, false);

$show_sidebar = apply_filters(‘sidebar_on_single, false, $post->ID);

The users could then add to their functions file:

add_filter('sidebar_on_blog', 'show_my_sidebar_on_blog');
function show_my_sidebar_on_blog($show) {
   return true;
}

add_filter('sidebar_on_single', 'show_my_sidebar_on_single', 10, 2);
function show_my_sidebar_on_single($show, $post_id) {
    $show_on = array(10, 23, 25);
    if ( in_array( $post_id, $show_on ) ) {
       $show = true;
    }
    return $show;
}

Of course, then you are wanting your users to program.

If you really must use eval (very bad juju!):

try {
    $user_input = $options['condition'];
    $condition = eval("{$user_input}");
    if ( $condition ) {
        echo "sidebar on";
    }
} catch ( Exception $e ) {
    echo "<p>Exception Ahoy!</p><pre>" . print_r($e, true) . "</pre>";
}

If you need to debug that, you should echo the user input out before running eval to make sure it is a proper statement. In your example, you left out a semi colon … so if you tell them not to put it in, the eval statement would become:

$condition = eval("{$user_input};");

You are welcome … but this is asking for serious problems and support headaches.