Warning: Trying to access array offset on value of type null in /home/167840.cloudwaysapps.com/bsrvzwpdrx/public_html/wp-content/themes/salma/inc/header/header-default.php on line 3

Writing and Activating a Function in WordPress

enquerer > Blog > Uncategorized > Writing and Activating a Function in WordPress
Writing-functions-Wordpress

When it comes to developing plugins and themes for WordPress, the developers need plenty of concentration and moreover, some extra skills that can make the process of development easy and stacked. One of these skills that you need is in writing functions effectively.

Writing functions can create huge confusions. Not always does it render as we think it will. Sometimes, they may not work properly, or leave you frustrated that you’ll quit working on it. They can also leave you in chaos regarding the action/filter hooks, where you become unsure of how to get going with your function.

Here in this article, let’s discuss some desperation-causing issues you’ll face while writing your functions. We can see how to bring the functions to a proper structure, and the general methods for activating the function you created. The article will also include some worthy practice tips which will help you ease your development of functions. Now, it’s time to move down!

What is Functions File in WordPress?

The functions file is a WordPress file that comes with every WordPress theme. It is also known as functions.php. The file allows the theme’s developer to define the functions and features of the theme. The file itself acts as a WordPress plugin where you can add your custom code snippets in WordPress.

You can google to find such code snippets that you can add to the functions.php file. They will include instructions on how to add these snippets into your theme’s function file. You should note that a theme’s function file will work for that theme alone. If you need any snippet to work in any other theme, you can copy and paste the snippet to that theme’s function.php file.

What You Need To Start

For the rest of this article, it would be a plus if you have a function in hand that you are already working on. That’ll help you get more practice on the same. Here’s the complete list of requirements:

– A code editor to create the code snippets

– A WordPress for developers installed on your website or localhost

– A functions.php file that is included in your theme’s files

If you don’t have a working functions file, you can learn more by following the rest of the article.

Where To Put Your Function?

Next, it’s time to decide where to put the code for your function. You can either put it in your theme’s functions file or in a plugin.

The choice depends on your purpose. If the function is based on the design or display of content, you can choose to put it in your theme. Otherwise, if it is a ‘functional’ function (as its name says!), you can put it in a plugin. While option one is dependent on themes and works only for the applied theme, option two makes the function universal for all themes.

Writing The Function

Now that you are ready with your functions.php file (or the plugin file) open, let’s start looking into the parts of the writing.

Adding Comments to the Functions File

It’s easy to get started directly and write some codes. But preferably, adding comments before coding is the best thing to do. This helps you keep a note of what the function is when you go back after sometime. You may not be visiting a code for years and have forgotten what the code is and how it looks like. Seriously, comments help in such cases. They also help other contributors when they open the file for any tweaks.

Here, we can see how to add some Google fonts to a theme with a function. As it is a display related function, let’s put it in the theme’s functions.php. Here’s an example for the first comment.

/************************************************************************

wpshop_add_google_fonts – Register Google Fonts

************************************************************************/

By giving the above format to codes, you can make them easily visible while you go back.

Opening a Function

Now, let’s move on to writing the function. A function starts with the term function followed by a function name. It is important to add a prefix to every function, as there may be more than one function of the same functionality inside the code which may belong to the theme or other third-party plugins. Adding a prefix helps avoid confusions when you need to edit them in the future. You can choose your name, initials, or the website name as the prefix.

The text with function and function name is followed by an open and close brackets and then by curly braces inside which your code goes. Following is an illustration of a function.

function wpshop_add_google_fonts()

{

your code goes here

}

Populating a Function

Everything you write as the code to run the function is written inside the curly braces. The conditional tags are included in this code. Here is a sample code which is written for the google fonts function.

