Documentation

Didn't find anything? Ask your questions in the forum.
Documentation Professionals How to execute PHP code on your WordPress website

How to execute PHP code on your WordPress website

For most functions here in the documentation, PHP must be used to achieve the desired results. Since WordPress does not offer any possibility to do this by itself, you either have to customize your WordPress theme or use a plugin for it. 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.

Download from WordPress.org


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 the 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
// ...

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


Useful links:

Was this post helpful?