简体   繁体   English

允许 Wordpress 帖子和页面中的 PHP 代码(使用 functions.php)

[英]Allow PHP codes in Wordpress posts & pages (Using functions.php)

I need to write pure PHP codes in WP posts - not using a plugin or snippets etc.我需要在 WP 帖子中编写纯 PHP 代码 - 不使用插件或片段等。

I know this Q has been asked before, but all were answered by using plugins etc. There were some plugins like PHP-Exec that could do the job, but due to latest WP/PHP updates they no longer work.我知道之前有人问过这个问题,但所有问题都通过使用插件等得到了回答。有一些像 PHP-Exec 这样的插件可以完成这项工作,但由于最新的 WP/PHP 更新,它们不再起作用。

Is there any way to write some functions in FUNCTIONS.PHP to allow this?有没有办法在 FUNCTIONS.PHP 中编写一些函数来允许这样做? So a code like this can work as a WP post?那么这样的代码可以作为 WP 帖子使用吗?

<p>
My text paragraph.
</p>

<?php
// Custom PHP codes - vary in wp posts
echo "Anything ... ";
?>

<p>
My second text paragraphs.
</p>

Thanks a lot guys!非常感谢你们!

Best approach would be to wrap your PHP code in a shortcode inside functions.php and then call it out of your page.最好的方法是将您的 PHP 代码包装在functions.php内的简码中,然后从您的页面中调用它。

function anything_function() { 
  
// your actual functionality.
$message = "Anything ... "; 
  
return $message;
}
// register shortcode
add_shortcode('anything', 'anything_function');

And then you can use the [anything] shortcode inside your templates.然后您可以在模板中使用[anything]短代码。 Please refer to Shortcode API for more information.请参阅简码 API了解更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM