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 Posts

Fade a Divi Image Module Edge into the Background

Want to create a stylish fade effect on your Divi image module—where one side fades smoothly into the background? With a bit of CSS, you can make any edge (or corner) of the image fade out: top, bottom, left, right, or even diagonally.Fade a Divi Image Module Edge...

Hide the Header and Footer in the Hello Elementor Theme

Removing the default header and footer from your Hello Elementor theme allows for a streamlined and distraction-free website design. This is especially useful when creating unique landing pages, full-width layouts, or custom headers and footers with a page builder. In...

Setting up the Divi Password Box Module

Setting up password protection on a page can help you control access to sensitive or private content in WordPress, allowing only authorized visitors to view certain sections. With the Divi Password Box module, you can replace the plain Divi password form with a fully...

Open Divi Social Media Icons in a New Tab

Ensuring your website's social media icons open in a new browser tab creates a seamless user experience and keeps visitors engaged with your site while allowing them to explore your social media presence. This approach prevents users from navigating away from your...

Set Hover Color for Menu Links in the Divi Menu Module

Customizing the hover color of menu links lets you enhance the user experience and maintain visual consistency with your website’s brand. By setting a specific hover color, you can guide visitors' attention and improve navigation feedback. In this guide we show you...

Random Posts