Since WordPress does not offer any possibility to add PHP code to the pages, you either have to customize your WordPress theme or use a plugin to execute PHP code on your WordPress website. You can also create your own plugin for it.
Code Snippets WordPress Plugin
With Code Snippets you can easily and clearly execute your PHP code on your website. If you use a Premium WordPress Theme or no Child WordPress Theme, the plugin should help you to execute your own code.
WordPress Functions File
If you don’t use a Premium WordPress Theme or a Child WordPress Theme, you can insert your code into the functions.php
of your WordPress Theme. Important is that WordPress must not update itself, so that functions.php
is not overwritten.
The functions.php
collects all functions of your WordPress theme and is used similar to plugins. The code in functions.php
is executed everywhere, on the website as well as in the admin area.
The functions.php
is located directly in the folder of your active WordPress Theme under: wp-content > themes > your-theme > functions.php
32 Useful Tricks for the Functions File – WPBeginner
WordPress Plugin
You can also create your own WordPress plugin to execute your own code. To do so, simply create a folder with the help of an FTP client: wp-content/plugins/my-plugin-name/
You can now create a file my-plugin-name.php
in this folder. To let WordPress know that this is a plugin, you have to write the following into the file my-plugin-name.php
:
<?php
/**
* Plugin Name: My Own Plugin
*/
// Deny unwanted access to the file
if (!defined('ABSPATH')) {
die('Direct access not permitted');
}
// Paste your code here
// ...
Afterward, you can activate your own WordPress plugin in the WordPress Admin (wp-admin) under Plugin.
You can also create the folder on your PC and place the file inside. Afterward, you just have to archive the folder into a .zip
and can upload the plugin in the WordPress Admin (also directly under Plugins).