Modify pluggable template functions in the WordPress child theme

With Child Themes there are a lot of different options with which you can customize your parent theme. In this tutorial of the series on child themes, I explain how you can modify pluggable template functions in the WordPress child theme.

In the last part of the series on child themes, I explained how you can overwrite template files of the theme in a child theme . Today I show how the quite similar template functions of a theme can be adapted with a child theme.

What are template functions or template tags?

Template tags are PHP functions that output HTML markup directly with echo or return it with return . Template functions are used to display a certain element in the theme and are mostly specialized for this purpose.

Two template functions in Twenty Seventeen are for example:

  • twentyseventeen_posted_on ()
    Shows the date and author of a post
  • twentyseventeen_entry_footer ()
    Shows categories, tags and comments of the post

Excursus: I have already explained how you can create your own template functions for post details in your WordPress theme in the posts Show date and author in WordPress theme and Output WordPress categories in theme .

Template functions are reusable and can therefore be used in several places in the WordPress theme. Instead of having to repeat the same code several times in different templates, it is simply outsourced to the template function.

Difference between template functions and files

In contrast to template files, the HTML markup is not output when the complete file is loaded, but when the template function is called. In the content-single.php of a theme, e.g. B. the template day for date and author can be called:

 <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <header class="entry-header"> <?php theme_slug_posted_on(); // Unsere Template-Funktion. ?> <?php the_title( '<h1 class="entry-title">', '</h1>' ); ?> </header> ... </article>

It is often the developer's decision whether to use a function or a file, because template files are also reusable. Theme developers can create new template files and integrate them as template partials with get_template_part () in other templates .

A rough rule of thumb: With predominantly HTML markup such as the display of a post in a loop, a separate template file is used, with smaller elements such as post meta, pagination or footer credits a template function.

A great advantage of template functions is that variables can be passed very easily to them, so that the output can be controlled with various parameters.

Recognize pluggable functions in the parent theme

As the name suggests, template functions can be found in the functions.php of the theme. With almost all themes, however, not all functions are stuffed into this one file, but rather divided into several files for more clarity and structure and loaded with require at the end of functions.php .

The template functions can very often be found in /inc/template-tags.php of the parent theme. Otherwise an analysis of the functions.php and all files integrated from there helps.

template-tags.php

The PHP file usually contains a whole collection of template tags. In order to modify the template functions in the WordPress Child Theme, the entire file is not overwritten, but only the individual function.

So that the template function can be overwritten with a child theme, it must be a pluggable function, ie the developer of the parent theme must first have implemented the function so that it can be overwritten so that it can be modified.

Pluggable functions can be recognized by a wrapper with function_exists () around the function:

 if ( ! function_exists( 'twentyseventeen_posted_on' ) ) : /** * Prints HTML with meta information for the current post-date/time and author. */ function twentyseventeen_posted_on() { // Function Code. } endif;

The If-Clause is used to check whether the twentyseventeen_posted_on () function has already been defined. If no function with the same name already exists, the template function will be integrated in the parent theme.

How you can override template functions in the WordPress Child Theme

With this mechanism it is now possible to overwrite the template function.

After you have identified the template function that you want to adapt, you can copy the whole function from the parent theme without the If query and paste it into the functions.php of the child theme. The name of the function remains the same and must not be changed.

 /** * Prints HTML with meta information for the current post-date/time and author. */ function twentyseventeen_posted_on() { // Original-Code der Funktion, der nun angepasst werden kann. }

You can then change the template function in the child theme as you wish.

WordPress will now use your modified template function from the child theme. The function in the parent theme is no longer used because the function of the same name has already been defined in the child theme and the if-query with ! function_exists () returns false .

Attention! For this reason, only pluggable template functions can be adjusted in the WordPress child theme. PHP will immediately spit out an error if a normal function is defined a second time in the child theme. Function names must be unique.

How the functions.php works in the child theme

The modification of template tags is made possible by the functionality of child themes and loading the functions.php . In contrast to template files, the functions.php of the child theme does not overwrite that of the parent theme, but is loaded additionally.

Loading order of the theme files:

  1. WordPress loads the functions.php of the child theme
  2. WordPress loads the functions.php of the parent theme
  3. WordPress loads a template file based on the template hierarchy :
    • Does the template exist in the child theme? Then it will be loaded from there.
    • Template not available in child? Then it will be loaded by the parent theme.

While with overwritten template files only the code of the template from the child theme is integrated, WordPress loads the functions.php of both themes with each call.

All articles in the series on child themes