Enable PHP code in your widget area without plugins!

Hey! In this WordPress code snippet I’ll show you how can enable PHP code in your WordPress site widget. Somebody uses plugins for this task but you can do it by this simple code. Just copy the code ┬áin your theme function.php file and allow php code in your widget area.

N.B : Please keep a backup your theme function.php file before any changes.


  • Go to Dashboard and then click on editor
  • copy and paste the code at the beginning of theme function.php file after this <?php line
add_filter('widget_text', 'enable_php_code', 99);
function enable_php_code ($text) {
if (strpos($text, '<' . '?') !== false) { ob_start(); eval('?' . '>' . $text);
$text = ob_get_contents();
return $text;
  • update your theme function.php.

Now you can write php code in your widget area.

Share on :

Leave a Reply

+ 6 = fourteen