WordPress does not allow you to include PHP code directly into a post. However, you can use a shortcode to include PHP code or call a PHP function in a post.
Here’s an example of how you can create a shortcode to include a PHP file in a post:
- Create a new file in your WordPress theme directory and name it “my_shortcode.php” (or any name you prefer).
- Add the PHP code that you want to include in the post in the “my_shortcode.php” file.
- Add the following code to your functions.php file:
codefunction my_shortcode_function() {
ob_start();
include 'path/to/my_shortcode.php';
return ob_get_clean();
}
add_shortcode('my_shortcode', 'my_shortcode_function');
- Replace ‘path/to/my_shortcode.php’ with the actual path to your “my_shortcode.php” file.
- Now, you can use the shortcode [my_shortcode] in your WordPress post to include the PHP code from the “my_shortcode.php” file.
Note that including PHP code directly in a post can be a security risk, so be sure to only include code that you trust and have verified to be safe.