Home > CMS Tutorial > WordPress > body text

How to run PHP code in WordPress text widget

藏色散人
Release: 2020-04-15 16:06:03
forward
2595 people have browsed it

The following column wordpress skills will introduce to you how to run PHP code in WordPress text gadgets. I hope it will be helpful to friends in need!

How to run PHP code in WordPress text widget

You can add any HTML code to the WordPress text widget, but the default PHP code added to the text widget cannot run. The following code is very useful!

Add the following code to your current theme’s functions.php file:

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

After that, you can add a text widget to the sidebar and enter the PHP function code in it, See if it can run normally.

Previously recommended plug-in: MO Widgets Although PHP can be run in gadgets, the above code is more convenient and practical.

The above is the detailed content of How to run PHP code in WordPress text widget. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:zmingcx.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template