How to create a WordPress functions plugin and why it is better than using functions.php file

What is functions.php file

The WordPress functions.php file behaves like a WordPress Plugin, It is used to add new features and functionality to a WordPress site.

You can use it to call functions to disable e enable new theme features, both PHP and built-in WordPress, and to define your own functions.

Each WordPress theme contains a Functions.php file which executes only when in the currently activated theme’s directory. You can use this file to add or remove new nav menus, widgets, Google Analytics code etc. If you will change your theme, You will lose all custom functionality.

A WordPress custom functionality plugin is stored in wp-content/plugins directory. If you will change your theme, you will not lost any custom functionality.

WordPress functions plugin can have numerous blocks of code used for many different purposes such as remove dashboard widgets, unregister sidebars, customize login page and error messages etc.

How to create a WordPress functions plugin to avoid using functions.php file

Create a new folder and name it yoursitename-custom-functions. Open this folder, create a new file and save it as yoursitename-custom-functions.php file. Add the following information in this php file.

<?php
/*
Plugin Name: JustLearnWP.com custom functions
Plugin URI: http://justlearnwp.com
version: 1.0
Description: all the custom function goes in this file
Author: Tahir Taous
Text Domain: justlearnwp-custom-functions
*/

// Add all custom WordPress functions below this line.

save your changes. Now create a zip file foe this folder. Login to your WordPress dashboard, go to Plugins > Add New and upload your custom functionality plugin.

Now whenever you need to add a custom function, You can go to Plugins > Editor select yoursitename-custom-functions.php plugin file and add custom function code in this file.

READ  How To Remove Proudly Powered by WordPress Twenty Seventeen, Sixteen , Fifteen Theme Footer

For example, Following code will hide Screen Options button from WordPress dashboard.

// delete screen options button from Dashboard

 function remove_screen_options(){
    return false;
    }
add_filter(screen_options_show_screen, remove_screen_options);

If you want to chnage the length of excerpts. Add the following code in the custom functions plugin file.

// https://codex.wordpress.org/Function_Reference/the_excerpt
// Control Excerpt Length using Filters / By default, excerpt length is set to 55 words, change excerpt length to 20 words
function jlwp_custom_excerpt_length( $length ) {
    return 35;
}
add_filter( 'excerpt_length', 'jlwp_custom_excerpt_length', 999 );

That’s all. It is very simple and easy to create custom functionality plugin for your site.


You May Also Like

Leave a Reply

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