Create a plugin to free space in the functions.php file

If you were not clear about whether it is better to use a plugin or the functions.php file, how about creating your own plugin to incorporate your favorite functions?

It’s what I did some time ago and I have to say that I’m delighted. I simply created a small plugin in which I incorporated the functions that I use more often and so when I create a new site I just have to install it, customize some absolute URL – if it has it – and activate it so that everything works.

The advantage of using it instead of the functions.php file of your theme is mainly that you can update your theme without fear of losing your customizations because your functions are in your plugin, not in the theme.

Doing so is very simple …

Create plugin file

The first thing is to make the plugin. To do this, you create a new PHP file with your favorite code editor(Atom, Brackets, and Visual Studio Code are some free examples) and add the standard header so that WordPress recognizes it as a Plugin, something like this:

<?php
/*
Plugin Name: Functions
Plugin URI: https://wphelp.com/
Description: Plugin to free file space from functions <code>functions.php</code> and activate it(or not) .
Version: 1.0
Author: Hector Snyder
Author URI: https://hector.com
License: GPLv2 or later versions
*/Code language: HTML, XML (xml)

Add functions

From there you simply add your functions and when you are happy you save the changes. This would be an example with a couple of typical functions that I have used before:

<?php
/*
Plugin Name: Functions
Plugin URI: https://wphelp.com/
Description: Plugin to free file space from functions <code>functions.php</code> and activate it(or not) .
Version: 1.0
Author: Hector Snyder
Author URI: https://hector.com
License: GPLv2 or later versions
*/
// Customized logo on login
add_action("login_head", "my_login_head");
function my_login_head() {
echo "
<style>
body.login #login h1 a {
background: url('".get_bloginfo('template_url')."/images/whloginlogo.png') no-repeat scroll center top transparent;
height: 135px;
width: 135px;
}
</style>
";
}
// Customize access url logo
add_action( 'login_headerurl', 'my_custom_login_url' );
function my_custom_login_url() {
return 'https://wphelp.com';
}
//Change alt text from login logo
add_action("login_headertitle","my_custom_login_title");
function my_custom_login_title()
{
return 'Another site created by Hector Snyder';
}Code language: HTML, XML (xml)

Install and activate the plugin

Then you upload it to the ‘/plugins/’ folder of your WordPress installation and you can activate it like any other plugin, except that it does specific functions for you.

More than likely question: Do I have to keep using the functions.php file for something or not anymore?

Answer: Of course you do. What you have to take into account is the following:

  • If the function affects or is related to the theme(or themes) then it will go better in the functions.php file
  • If the function is related to general WordPress functionality then it will go better in your Plugin’s
  • Doing it this way (plugin on one hand and ‘functions.php’ on the other) is a much more logical and organized way of doing things

Note: Of course, once you activate the plugin you can(must) remove the functions that it incorporates from the file ‘functions.php’ of your theme and are already in the plugin.

How useful was this post?

Click on a smiley to rate it!

Average rating 3 / 5. Vote count: 2

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

About The Author

Leave a Comment

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

Scroll to Top
Skip to content