Execute PHP Code in WordPress Widget

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.

Anil Kumar Panigrahi

With more than 10 years experience in PHP and Founder of Anil Labs, a blog for PHP and related posts. contributed posts regarding CodeIgniter, CakePHP and Learn PHP online ... Contact : [email protected]

You may also like...

4 Responses

  1. PHP coding says:

    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.

  2. Emily says:

    Really I love to learn PHP. Thanks I really liked it. Thanks.

  1. October 10, 2013

    […] in author widget in wordpress blog. In earlier we learned about how to create WordPress widgets and execute PHP Code in WordPress Widget, this post is extended to earlier posts. PHP code for Author Widget in WordPress by Anil Kumar […]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.