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
![]() |
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;
}
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 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.
Nice shearing
Pingback : PHP code for Author Widget in Wordpress - Anil Labs
Really I love to learn PHP. Thanks I really liked it. Thanks.