How to Add Custom PHP Code in WordPress

WordPress is an exceptional content management system that operates seamlessly out of the box. It boasts thousands of themes and plugins to help you construct any website.

However, there will be instances when you must customize the theme or plugin to suit your specific needs. For example, you may need to add additional functionality to your website or adjust the theme without overhauling the entire design.

Using PHP code is essential when customizing WordPress themes and plugins. Instead of relying solely on plugins, consider enhancing WordPress with new functionality through PHP.

However, avoiding making direct code adjustments in the parent theme’s files, especially in the functions.php file, is crucial. This approach may work initially but there are more effective solutions. Any changes made this way will be lost when you update your theme or plugin.

Consequently, avoiding updates can have severe repercussions for your website. By neglecting updates, you risk missing critical security or bug fixes typically included in theme or plugin updates. Moreover, upgrading your WordPress version may lead to the loss of your changes.

Add Custom PHP Code in WordPress

There are 3 methods of adding PHP code in WordPress:

  1. Adding PHP code with Theme Editor
  2. To edit PHP files manually by Using the FTP client
  3. Convert PHP into WordPress shortcodes
  4. By Adding sitewide code snippets with a plugin

It is recommended that you always back up your site before adding code to WordPress to avoid potential issues. Improper PHP syntax could trigger WordPress recovery mode.

Syscality


  • Adding PHP code with Theme Editor

In WordPress, the Theme Editor tool lets you edit your theme files directly from the dashboard. Using a child theme to retain modifications when updating the parent theme is crucial.

To access the theme editor, navigate to Appearance >> Theme Editor, and you’ll find a simple text editor on the left and a list of files on the right.

Theme Editor

The Theme Files list provides direct access to your theme’s customizable files. Beneath each entry, you’ll also see its full filename. Most of the options here will be PHP files.

You can switch which theme you edit using the “Select theme to edit” drop-down menu.

Additionally, you can look up documentation for specific functions using the “Documentation” drop-down menu beneath the editor.

Get Option

When you look up a function, you will be directed to its WordPress Development Codex page if it’s available. Please note that if you’re editing a custom theme, you may not find documentation for specific functions.

After making the desired changes, you can press “Update File” to save them. To add code snippets to your site, you must edit the functions.php file.


  • To edit PHP files manually by Using the FTP client

The WordPress Theme Editor is extremely limited. If you’re accustomed to using a full code editor, you’re missing out on a lot of functionality when making changes from your dashboard.

A superior approach is to connect to your website’s files via FTP and utilize your preferred text editor for updates. To accomplish this, you’ll need an FTP client like FileZilla. You must locate your website’s login details if you’re new to using an FTP client.

Remember, your FTP credentials differ from the ones used to log into WordPress. Typically, your web host will create an FTP account for you upon plan signup, and you can find the details in your hosting control panel.

FTP Account

To connect to your website through FTP, you must have the host address, your FTP username and password, and specific port details, if available. Once you obtain this information, connect to your website using FileZilla or a different FTP client.

After gaining access to your website, locate the WordPress root folder containing all your site’s files. Typically, this folder is named public or public_html or bears the name of your website.

Root Folder

Once you can access the root folder, you can modify your WordPress files and add custom PHP code. You are strongly advised against modifying files if uncertain of their function.

A solid understanding of the root directory is structure is essential to edit files. Within the main folder, you will find site-wide configuration files, such as the .htaccess file, which you can modify to incorporate advanced functionality into WordPress.

You can locate the theme and plugin files inside the wp-content folder. Navigate to wp-content/plugins or wp-content/themes to find individual folders for each theme and plugin. Upon opening a theme’s folder, you can edit all the files available through the WordPress Theme Editor tool.

Right-click on a file and select the View/Edit option to edit it. This action will open the file using your local default text editor.

Default Text Editor

After making changes to a file, be sure to save it. Your FTP client will then promptly upload the new version to your server.


  • Convert PHP into WordPress shortcodes

Plugins are a great way to solve specific problems in WordPress. Several plugins allow you to add custom code to your WordPress files without directly editing them. One option is to convert your PHP code snippets into shortcodes,

which can be easily used in the WordPress editor or widget areas. This is helpful if you want to add PHP to a specific post or page on your site. You can achieve this by using the free “Insert PHP Code Snippet” plugin.

This plugin allows you to create custom shortcodes for PHP code snippets. You can add these snippets using the plugin, and it will generate shortcodes that you can use anywhere on your website to execute those functions.

After installing and activating the plugin, you will see a new tab in the dashboard labeled “XYZ PHP Code.” Select that tab and navigate to PHPCode Snippets >> Add New PHP Code Snippet.

PHP Code Snippets

The plugin will prompt you to provide a “name” for your snippet, which should describe its purpose, and to add its code within the PHP code field.

PHP Code

Note that Insert PHP Code Snippet does not validate your add code. If the code does not work, you must troubleshoot it independently.

Once you add a code snippet using the plugin, it will appear under the PHP Code Snippets tab. You have options for pausing individual scripts, editing, deleting, and previewing them.

Snippet Short Code

The snippet shortcode is available under the “Snippet Short Code” column. After reviewing the code to ensure it works, you can add the shortcode anywhere on your site.


  • By Adding sitewide code snippets with a plugin

To easily incorporate PHP into your WordPress website, utilize the free Code Snippets plugin.

Access the “Add New” section under Code Snippets to seamlessly integrate general code snippets into your site. When adding your PHP code snippet, assert your choice to run it universally or exclusively on your site’s front or back end.

Edit Snippet

The plugin integrates PHP code into WordPress and facilitates the addition of custom JavaScript, CSS, or HTML.

Conclusion

Editing and adding PHP in WordPress is a straightforward process. If you’re comfortable with code or even just copying and pasting it, you can easily add snippets that will significantly enhance your site’s functionality.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *