Allow PHP in WordPress Widgets without any plugin

Sometimes in your WordPress theme you need to execute custom php code in a widget, because you want to display different information according to the category you are in, or simply because you need to execute a php script into that widget.

There are a lot of plugins doing this task, adding a new type of widget generally called “php widget”, but rather than installing a plugin this simple job can be done simply adding in functions.php file of your theme these few lines:

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;
}

Now, when you add a text widget to your site, you can use PHP.

Dompdf Problem in CentOS

dompdf is an useful plugin to create html to pdf file. It works fine in windows environment but it creates some problem for some version of CentOS operating system. For example, it doesn’t show any error in the browser. To fix this problem, try the following:

  1. open terminal window/shell command window.
  2. run the command:- yum install php-xml
  3. run another command:- yum install php-mbstring

The above two commands setup two important packages which is mandatory for using dompdf plugins. There is a possibility that these two packages were already installed in your OS. No matter than. If everything goes okay, then I think your dompdf plugin will now work fine.