简体   繁体   中英

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.

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.

Is there any way to write some functions in FUNCTIONS.PHP to allow this? So a code like this can work as a WP post?

<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.

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. Please refer to Shortcode API for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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