Allow PHP in WordPress default text widget

The default text widget in WordPress only supports HTML. You can extend the functionality of the text widget by making it possible to execute PHP.

Put this code in the functions.php of the theme you are using:

add_filter('widget_text', 'php_text', 99);
 
function php_text($text) {
 if (strpos($text, '<' . '?') !== false) {
 ob_start();
 eval('?' . '>' . $text);
 $text = ob_get_contents();
 ob_end_clean();
 }
 return $text;
}

Always backup your functions.php before changing it!

Default image
Ronald Heijnes

Since 2008 I keep myself busy with the functionality, management, maintenance and performance of self hosted WordPress. I like to share this knowledge. All in my spare time!

Articles: 44
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x