WordPress doesn't support directly inserting PHP code into a page due to security and update concerns.
Typically, PHP code is added to the core theme files. However, this practice has drawbacks because any custom PHP code within the core files can be overwritten when a theme update occurs.
To safely incorporate PHP code into your WordPress theme, we have two primary options:
1) Using a Child Theme
A child theme is recommended for adding customizations or additional code to your theme. If you're unsure how to create a Child Theme, follow the How To Make A Child Theme article.
The functions.php file of your child theme can include custom PHP functions. Once you've activated the child theme, you can access the functions.php file by going to WordPress Dashboard β Appearance β Theme Files 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.
Example:
If you want to transfer the Blog module to the child theme, refer to the Moving Blog module in the Child theme article.
2) Using a Third-Party Plugin
If you're uncomfortable creating a child theme or prefer a simpler solution, you can incorporate your custom PHP code using a plugin such as Code Snippets.
After activating the plugin, you can add the codes from the WordPress Dashboard β Snippets β Add New option.
The Code Snippets plugin is widely used to add PHP code to your WordPress website.
Note: When working with child themes or plugins, writing valid PHP code free of syntax errors is crucial. Any code with errors can disrupt your website's functionality, so be cautious and double-check your code for accuracy.