Execute PHP code in WordPress

Execute PHP code in WordPress

Since the question already arose in relation to Helpful, I would like to answer it here briefly. You will learn here how to execute your own PHP code on your WordPress website. This is especially useful if you want to or sometimes even have to make small adjustments to your website.

Code Snippets WordPress Plugin

The easiest way to execute your own PHP code on the website is to use the Code Snippets plugin from Shea Bunge. The plugin can be downloaded for free from the WordPress repository. So it is ready to use with just a few clicks. You can also find the plugin directly in your WordPress Admin (wp-admin) under Plugins if you are looking for it.

Code Snippets WordPress Plugin

WordPress Theme functions.php

Another possibility is the functions.php of your WordPress Theme. You can find it with the help of an FTP client in the folder wp-content > themes > my-wordpress-theme > functions.php. It is important that you use a child theme or your own WordPress theme so that the changes are not overwritten by updates.

WordPress Plugin

The last and probably best option is the solution with a custom WordPress plugin. This has the advantage that you only execute the code that is necessary for you and you can control yourself if and when something needs to be updated. You can also deactivate the plugin at any time, if you don't need your customizations anymore.

You create your own plugin by creating a new folder in your plugins folder: wp-content/plugins/my-plugin-name/

In this folder you will now create your plugin file: my-plugin-name.php in this file you will pack all your PHP code, as soon as you have done the following:

<?php
/**
 * Plugin Name: My Own Plugin
 */

// Deny unwanted access to the file
if ( ! defined( 'ABSPATH' ) ) {
   die( 'Direct access not permitted' );
}

// Paste your code here
// ...

Afterwards 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. Afterwards you just have to archive the folder into a .zip and can upload the plugin in the WordPress Admin (also directly under Plugins).

Did you find this article valuable?

Support Kevin Pliester by becoming a sponsor. Any amount is appreciated!