Sunday, June 26, 2016

Execute php inside WordPress widget without plugin

Execute php inside WordPress widget without plugin

Hi Firends Hope you are doing good.Today I am going to tell how we can excute php code inside wordpress widget. Sometimes in your theme you want to excute custom php code in a widget,Because you want to display information according to the category, Or just simply you want to excute php code inside the widget. There are lot of plugins are available to run custom php code inside widget area.I will not recommend to Install Plugin, because plugin will consume extra resources to complete this task.but rather than installing a plugin this simple job can be done simply adding in functions.php file of your theme these few lines. Step1: Copy this Code and Paste in functions.php inside theme folder
function execute_php_text_widget($html){
 if(strpos($html,"<"."?php")!==false){ 
  ob_start(); 
  eval("?".">".$html);
  $html=ob_get_contents();
  ob_end_clean();
 }
 return $html;
}
add_filter('widget_text','execute_php_text_widget',100);
Which will turn the default Text widget into a php enabled widget. For Testing Purpose Just Paste This Code in Text Widget
//Hello World
<?php echo "Hello World!" ?>

0 comments:

Post a Comment