Execute php code in WordPress widgets

Turbo SEO Tools
Nov
24

Execute php code in WordPress widgets

11/24/2017 12:00 AM by Admin in Wordpress


WordPress widgets area is an important element to design a web site. You can display your site’s related post, recent post, random post and other what you want to display using WordPress widgets area. In these Web developer resources, I will share a simple WordPress snippet that will help to Execute php code in WordPress text widget.

Simply add the below WordPress snippet in your theme’s functions.php file.

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

After adding this WordPress snippet your default WordPress text widgets will support PHP codes.

PHP enable WordPress text widgets

To test PHP codes in WordPress widgets area simply add some PHP codes in your WordPress default text widgets and see the result. You can try the following sample of php codes in WordPress text widgets.

<?php
 //Gets the IP address
 $ip = getenv("REMOTE_ADDR") ;
 Echo "Your IP is: " . '<b><span>'.$ip .'</span></b>';
 ?>

This code will display your IP address Where you place Your widgets it may be sidebar or footer area.



leave a comment
Please post your comments here.


seo agency

Best Free Seo Tools

Seo Friendly Title Generator

This is a free online tool to generate article title headline based on your subject input.

Create Safe & Secure Passwords

Use Password Generator to create highly secure passwords that are difficult to crack

Keyword Position Checker

Do you know your keyword position on Google or Yahoo? Use it and know the position of your website

Website SEO Score Checker

Is you website SEO friendly? Check and Optimize your website's visibility