Adding Functions to a Plugin vs Theme
While the easiest and most common way of adding simple snippets to extend a WordPress website’s functionality is pasting them into the functions.php file of the active theme, any function can be added by creating and installing a simple plugin. There are two main advantage to this. First, it will clean up your functions.php, especially if you have a lot of code in it. Second, it will allow you to use your functions with any theme. If you frequently change themes, you’d need to copy your functions.php over to the new theme to keep the same functionality. By using a plugin, you eliminate the risk of forgetting and losing potentially very important functions.
To add a function to your WordPress site using a plugin instead of pasting it into the functions.php file, follow these steps:
Create a PHP file to hold your functions
This is pretty easy, even if you aren’t a PHP coder. Simply open up any code/text editor, create a new file, and save it as a .php file. For best naming practices, give it a unique file name, such as my_custom_functions_plugin.php. This file will be your plugin’s primary file so you need to inform WordPress of this by placing the code below at the very top of the file. Then, add your code and save!
<?php /** * Plugin Name: Name Of The Plugin * Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates * Description: A brief description of the Plugin. * Version: The Plugin's Version Number, e.g.: 1.0 * Author: Name Of The Plugin Author * Author URI: http://URI_Of_The_Plugin_Author * License: A "Slug" license name e.g. GPL2 */
Aside from the opening php tag (<?php), the only line from this code that is required is the Plugin Name line. All others can be removed if you would like. You can read more about this code on the WordPress Codex for writing plugins. Once you’ve got all your functions saved to a PHP file (i.e. my_custom_functions_plugin.php), just upload it to WordPress. If you’re uploading the using the WordPress plugin uploader, you need to zip the file first, even if it’s a single file without a folder.
That’s it! Drop any questions or comments below.
Alvaro Guevara says
gajju fauzdar says
How to remove post content from only archive page ?