WordPress侧边栏如何运行PHP代码的教程 WordPress 第1张

为了方便快捷的调用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;
}

之后你就可以在小工具下的文本工具中写入php代码并且能够运行。

发表回复

后才能评论