• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
Appfinite

Appfinite

Premium WordPress Themes for The Genesis Framework

  • Themes
  • Blog
  • Tutorials and Resources
  • Forums
  • Contact Us

Customization

  • An Introduction to Filters
  • An Introduction to Hooks
  • Filter Reference
  • Hook Reference
  • Shortcode Reference
  • Theme Customization Basic Skills
  • Visual Markup Guide
Shortcode Reference
  • Footer Shortcode Reference
  • Post Shortcode Reference
  • Shortcodes Overview

General

  • An Introduction to Genesis Child Themes
  • Getting Started with WordPress and the Genesis Framework

Genesis Code Snippets

Accessibility
  • Enable Accessibility Features
Admin Management
  • Force the Genesis Layout Settings
  • Remove the Genesis In-Post SEO Settings
  • Remove the Genesis Layout Settings
  • Remove the Genesis Menu Link
  • Remove the Genesis SEO Settings Menu Link
  • Unregister the Genesis Layout Settings
  • Unregister the Genesis Widgets
Author Box
  • Enable the Author Box on Archive Pages
  • Enable the Author Box on Single Posts
  • Modify the Author Box Title
  • Modify the Gravatar Size
  • Remove the Author Box on Single Posts
Block Editor
  • Disable Responsive Typography in Genesis Blocks
  • Enable Block-Based Widget Editor
Breadcrumbs
  • Modify the Breadcrumbs Display
  • Modify the Breadcrumbs Home Link
  • Reposition the Breadcrumbs
Comments
  • Add A Comment Policy Box
  • Modify the Author Says Text
  • Modify the Comments Headline Text
  • Modify the Comments Link Text
  • Modify the Gravatar Size
  • Modify the Speak Your Mind Text
  • Modify the Submit Button Text
  • Modify the Trackbacks Headline Text
  • Remove the Allowed Tags Text
Custom Feeds
  • Remove Custom RSS Feed Redirect
Entry Content (HTML5)
  • Add Post Navigation
  • Remove Entry Content
  • Remove Post Image
  • Remove Post Navigation
  • Remove Post Permalink
Entry Footer (HTML5)
  • Customize the Entry Footer
  • Remove Entry Footer Markup
  • Remove Entry Meta
Entry Header (HTML5)
  • Customize the Entry Header
  • Remove Entry Header Markup
  • Remove Entry Meta
  • Remove Entry Title
  • Remove Post Format Image
Footer
  • Customize the Credits Text
  • Customize the Return to Top of Page Text
  • Customize the Site Footer
  • Reposition the Site Footer
Google AdSense
  • Remove AdSense ID Setting in Customizer
  • Remove AdSense Metabox from Theme Settings Screen
  • Remove AdSense Metabox on Posts and Pages
Head Section
  • Add Custom Viewport Meta Tag
  • Add Viewport Meta Tag
Header
  • Modify the Header URL
  • Remove the Header Widget Area
  • Remove the Site Description
  • Remove the Site Title
HTML5
  • Enable HTML5 Markup
Images
  • Add Featured Images
  • Display a Custom Gravatar
Miscellaneous
  • Add an After Entry Widget Area
  • Add Custom Body Class
  • Show Content Above Posts on Blog Page
Navigation Menus
  • Add Navigation Extras
  • Reposition the Primary Navigation Menu
  • Reposition the Secondary Navigation Menu
  • Unregister the Primary Navigation Menu
  • Unregister the Primary/Secondary Navigation Menus
  • Unregister the Secondary Navigation Menu
Post Excerpts
  • Modify the Content Limit Read More Link
  • Modify the Content Read More Link
  • Modify the Length of Post Excerpts
Post Formats
  • Add Post Format Images
  • Add Post Formats
Post Info (XHTML)
  • Customize the Post Info Function
  • Remove the Post Info Function
Post Meta (XHTML)
  • Customize the Post Meta Function
  • Remove the Post Meta Function
Post Navigation
  • Customize the Next/Newer Page Link
  • Customize the Previous/Older Page Link
Scripts
  • Disable Superfish Script
  • Enable Superfish Script
Search Form
  • Customize the Search Form Input Box
  • Customize the Search Form Input Button
  • Customize the Search Form Label
Sidebars
  • Unregister Primary Sidebar
  • Unregister Secondary Sidebar
Structural Wraps
  • Add Structural Wraps
  • Home
  • Docs
  • Customization
  • An Introduction to Filters

An Introduction to Filters

Table of Contents
  • How do Filters Work and How do I Create One?
  • That's It!
  • Pre-Applied Genesis Filters

One of the most valuable features integrated into Genesis is the ease of customization. There are numerous methods to modify and extend your website’s functionality. Although hooks and actions are one way to achieve this, sometimes it is not the most efficient approach to add or remove actions and supply new functions just to modify an existing function’s output. Filters come into play in these situations. Similar to actions, filters can be used to adjust your child themes without modifying the core Genesis Framework files.

It is important to be acquainted with hooks and actions before diving into filters.

