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

1
2
3
4
5
6
7
8
9
10
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

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

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

Categories: CMSWordpress

4 Comments

PHP coding · September 26, 2013 at 6:16 am

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.

    Ali · September 26, 2013 at 6:16 am

    Nice shearing

Emily · November 19, 2014 at 5:43 pm

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

PHP code for Author Widget in Wordpress - Anil Labs · October 10, 2013 at 1:49 am

[…] 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

Avatar placeholder

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