Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to make widgets accept php, but without resorting to plugins.

So far I have enabled php for the widget_text but cant make it work for widget_title.

I am using the following code in functions.php:

add_filter('widget_text','execute_php',100);
function execute_php($html){
     if(strpos($html,"<"."?php")!==false){
          ob_start();
          eval("?".">".$html);
          $html=ob_get_contents();
          ob_end_clean();
     }
     return $html;
}

What would I have to do to enable php in the title as well?

share|improve this question
2  
Why would you want this? –  onetrickpony Mar 11 '13 at 15:33
    
i need to work around a commercial theme and plugin i dont want to edit, which handles all widgets & sidebars. –  user1721135 Mar 11 '13 at 17:40

2 Answers 2

up vote 1 down vote accepted

You can use the filter widget_title and a "virtual" shortcode. Of course, as One Trick Pony points out, the "why" (and the "what") is essential to find a proper solution...

Supposing the widget title was Title [php] More Title:

add_filter( 'widget_title', 'pseudo_php_wpse_90360', 10, 3 );

function pseudo_php_wpse_90360( $title, $instance, $id_base )
{
    if( FALSE === strpos( $title, '[php]' ) )
        return $title;

    $break_it = explode( '[php]', $title );

    $title = $break_it[0] . 'hello world' . $break_it[1];

    return $title;
}

The output is Title hello world More Title.

share|improve this answer
add_filter('widget_title', function($title_option, $all_options, $widget_id){

  // modify $title_option as you wish

  return $title_option;

}, 10, 3);

Note that a lot of 3rd party plugins don't apply this filter on their widgets, so this might not always work.

Create a new plugin and put this code inside it, no need for any eval here. Same for the code you have for widget_text.

And if you insist on allowing PHP code input from the widget settings, at least save it inside .php files, not the database (what if someone gains unauthorized access to your db?). Then you can require / include them.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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