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.