A while back I found this interesting article over at DoItwithWP about using a functionality plugin versus adding custom functions to your theme’s functions.php file. As a developer who loves to customize like crazy, uses pre-made themes on occasion, and changes my sites up regularly with new themes… I really like the idea of this! I see it as a huge time saver, and I’m all on board.
So what’s the point? Well, let’s say you’ve added a bunch of functions to your current theme’s functions.php file and made your site completely customized to your liking. Now let’s say you switch themes… it’s all lost.
For example, I have a custom function enabled on this site that adds an alert box using a shortcode.
If I had placed this function in my theme file, when I switched themes all the user would see in any post using the shortcode would be the following, which isn’t very pretty.
[xmasalert]] For demonstration purposes, I have added the alert box here. This is the output of the custom function I just mentioned… this is the alert box. [[/xmasalert]
Yuck, nobody wants broken code for the whole world to see on their site. If I had used my alertbox in lots of pages, just think how long it would take to find them all and manually delete the code?
You see, when you add functions to your active theme’s functions.php file, it only works with that theme. So when you go to switch, you have to go back and add all of your custom functions to your new theme’s functions file.
But if you’ve created and utilized your very own custom functionality plugin, all of your customizations will still be there when you switch themes. No matter if you switch themes every week… your custom functions will always work unless you deactivate your plugin in the plugins page.
So, let’s make a plugin! First open your text editor and create a new file named functionality-plugin.php. You will need to paste the following at the top, changing the information as needed:
<?php /* Plugin Name: Functionality Plugin Plugin URI: https://christmaswebmaster.com/ Description: Take your custom functions with you! Author: Jinglebelle Version: 1.0 Author URI: https://christmaswebmaster.com License: GPL2 */
You’ve just created your plugin, but it does nothing until you add functions to it. So go grab your custom functions and paste them below the plugin header. Here’s what mine looks like so far:
<?php /* Plugin Name: Functionality Plugin Plugin URI: https://christmaswebmaster.com/ Description: Take your custom functions with you! Author: Jinglebelle Version: 1.0 Author URI: https://christmaswebmaster.com License: GPL2 */ //Custom Gravatar function newgravatar ($avatar_defaults) { $myavatar = get_bloginfo('template_directory') . '/images/guest-gravatar.png'; $avatar_defaults[$myavatar] = "Guest"; return $avatar_defaults; } add_filter( 'avatar_defaults', 'newgravatar' ); //Correct my spelling function CorrectIt($content) { $content = str_ireplace("chrsitmas","Christmas", $content); $content = str_ireplace("wordpress","WordPress", $content); $content = str_ireplace("dosen't","doesn't", $content); return $content; } add_filter( 'the_title', 'CorrectIt',1 ); add_filter( 'the_content', 'CorrectIt',1 ); //xmasalret box function xmasalertbox($atts, $content=null, $code="") { $return = '<div class="xmasalert">'; $return .= $content; $return .= '</div>'; return $return; } add_shortcode('xmasalert' , 'xmasalertbox' );
Now you need to create the folder ‘functionality-plugin” in your wp-content/plugins directory and place your functionality-plugin.php file in it.
Then, in your WordPress admin dashboard you will need to activate your plugin. That’s it.
I want to point out that there will still be times you would want to use your active theme’s functions.php file. As a general rule of thumb add things to your functionality plugin that you would want to use no matter the theme in use on your site. Add things to your theme’s functions.php file that are specific only to the theme in use – or things you wouldn’t want to use in another theme.
Here’s another great article I found recently on the subject: Functionality: Plugins VS Themes
I hope you find this post useful! I would love to hear from you in the comments…
Pingback: How to change WordPress default email ‘send from’ settings | Christmas Webmaster
Hi Monica,
Glad to see you jumping on the bandwagon, doing it the right way and teaching others about it! Thanks for the link back to the original article. Always appreciated.
Hiya Dave! Thanks for clicking on over 😉
Always happy to help spread the word!