其实,要让WordPress侧边栏的文本小工具运行PHP代码的功能还是比较简单的,只需要将以下代码添加到当前主题的 functions.php 文件即可:
//实现侧边栏文本工具运行PHP代码 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; }
添加好代码,并保存更新functions.php 文件后,我们可以在WordPress后台的外观》小工具中,添加一个文本工具到侧边栏试试效果。