Execute PHP code in WordPress

Speaks many languages, but currently only uses PHP, JavaScript and WordPress (I consider it its own language, since it's a big sandbox).
Search for a command to run...

Speaks many languages, but currently only uses PHP, JavaScript and WordPress (I consider it its own language, since it's a big sandbox).
I was having a problem after upgrading the WordPress version while updating a PHP script for the contact form in WordPress website, this tutorial helped me a lot in executing PHP script on a web page.
Recently, a friend asked me to send a script to remove pre tags from a string, including content. He uses a script to calculate the reading time for the content, but doesn't want the pre-tags to be included in the scoring. So if you ever have a simil...

When there are problems with one's WordPress website, it happens that one has to create a user without having access to the database or the WordPress admin. Then it is useful to be able to create or log in a user using FTP. So here you will learn how...

Occasionally it happens that overlaps occur in a large table and the message Duplicate entry '0' for key 'PRIMARY' for query then gets the upper hand in the error logs. This error can be fixed quite easily with a short line of SQL. Fix duplicate entr...

WordPress is packed with useful features. It is easy to lose the overview. Here you will find a selection of functions that deal with arrays and objects. A small collection of useful functions that you can always use, if you know that they exist. wp_...

As a rule, searches are created with the MySQL LIKE. This usually works very well, as long as the database table is not too large. However, if you work with a large database (millions+ entries), then you quickly notice how long a search in it actuall...

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.
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
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.
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).