in WordPress, add_filter()
is a operate used to hook a customized operate or an current WordPress operate to a selected filter motion. Filters are one of many two varieties of Hooks, the opposite being Actions. They supply a approach for features to change the info of different features and are the cornerstone of WordPress’ plugin performance.
Right here’s the essential 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 publish, or perhaps a customized publish you could have a title and content material. However not all content material is restricted to these two choices. One instance is the acronym library that I’ve developed on Martech Zone. An acronym has three parts… the acronym itself, the definition that exhibits what the acronym stands for, and an evidence of it.
I used to be in a position to simply add a customized area for the definition utilizing MetaBox, however that customized area isn’t printed all through the positioning. One technique of doing it will be to construct a customized template for the archive and single acronym web page the place I can extract the customized area. Nevertheless, that requires fairly a bit of labor and needs to be finished wherever I would like that info – within the archive, the one publish, the excerpt, and the feed of the customized publish sort.
Another is to make use of your theme and prepend that info inside 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 Social gathering 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 verify to see if the acronym is numeric… through which case it’s probably a code. (I understand I might improve this additional, however for now it’s tremendous). Inside the features.php file of my baby theme, I merely add the next operate after which name the suitable filters to use it all through the positioning:
// 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) {
world $publish;
// Test if it is an 'acronym' publish
if($post->post_type == 'acronym') {
// Get the publish title and the 'acronym_definition' area
$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.