Skip to content

Instantly share code, notes, and snippets.

@jerewall
Last active August 29, 2015 14:16
Show Gist options
  • Save jerewall/81ffeecbce5ab764df7c to your computer and use it in GitHub Desktop.
Save jerewall/81ffeecbce5ab764df7c to your computer and use it in GitHub Desktop.
How to execute PHP code in Text Widget without using Plugin
/* Sometimes we need to execute PHP scripts in text widget but by default WordPress doesn’t allow this feature because of
security issues.
Find the functions.php file of your current theme and add the following code at the end of the file.
*/
function php_execute($html){
if(strpos($html,"<"."?php")!==false){ ob_start(); eval("?".">".$html);
$html=ob_get_contents();
ob_end_clean();
}
return $html;
}
add_filter('widget_text','php_execute',100);
// use [template_directory_uri] within the sidebar or posts.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment