Execute PHP Code in WordPress Widget

in CMS, Wordpress / 3 Comments

Hi friends, In the earlier post learned how to create wordpress widgets and in this post I will explain how to execute PHP code in your widgets in WordPress. Recently I have done in my “About The Author” widget in my blog. I have done this for get and display the Author image in the widget.

Execute PHP Code in WordPress Widget by Anil Kumar Panigrahi

Execute PHP Code in WordPress Widget by Anil Kumar Panigrahi

demo link for how to execute PHP code in your wordpress widget  - check author block image  

Follow the below steps to execute PHP code

Step 1:

Open functions.php (Theme Functions) of your activated theme.

Step 2:

Add the below code in that file

add_filter('widget_text','widget_custom_php_code',100);
function widget_custom_php_code($html_php_code)
    {
         if(strpos($html_php_code,"<"."?PHP")!==false){
               ob_start();
               eval("?".">".$html_php_code);
               $html_php_code = ob_get_contents();
            }
          return $html_php_code;    
    }

Step 3:

Add any line PHP code in your widget, just add below line of code and test it

<?php echo $_SERVER['REMOTE_ADDR'];?>

By this we can execute the PHP code in your widget without adding or installing plugins.

PHP Web developer with 6 years of experience in Web Development, Integrating web designing, Testing and Deployment , and developed medium level to large scale websites using PHP , and as blogger maintaining technical blog Contact : php.anil@gmail.com

Discussion 3 Comments

  1. PHP coding

    PHP is one of the open source language and that is reason have large number of open source framework and script available in market. Facebook still believe PHP is best language for web programming.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>