神秘博客

让WordPress侧边栏文本小工具运行php代码

其实,要让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后台的外观》小工具中,添加一个文本工具到侧边栏试试效果。

版权说明:
点赞

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

觉得文章有用就请我吃包辣条吧

微信扫一扫打赏