In WordPress, directly inserting PHP code into a page isn't supported due to security and update concerns. Typically, PHP code is added to the core theme files. However, this practice has its drawbacks because when a theme update occurs, any custom PHP code within the core files can be overwritten.
To safely incorporate PHP code into your WordPress theme, we have two primary options:
Option 1: Using a Child Theme.
A child theme is a recommended approach for adding customizations or additional code to your theme. If you're unsure how to create a child theme, you can follow this guide to create one.
Custom PHP functions can be included in the functions.php file of your child theme. Once you've activated the child theme, access the functions.php file via your WordPress Dashboard: go to Appearance > Theme Editor.
Alternatively, you can move core theme files from the parent theme to the child theme directory and modify them as needed. This method may involve additional steps and code adjustments. For example, if you want to transfer the blog module to the child theme, you can refer to this relevant guide for assistance.
Option 2: Using a Third-Party Plugin.
If you're not comfortable creating a child theme or prefer a simpler solution, you can use a plugin such as "Code Snippets" to incorporate your custom PHP code.
After activating the plugin, you can add the codes from the WordPress Dashboard > Snippets > Add New option.
Code Snippets is a widely used plugin designed specifically for adding PHP code to your WordPress website.
Note: When working with child themes or plugins, it's crucial to write valid PHP code, free of syntax errors. Any code with errors can potentially disrupt your website's functionality, so be cautious and double-check your code for accuracy.