2 ways to execute the PHP code in WordPress Text widget


2 ways to execute the PHP code in WordPress Text widget

Today tutorial we are going Learn about Execute the PHP code in WordPress Text widget WordPress inbuild Text Widget Does not Support the PHP Codes and snippets it supports HTML/javascript only.

Due to some security issues WordPress Does not have this Feature.But sometimes we Want to execute the PHP code in WordPress Text widget for Display some PHP widgets.

Here is the 2 Method for Execute PHP code in Text Widget

Method 1

Method 2

execute PHP code in Text Widget without using Plugin.

  • Just Copy the below Code and Paste it on your theme’s functions.php file
add_filter('widget_text', 'php_text', 99);

function php_text($text) {
 if (strpos($text, '<' . '?') !== false) {
 ob_start();
 eval('?' . '>' . $text);
 $text = ob_get_contents();
 ob_end_clean();
 }
 return $text;
 }
  • After Installing the Code
  • Go to Appearance > Widgets > Choose TEXT and add your PHP Code and save the Widget
  • For DEMO check the Below image

 

execute the PHP code in WordPress Text widget

Code

Output

execute the PHP code in WordPress Text widget

  • That’s all

Updated

If the Above PHP snippet not working Means try this one

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

If you have Any Doubts in this Topic Please Feel to Comment here I Will help you.



Was this article helpful?
Thanks!

Your feedback helps us improve Allwebtuts.com