Monday, November 20, 2023
HomeMobile MarketingWordPress: How To Prepend Textual content With A Customized Subject To Your...

WordPress: How To Prepend Textual content With A Customized Subject To Your Customized Submit Kind Content material


in WordPress, add_filter() is a operate used to hook a customized operate or an current WordPress operate to a particular filter motion. Filters are one of many two sorts of Hooks, the opposite being Actions. They supply a manner for capabilities to change 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 every other platform… wether it’s a web page, a put up, or perhaps a customized put up you’ve gotten 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 reveals what the acronym stands for, and a proof 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 revealed 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 carried out wherever I need that info – within the archive, the only put up, the excerpt, and the feed of the customized put up kind.

An alternate is to make use of your theme and prepend that info 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 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 examine to see if the acronym is numeric… by which case it’s possible a code. (I notice I may improve this additional, however for now it’s fantastic). Throughout the capabilities.php file of my little one 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 $put up;

    // Test if it is an 'acronym' put up
    if($post->post_type == 'acronym') {
        // Get the put up 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, once 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 only posts web page.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments