1

Before I had the following code in the function.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;
}

Then I have updated WP to the version 4.4.1 and now it's not working anymore. Is there any other solution?

2
  • 1
    write you own widget that does what you need? This code you show is just a horrible idea. Jan 23, 2016 at 17:34
  • 1
    Yeah, I agree with Mark. This really is a bad idea. You should in theory be able to write a widget that would execute PHP safely, but one mistake and your site is compromised. And, honestly, if you are asking how to do this you don't have the knowledge to do it safely.
    – s_ha_dum
    Jan 23, 2016 at 19:16

2 Answers 2

3

Better to write your own widget that does precisely what you need instead of something like this.

However, if you really want to execute arbitrary PHP in a widget, use a plugin specifically designed for that task: https://wordpress.org/plugins/php-code-widget/

I maintain this plugin specifically so that people don't resort to doing things like what you have in your post. The PHP Code Widget is basically a copy of the text widget, but which also runs PHP Code.

Nevertheless, PHP Code Widgets are a bad-idea™ and should be avoided. Make a custom widget with your static code instead.

2
  • with 100k active installs seems like your appeal to only short term use it, is ignored by too many users :( Jan 23, 2016 at 18:10
  • Mark, to you, 100k is a lot. To me, it's down from 400k years ago, and I'm concerned with millions of sites. I'm happy in my progress. ;)
    – Otto
    Jan 30, 2016 at 0:52
0

I have faced the same problem, what I have noted is, professionally it is not advised to use plugin or functions.php for executing php in widgets, that's why such as now that functions.php technic don't work.

It is advised to construct your own widget, this is what I'm going to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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