Dynamic Breadcrumbs in Divi Breadcrumbs Module

This post may contain referral links which may earn a commission for this site

My Divi Breadcrumbs module allows you to show users where they are in your site – i.e. their location relative to your homepage. But what if you want breadcrumbs which show the user the last pages they've visited?

While there isn't yet a feature in the breadcrumbs module for doing this, I've just added a "filter" to the breadcrumbs module – available from version 1.2.3 onwards – which lets you modify the breadcrumbs programmatically. This means you can implement dynamic breadcrumbs with something like the following PHP code:

add_action('wp', 'dbbccf_record_page_visit_in_cookie');
add_filter('et_pb_dmb_breadcrumbs_crumbs', 'dbbccf_get_breadcrumbs_from_cookie');

function dbbccf_record_page_visit_in_cookie() {
	global $post;
	$breadcrumbs = dbbccf_get_breadcrumbs_from_cookie();
	$title = get_the_title();
	if (!empty($title)) {
		$breadcrumbs[] = array(
			'text' => $title,
			'url' => get_permalink()
		);
	}
	$breadcrumbs = array_slice($breadcrumbs, -5);
	setcookie('dbbc_breadcrumbs', json_encode($breadcrumbs), 0, '/');
}

function dbbccf_get_breadcrumbs_from_cookie() {
	$breadcrumbs = array();
	if (isset($_COOKIE['dbbc_breadcrumbs'])) {
		$breadcrumbs = json_decode(stripslashes($_COOKIE['dbbc_breadcrumbs']), true);
	}
	return $breadcrumbs;
}

The code stores the last five pages viewed by the user in a cookie, and uses this to replace the regular breadcrumbs.

Please note that this is a rough implementation for demonstration purposes. If you plan to use it, please test it thoroughly to ensure it does what you need. If you have any questions about it, please ask and I'll do my best to help out.

Hundreds of new features for Divi
in one easy-to-use plugin

0 Comments

Submit a Comment

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