WordPress Custom Functionality A custom functionality plugin allows you to separate your WordPress code between the presentation and functionality layers. It is a best practice often overlooked yet important to help future-proof your WordPress website.

Creating a plugin like this is easy to do and fast. It won’t slow down your workflow and once you have a good base coded you can easily reuse code in additional projects. In fact, a custom functionality plugin may actually speed your development process up. There are many reasons to separate you presentation code from your functionality code.

Why Create a Separate Functionality Plugin?

Many theme authors add a lot of mission critical functionality code like registering post types within a theme’s functions.php file. This isn’t thinking long term. What if in a few years the user wants to change themes? All that content is now hidden from the users’ view and unless they know how to re-register that CPT they are lost.

Site-Specific WordPress plugin is a specific place to add snippets that you find on the web that performs functions that are not theme related.

– wpbeginner

Beware of Application Themes…

Old-ball-and-chain-series-1-351760-m
Beware of Ball and Chain Themes

Many application themes do not separate theme code from plugin code. Suppose you or someone else developed an entire website using a popular directory theme (like you find within many themes on some of the larger theme marketplaces). The client decides they no longer like that theme and wants a change. Now what? You’re stuck trying to figure out how to move that data. That’s a big headache.

If you’re the developer you could get a call from that client that you haven’t heard from in years. So make sure before you buy that next theme that they separate plugin (application) logic from theme (presentation) logic and can verify the data the theme uses is portable from theme to theme.

Examples of What Goes in a Site-Specific WordPress Plugin

A good rule of thumb when thinking about what to place into a functionality plugin would be to ask yourself, if I change themes will it break? For instance, if you add a post redirect in your theme’s functions.php and switch themes the redirect is gone. This is an example of something that needs to go into a plugin. Here are a few more:

  • Shortcodes
  • Thumbnail support and sizes
  • Redirects (301, 302, etc)
  • Custom rewrite rules
  • Custom post types and taxonomies
  • Custom metaboxes and fields

What CAN you put in functions.php?

Don’t get the idea that the functions.php file is totally off limits. This is the place for theme-specific functions like navigation menus, sidebars and enqueuing scripts.

Registering navigation and sidebars belong in the theme because they rely on it for their placement. If you were to put these within a separate plugin and switch themes the theme would not contain the code to output them appropriately.

Enqueuing scripts gets a little into a gray area. If a script is solely used for a theme function it can go within the theme. But, if it’s purpose is outside the realms of a theme then put it in a separate plugin. For instance, if you have a script to filter posts like isotope this can go in the theme. This is because if you switch themes down the line this script will be still present in your source but rendered useless which increases load time. If a script is used to modify things like the WP login screen or admin area then this can go in a plugin because most likely you want this to carry over regardless of the theme.

Building a Custom Functionality Plugin

If you thought developing a separate plugin for functionality was difficult I’m here to prove to you it’s not. In fact, it’s actually quite easy. Here’s what our custom functionality plugin will do:

  1. Customize the WP login
  2. Add a simple dashboard widget with developer information
  3. Move some admin menu items
  4. Add a favicon for the admin area

Please note that all the following code is up on GitHub. Please feel free to fork and use for your needs. Let’s get started:

1. Create the plugin directory structure

I like to structure functionality plugins with the following structure. Feel free to modify this to your liking. This is just my preference:

  • company-name-plugin
    • includes
    • assets
      • js
      • css
      • img

wordimpress-functionality-plugin-structure

My rationale with this structure is that it you can easily place your assets within their specific subdirectory. Includes is reserved for helper classes for registering post types or custom metaboxes and fields.

2. Create the main plugin file

Within the base directory create a file called company-name-plugin.php and add the following code customized to suit your needs:

Congrats, you have created a plugin. Now go and activate it:

wordimpress-functionality-plugin-activate

Got it activated? Good. Now let’s add some functionality.

3. Customize the WP Login Screen

Customizing the admin login is a nice touch to add for your clients. It’s simple to do and our plugin here is the perfect place to put the code.  First, create a new css file under assets/css called admin.css. Next, add the following code to our main php file:

This code is essentially adding our CSS file to the admin head and also placing a favicon. The following is some CSS to customize the login screen which you can add to our previous created admin.css file:

Refresh your admin screen and you should now see a WordImpress branded login. You can easily swap out the images here for your own background and logo.

wordimpress-branded-login

Don’t forget to add a favicon. You can either use a PNG or ICO file for your favicon. Simply place it in the image directory and you should see it display on your admin pages.

 4. Create a Better Dashboard Experience

The WordPress Admin dashboard likes to include a few widgets I find unnecessary. I like to remove some of these widgets and also include my own helper widget that includes information about the site and how to get support. Here’s the code I use to do that:

Now when a users visits the dashboard they are met with a much cleaner interface and also have our helpful widget. This widget can be extended to show videos, latest posts, tweets, and even forms to submit bug reports or support requests.

wordimpress-functionality-cleaner-dashboard

 5. Remove and/or Rearrange Admin Menu Links

Depending on the site you are building you may not want to display admin menu links such as “Posts” or “Comments”. These can easily be hidden from view with just a few lines of code. You may also rearrange them in an order that may make more sense for your use case. Here’s how we do it:

Now suppose we aren’t using posts? Using remove_menu_page we can easily get rid of the items we don’t want:

Now when you refresh the admin screen you should see posts are removed. Simply uncomment some of the other functions to remove other links. As you can see it’s really easy to remove them.

6. Columns Clean Up

There are a number of columns that display which I don’t like to see such as author and comments. Removing default columns only takes a few lines of code as well:

 7. Tame WordPress SEO’s Metabox

WordPress SEO is an excellent plugin. However, I don’t like how it sets it’s own metabox priority at the highest level. This means it displays above all others in the edit page view. Let’s put a wrangle on this and knock it down a few pegs:

Special thanks to Chris over at Revelation Concept for this snippet.

8. Registering Post Types and Taxonomies

As previously mentioned, it is very important to register post types and taxonomies within a custom functionality plugin. I like to include two classes that make this extremely easy: WordPress Custom Post Types and WordPress Custom Taxonomies. Within the GitHub repo I have included these as submodules.

9. Custom Metaboxes and Fields

I tend to sway between using Custom Metaboxes and Fields and Advanced Custom Fields. They both have their pluses and minuses. CMB is lightweight yet less powerful. ACF is heavier but oh so powerful. It depends on the project but it’s always good to have CMB at your disposal. That’s why I’ve also included it as a submodule.

Check it Out on GitHub

You can find the code above all packed and ready to go for your use on GitHub.

wordpress-custom-functionality-on-github

Please feel free to fork, submit pull request and use at your disposal. Have any questions or comments? Be sure to leave them below!

 

Devin Walker is a San Diego-based WordPress Developer and enthusiast. He is the author of several popular and highly-rated WordPress themes and plugins. In his free time he enjoys playing Golf and traveling.

Follow Devin: