Extend the Divi Blog Module With Custom Filters

Written by Dan Mossop

Here is a powerful way to customize your Divi Blog Module using PHP code and filters. By adding the code given below, new filters will be added to the Divi Blog Module, which allow you to modify and extend the functionality of the Blog Module.

First, I'll give the code to add the filters, and then details of the filters it adds

Registering the Divi Blog Module filters using PHP

This PHP code below adds several filters to the Divi Blog Module:

  • dbc_blog_module_article – for filtering the HTML of individual articles in the blog listing
  • dbc_blog_module_post_meta_items – for filtering the post meta data items

To make these filters available, add this PHP code to your site:

// Add the "dbc_blog_module_article" filter

add_filter('et_pb_blog_shortcode_output', array(new DBC_Blog_Module_Filters, 'add_article_filters'), 10, 3);

class DBC_Blog_Module_Filters {
	
	private $props;

	public function add_article_filters($content, $render_slug, $module) {
		if (is_string($content)) { 
			$this->props = (is_object($module) && isset($module->props))?$module->props:array();
			return preg_replace_callback('/<article id="post-(\d+)".*?<\/article>/s', array($this, 'apply_article_filters'), $content);
		}
		return $content;
	}
	
	public function apply_article_filters($match) {
		if (isset($match[0]) && isset($match[1])) { 	
			return apply_filters('dbc_blog_module_article', $match[0], $this->props, $match[1]);
		}
		return $match;
	}
}


// Add the "dbc_blog_module_post_meta_items" filter

add_filter('dbc_blog_module_article', 'dbc_register_blog_module_post_meta_items_filter', 10, 3);

function dbc_register_blog_module_post_meta_items_filter($html, $props, $id) {
		
	if (preg_match('/<p class="post-meta">(.*?)<\/p>/s', $html, $matches)) {
		$meta_items = explode(" | ", $matches[1]);
	} else {
		$meta_items = array();
	}

	$meta_items = apply_filters('dbc_blog_module_post_meta_items', $meta_items, $props, $id);
	
	if (!empty($meta_items) && strpos($html, '<p class="post-meta">') === false) {
		$html = str_replace('<div class="post-content">', '<p class="post-meta"></p><div class="post-content">', $html);
	}

	$html = preg_replace('/<p class="post-meta">.*?<\/p>/s', '<p class="post-meta">'.implode(' | ', $meta_items).'</p>', $html);
		
    return $html;
}

Note that adding this code doesn't itself change the behavior of the site. It simply makes the new filters available for use by additional PHP code.

Filter: "dbc_blog_module_article"

This filter lets you add to or modify the HTML for individual articles listed in the blog module.

For instance, you could use it to add an author box or related posts after each article within the blog module, e.g.:

function add_author_box($article_html, $module_props, $post_id) {
   $author_box = '<div class="author-box">... your author box HTML code ...</div>';
   $article_html .= $author_box; // append author box to the content
   return $article_html; 
}
add_filter('dbc_blog_module_article', 'add_author_box', 10, 3);

Filter: "dbc_blog_module_post_meta_items"

This filter is helpful when you want to add or modify the post metadata, such as number of comments, post date, etc. It converts the existing meta data items into an array, which can then be manipulated via the filter.

Example:

function add_reading_time($meta_items, $module_props, $post_id) {
    $content = get_post_field( 'post_content', $post_id );
    // Calculating reading time based on average reading speed.
    $reading_time = ceil(str_word_count($content) / 200);
    $meta_items[] = $reading_time . ' min read';
    return $meta_items;
}
add_filter('dbc_blog_module_post_meta_items', 'add_reading_time', 10, 3);

Run PHP Code Directly in your Divi Layouts

Unlock endless customization, automation, and dynamic functionality by seamlessly adding PHP code to your Divi pages and posts with the Divi PHP Code Module. Style, preview, and debug your PHP creations directly in the visual builder with robust error handling and enhanced security.

About Dan Mossop

Dan is a Scottish-born web developer, now living in Brisbane with his wife and son. He has been sharing tips and helping users with Divi since 2014. He created Divi Booster, the first Divi plugin, and continues to develop it along with 20+ other Divi plugins. Dan has a PhD in Computer Science, a background in web security and likes a lot of stuff, 

0 Comments

Submit a Comment

Comments are manually moderated and approved at the time they are answered. A preview is shown while pending but may disappear if your are cookies cleared - don't worry though, the comment is still in the queue.

Your email address will not be published. Required fields are marked *.

We may earn a commission when you visit links on our website.

Latest From Divi Booster

Enable Automatic Looping for YouTube Videos in the Divi Video Module

Enabling automatic looping for YouTube videos in your Divi video module ensures that content plays seamlessly and continuously without interruption. This feature can enhance user engagement, highlight key information, or create dynamic visual effects on your website....

Hide Video Controls in Divi Video Module

Disabling YouTube video controls in the Divi Video Module helps create a seamless, distraction-free viewing experience for your site visitors. This approach is useful when you want full control over how your video content appears and is interacted with on the page, or...

Mute YouTube Videos by Default in the Divi Video Module

In the Divi Video Module, starting your YouTube videos muted by default can create a more user-friendly browsing experience on your webpage. It is particularly beneficial for reducing distractions, enhancing visitor engagement, and providing a seamless content preview...

Autoplay Videos in Divi Video Module

Enabling videos in your Divi video module to start playing automatically can enhance both the site's design and user engagement. To ensure full compatibility with browser requirements, such as Chrome's autoplay policy, it's often necessary to start your videos muted...

How to Add the Post Status in Divi's Dynamic Content

Divi's Dynamic Content feature is great for enhancing your post/page templates with post-specific information. While Divi includes an option to show information such as the Post Created Date using Dynamic Content, there is no equivalent option for the Post Status....

Random Divi Posts