in WordPress, add_filter()
is a operate used to hook a customized operate or an present WordPress operate to a selected filter motion. Filters are one of many two varieties of Hooks, the opposite being Actions. They supply a method for capabilities to switch the information of different capabilities and are the cornerstone of WordPress’ plugin performance.
Right here’s the fundamental syntax of add_filter()
:
add_filter( string $tag, callable $function_to_add, int $precedence = 10, int $accepted_args = 1 )
As a content material administration system (CMS), WordPress was designed as some other platform… wether it’s a web page, a submit, or perhaps a customized submit you’ve a title and content material. However not all content material is proscribed to these two choices. One instance is the acronym library that I’ve developed on Martech Zone. An acronym has three components… the acronym itself, the definition that reveals what the acronym stands for, and an evidence of it.
I used to be capable of simply add a customized subject for the definition utilizing MetaBox, however that customized subject isn’t revealed all through the location. One technique of doing it could be to construct a customized template for the archive and single acronym web page the place I can extract the customized subject. Nevertheless, that requires fairly a bit of labor and must be achieved wherever I need that data – within the archive, the one submit, the excerpt, and the feed of the customized submit sort.
An alternate is to make use of your theme and prepend that data throughout the content material itself. On this case, I merely wish to prepend a brief sentence: The {title} is the acronym for {definition}. As a result of I additionally use the acronym library for codes, I additionally wish to modify the prepended textual content if the code is numeric: The {title} is the code for {definition}. Right here’s are examples:
0p is the acronym for Zero Occasion and 404 is the code for Not Discovered.
To do that, I can make the most of add_filter for the idea, excerpt, feed, and RSS to prepend the suitable textual content. Moreover, I examine to see if the acronym is numeric… by which case it’s doubtless a code. (I understand I may improve this additional, however for now it’s effective). Inside the capabilities.php file of my youngster theme, I merely add the next operate after which name the suitable filters to use it all through the location:
// Prepend textual content to the content material of 'acronym' posts
add_filter('the_content', 'prepend_text_to_acronym');
add_filter('the_excerpt', 'prepend_text_to_acronym');
add_filter('the_content_feed', 'prepend_text_to_acronym');
add_filter('the_excerpt_rss', 'prepend_text_to_acronym');
operate prepend_text_to_acronym($content material) {
international $submit;
// Examine if it is an 'acronym' submit
if($post->post_type == 'acronym') {
// Get the submit title and the 'acronym_definition' subject
$title = get_the_title($post->ID);
$definition = get_post_meta($post->ID, 'acronym_definition', true);
if (is_numeric($title)) {
$new_content = "<p>$title is the code for $definition.</p>";
} else {
$new_content = "<p>$title is the acronym for $definition.</p>";
}
// Prepend the brand new content material to the unique content material
$content material = $new_content . $content material;
}
return $content material;
}
Now, if you see my Acronym archive, you’ll see every of the entries have that sentence prepended within the excerpt. And it’s a standalone paragraph on the one posts web page.