wp_register_style( ‘googleFonts’, ‘https://fonts.googleapis.com/css?family=Assistant|Oswald:300’);    

wp_enqueue_style( ‘googleFonts’);

In this article, we aren’t teaching you to code any specific functions, as it depends on your purpose of making the code. However, there are plenty of online resources where you can master a variety of functions.

Now, see the complete function:

/************************************************************************

wpshop_add_google_fonts – register google fonts

************************************************************************/

function wpshop_add_google_fonts {

 wp_register_style( ‘googleFonts’, ‘https://fonts.googleapis.com/css?family=Assistant|Oswald:300’);    

 wp_enqueue_style( ‘googleFonts’);

}

Once you have finished writing the code, you will wish to visit your website to see if the code worked. Don’t wonder why there isn’t any change. It’s just because the function is not yet activated.

Activating the Function

A function can be made functional only when it is activated in any way. Activation refers to instructing WordPress when to fire the function. This is done in three different methods:

(1) By attaching the function to an action hook.

(2) By attaching it to a filter hook.

(3) By Coding it directly into the theme’s template file.

Let’s go through each of these methods.

A function is useless unless you activate it in some way, telling WordPress when to fire the function. You have three options for this:

  • Code the function directly into your theme template file (or another plugin file).
  • Attach it to an action hook.
  • Attach it to a filter hook.

Let’s look at each of these.

Attaching the Function to an Action Hook

An action hook is a code that lets you run the code attached to it each and every time WordPress encounters the action hook. WordPress already provides some hooks for your use. You can either use them or develop your own action hook.

In our case, as it is a fonts function, we can use the ‘wp_enqueue_scripts’ hook provided by WordPress. To fire the function, this hook should be attached outside the braces.

add_action( ‘wp_enqueue_scripts’, ‘wpshop_add_google_fonts’ );

That will make the complete function look as below.

/************************************************************************

wpshop_add_google_fonts – Register Google fonts

************************************************************************/

function wpshop_add_google_fonts {

 wp_register_style( ‘googleFonts’, ‘https://fonts.googleapis.com/css?family=Assistant|Oswald:300’);    

 wp_enqueue_style( ‘googleFonts’);

}

add_action( ‘wp_enqueue_scripts’, ‘wpshop_add_google_fonts’ );

Attaching the Function to a Filter Hook

The filter hook is one another type of hook, which is already wrapped around certain content or code when it is developed. Your function will override this code. Filter hooks are handy when you need to code something into your theme template file and then override it in the future. WordPress itself offers you with a good range of filter hooks for various functions.

The apply_filters() function is what we use for adding a filter hook to the template file. The hook is added with the filter name as the first parameter and the default content as the second parameter.

echo apply_filters( ‘wpshop_filter_hook’, ‘<h3>Latest Posts</h3>’ );

Following is how it looks when you hook a function attached to it:

/*****************************************************************************

wpshop_heading – override title of latest posts section

*****************************************************************************/

function wpshop_heading() {

       <h3>Recent Posts</h3>

}

add_filter ( ‘wpshop_filter_hook’, ‘wpshop_heading’ );

Coding  Your Function into the Theme Template File

This option of activating the function is available only when you are working with your own theme. If you need to activate the function in a third-party theme, you have to create a child theme, make a duplicate of the theme template file (which WordPress will use from then on) in the child theme, and then add the function to the duplicate template file. When the theme provides future updates, the theme template file will have changes, and these will not be reflected in the child’s theme template file.

Assuming that you are working on your own theme file, let’s see how this method is applied. We also assume that you have the snippet of the code you need to repeat in different template files. Now, you can either create an include file and call it in those files, or you can use the functions file to create a function. As the snippet of code with you is efficient enough, you can choose the second option, i.e, to use the functions file to create a function.

Suppose that your function echoes out the date. Let’s have a look.

/*****************************************************************************

wpshop_posted_on – add dates on single posts

*****************************************************************************/

function wpshopadmin_posted_on() {

       <section class=”entry-meta”>

           <?php echo get_the_date(); ?>

       </section>

}

After that, you can add the code to your theme template file, where you want the code to run.

wpshopadmin_posted_on();

Wrapping Up

A clear knowledge of how functions work and how to write and activate them will give great mileage to your WordPress development. It becomes essentiality while coming to coding WordPress themes and plugins. We really hope this article helps you to get more familiar with functions, and how to create and activate them.

Have A Look At Our Top Rated WooCommerce Plugins ThemeHigh


Warning: Trying to access array offset on value of type null in /home/167840.cloudwaysapps.com/bsrvzwpdrx/public_html/wp-content/themes/salma/template-parts/about_author.php on line 5

Warning: Trying to access array offset on value of type null in /home/167840.cloudwaysapps.com/bsrvzwpdrx/public_html/wp-content/themes/salma/single.php on line 46

Leave a Comment


Warning: Trying to access array offset on value of type null in /home/167840.cloudwaysapps.com/bsrvzwpdrx/public_html/wp-content/themes/salma/single.php on line 62

Warning: Trying to access array offset on value of type null in /home/167840.cloudwaysapps.com/bsrvzwpdrx/public_html/wp-content/themes/salma/inc/template-functions.php on line 133