What is a Filter? Filters can be viewed as a specialized type of hooks that enable you to modify a function during its execution. You can consider it a hook within a function, where you can input your own directives for what to alter. Alongside the standard hooks, the Genesis Framework includes its unique set of filters, in addition to those that come with every WordPress installation.

What Do Filters Look Like? Filters consist of several components. To utilize a filter, you initially need a function. The filter is positioned within that function, and a callback function (the function that changes the original) can be applied to the filter. The different components appear as follows:

add_action( 'hook', 'my_function' ); // This code executes the original function at the 'hook' hook.

function my_function() { // This is the original function that you want to modify.
	echo apply_filters('filter_hook', 'Hello world!'); // This is the apply_filters() function within the original function that creates the filter.
}

add_filter( 'filter_hook', 'callback_function'); // This code adds your callback function to the filter created with apply_filters() in the original function.

function callback_function() { // This code is your callback function that actually does the modification of the original function.
	return 'This is my new text!';
}

How do Filters Work and How do I Create One? #

Let’s examine the entire process of a filter’s operation. Assume you have the following code in your child theme:

add_action( 'hook', 'my_function' );

function my_function() {
	echo 'Hello world!';
}

In the aforementioned code snippet, the ‘my_function’ function displays the text “Hello world!” at the ‘hook’ position. But what if you wish to modify the text to “This is my new text!”? You will need to implement a filter within the function. Filters are employed using apply_filters(). This should be placed in your function, replacing the original value you want to change. The apply_filters() function appears as follows:

apply_filters( $tag, $value, $var1, $var2, ... );

apply_filters(): To utilize a filter in a function, the function must incorporate apply_filters(). Consider this as a “hook” within the function you aim to filter. It comprises these parameters:

$tag: the “name” of the callback function you plan to implement. You will need to create the callback function later. $value: the existing data you want to modify. $var1, $var2…: any number of extra parameters that can be passed into your filter function to adjust the $value. These are optional. Certain functions integrated into Genesis and WordPress already contain apply_filters(). To leverage these filters, all you need to do is compose the callback function. Refer to the Pre-Applied Genesis Filters section below for more details.

Thus, when you include apply_filters() in your original function, it appears as follows:

add_action( 'hook', 'my_function' );

function my_function() {
	echo apply_filters('filter_hook', 'Hello world!', 'var 1', 'var 2');
}

In the code snippet above, our filter is implemented using apply_filters(), where the filter tag is ‘filter_hook’, the value to be altered is ‘Hello world!’, and any additional variables that can be utilized are ‘var 1’ and ‘var 2’. The variables can also be dynamic values denoted by some $var defined in your original function.

If you were to save your files now and refresh the page, what would be different? Nothing! By employing apply_filters(), you’ve just established the hook that your callback function will eventually use, but you haven’t created the callback function itself yet.

Now, let’s develop a callback function. In your functions.php file, you’ll need to compose your callback function and then attach it to the hook you’ve created using add_filter(). It functions similarly to add_action() with hooks. It might resemble the following:

add_filter( 'filter_hook', 'callback_function');

function callback_function($value, $var1) {
	if( $var1 == 'var 1' ) {
		$value = 'This is my new text!';
	} 
	return $value;
}

In the code snippet above, the add_filter() function targets the $tag we established with apply_filters(), called ‘filter_hook’. It then incorporates the callback function ‘callback_function’, defined right below it.

The callback function comprises the code that modifies the original function’s $value. In this example, it accepts the $value and $var1 from the original function. It verifies whether $var1 is equal to ‘var 1’ (it is) and updates $value to “This is my new text!” Subsequently, it returns $value to the original function.

Keep in mind that the callback function should always return a value rather than echo it. Echoing a value sends it to the browser, preventing any other filters or processes within the original function from utilizing the modified data from your callback function. Returning a value ensures that the data can be passed on to other filters or further modified down the line.

That’s It! #

You’ve successfully employed apply_filters() to create a “function hook” in your original function, composed a callback function, and integrated that function into the hook using add_filter(). Once you save all your files and refresh your site, the modified text should be displayed on the page!

Pre-Applied Genesis Filters #

The Genesis Framework includes several functions where apply_filters() is already integrated, with a $tag predefined for each. These instances are offered for your convenience, so if you wish to modify some output, all you need to do is write your callback function and incorporate it using add_filter() and the supplied $tag. It’s a fantastic shortcut for adjusting output on your website without modifying any of the Genesis core files.

Refer to the Filter Reference for a list of filters and tags that are built into Genesis.

An Introduction to Hooks
Table of Contents
  • How do Filters Work and How do I Create One?
  • That's It!
  • Pre-Applied Genesis Filters
  • Buy Genesis!
  • Shopping Cart
  • Themes
  • My Account
  • Support Forums
  • Tutorials and Resources
  • Privacy Policy
  • Contact Us
  • Follow Us on Twitter

Copyright © 2023 · Appfinite · Built With The Genesis Framework