Adds Breadcrumb in Beaver Builder Theme

Original Request

Add breadcrumbs to my Posts in Beaver Builder (theme)

Breadcrumb is not available in Beaver Builder Theme. One of my users requested it. So I am sharing the tutorial for him and other Beaver users.

Adding Option in Theme Customizer

Enable Breadcrumb

Adding the Breadcrumb option in the customizer. So you can easily enable or disable it from theme customizer (appearance -> customize) as per your requirement for your site. Open the functions.php file of your bb child theme and add this PHP snippets.

Adding new section into theme customizer

/**
 * Adding new section into theme customizer
 */
add_action( 'customize_register', 'probb_customize_register', 11 );
function probb_customize_register( $wp_customize )
{
	probb_customize_breadcrumb( $wp_customize );
}

/**
 * Helper function
 */
function probb_customize_breadcrumb( $wp_customize )
{
	add_section($wp_customize, 'fl-content', array(
		/* Breadcrumb Section */
		'fl-breadcrumb' => array(
			'title'   	=> _x( 'Breadcrumb', 'Customizer section title.', 'fl-automator' ),
			'priority' 	=> 0,
			'options' 	=> array(

				/* Checkboxes */
				'show_breadcrumb' => array(
					'section_key' 	=> 'fl-breadcrumb',
					'setting'   => array(
						'sanitize_callback' => 'FLCustomizer::sanitize_checkbox_multiple'
					),
					'control'   => array(
						'class'         => 'FLCustomizerControl',
						'label'         => __('Enable Breadcrumb On', 'fl-automator'),
						'type'          => 'checkbox-multiple',
						'choices'       => array(
							'breadcrumb_front_page' 	=> __('Front Page', 'fl-automator'),
							'breadcrumb_posts_page' 	=> __('Posts Page', 'fl-automator'),
							'breadcrumb_single' 		=> __('Single Posts', 'fl-automator'),
							'breadcrumb_page' 			=> __('Pages', 'fl-automator'),
							'breadcrumb_archive' 		=> __('Archives', 'fl-automator'),
							'breadcrumb_404' 			=> __('404 Page', 'fl-automator')
						)
					)
				),
				
			)
		)
	));
}

/**
 * Adding new section into existing panel
 */
function add_section( $customizer, $panel, $sections )
{
	foreach ( $sections as $section_key => $section_data ) {
		// Add section
		$customizer->add_section( $section_key, array(
			'panel'    => $panel,
			'title'    => $section_data['title'],
			'priority' => $section_data['priority']
		));

		// section options
		if ( isset( $section_data['options'] ) ) {
			add_settings( $customizer, $section_data['options'] );
		}
	}
}

/**
 * Adding new setting into existing sections
 */
function add_settings( $customizer, $settings_data )
{
	$option_priority = 10;

	foreach ( $settings_data as $option_key => $option_data ) {

		// Add setting
		if ( ! isset( $option_data['setting'] ) ) {
			$option_data['setting'] = array( 'default' => '' );
		}

		if ( isset( $option_data['priority'] ) ) {
			$option_priority = $option_data['priority'];
		}

		$customizer->add_setting( $option_key, $option_data['setting'] );

		// Add control
		$option_data['control']['section']  = $option_data['section_key'];
		$option_data['control']['settings'] = $option_key;
		$option_data['control']['priority'] = $option_priority;
		$customizer->add_control(
			new $option_data['control']['class']( $customizer, $option_key, $option_data['control'] )
		);

		// Increment option priority
		$option_priority++;
	}
}

Creating Breadcrumb Builder Class

Creates a new file “class-breadcrumb.php” and put into “classes” folder of your bb child theme. This file is generating the breadcrumb links. Here is the complete code of this file

BB_Breadcrumb Class

 __( 'Home', 'fl-automator' ),
			'sep'                     => __( ' / ', 'fl-automator' ),
			'list_sep'                => ', ',
			'prefix'                  => '',
			'heirarchial_attachments' => true,
			'heirarchial_categories'  => true,
			'labels' => array(
				'prefix'    => __( 'You are here: ', 'fl-automator' ),
				'author'    => __( 'Archives for ', 'fl-automator' ),
				'category'  => __( 'Archives for ', 'fl-automator' ),
				'tag'       => __( 'Archives for ', 'fl-automator' ),
				'date'      => __( 'Archives for ', 'fl-automator' ),
				'search'    => __( 'Search for ', 'fl-automator' ),
				'tax'       => __( 'Archives for ', 'fl-automator' ),
				'post_type' => __( 'Archives for ', 'fl-automator' ),
				'404'       => __( '404 Page ', 'fl-automator' )
			)
		);

	}

	/**
	 * Determine if static page is shown on front page.
	 */
	static private function page_shown_on_front() {
		return 'page' === get_option( 'show_on_front' );
	}
	
	/**
	 * Return the final completed breadcrumb in markup wrapper.
	 */
	public function get_output( $args = array() ) {

		self::$args = apply_filters( 'probb_breadcrumb_args', wp_parse_args( $args, self::$args ) );

		return self::$args['prefix'] . self::$args['labels']['prefix'] . self::render_crumbs() . self::$args['suffix'];

	}

	/**
	 * Echo the final completed breadcrumb in markup wrapper.
	 */
	static public function output( $args = array() ) {

		echo self::get_output( $args );

	}

	/**
	 * Return the correct crumbs for this query, combined together.
	 */
	static private function render_crumbs() {

		$crumbs[] = self::get_home_crumb();

		if ( is_home() ) {
			$crumbs[] = self::get_blog_crumb();
		} elseif ( is_search() ) {
			$crumbs[] = self::get_search_crumb();
		} elseif ( is_404() ) {
			$crumbs[] = self::get_404_crumb();
		} elseif ( is_page() ) {
			$crumbs[] = self::get_page_crumb();
		} elseif ( is_archive() ) {
			$crumbs[] = self::get_archive_crumb();
		} elseif ( is_singular() ) {
			$crumbs[] = self::get_single_crumb();
		}

		$crumbs = apply_filters( 'probb_render_crumbs', $crumbs, self::$args );

		return implode( self::$args['sep'], array_filter( array_unique( $crumbs ) ) );

	}

	/**
	 * Return archive breadcrumb.
	 */
	static private function get_archive_crumb() {

		if ( is_category() ) {
			$crumb = self::get_category_crumb();
		} elseif ( is_tag() ) {
			$crumb = self::get_tag_crumb();
		} elseif ( is_tax() ) {
			$crumb = self::get_tax_crumb();
		} elseif ( is_year() ) {
			$crumb = self::get_year_crumb();
		} elseif ( is_month() ) {
			$crumb = self::get_month_crumb();
		} elseif ( is_day() ) {
			$crumb = self::get_day_crumb();
		} elseif ( is_author() ) {
			$crumb = self::get_author_crumb();
		} elseif ( is_post_type_archive() ) {
			$crumb = self::get_post_type_crumb();
		}

		return apply_filters( 'probb_archive_crumb', $crumb, self::$args );

	}

	/**
	 * Get single breadcrumb, including any parent crumbs.
	 */
	static private function get_single_crumb() {

		if ( is_attachment() ) {
			$crumb = self::get_attachment_crumb();
		} elseif ( is_singular( 'post' ) ) {
			$crumb = self::get_post_crumb();
		} else {
			$crumb = self::get_cpt_crumb();
		}

		return apply_filters( 'probb_single_crumb', $crumb, self::$args );

	}

	/**
	 * Return home breadcrumb.
	 */
	static private function get_home_crumb() {

		$url   = self::page_shown_on_front() ? get_permalink( get_option( 'page_on_front' ) ) : trailingslashit( home_url() );
		$crumb = ( is_home() && is_front_page() ) ? self::$args['home'] : self::get_breadcrumb_link( $url, '', self::$args['home'] );

		return apply_filters( 'probb_home_crumb', $crumb, self::$args );

	}

	/**
	 * Return blog posts page breadcrumb.
	 */
	static private function get_blog_crumb() {

		$crumb = self::get_home_crumb();
		if ( self::page_shown_on_front() ) {
			$crumb = get_the_title( get_option( 'page_for_posts' ) );
		}

		return apply_filters( 'probb_blog_crumb', $crumb, self::$args );

	}

	/**
	 * Return search results page breadcrumb.
	 */
	static private function get_search_crumb() {

		$crumb = self::$args['labels']['search'] . '"' . esc_html( apply_filters( 'the_search_query', get_search_query() ) ) . '"';
		return apply_filters( 'probb_search_crumb', $crumb, self::$args );

	}

	/**
	 * Return 404 (page not found) breadcrumb.
	 */
	static private function get_404_crumb() {

		$crumb = self::$args['labels']['404'];
		return apply_filters( 'probb_404_crumb', $crumb, self::$args );

	}

	/**
	 * Return content page breadcrumb.
	 */
	static private function get_page_crumb() {

		global $wp_query;

		if ( self::page_shown_on_front() && is_front_page() ) {
			$crumb = self::get_home_crumb();
		} else {
			$post = $wp_query->get_queried_object();
			if ( ! $post->post_parent ) {
				$crumb = get_the_title();
			} else {
				if ( isset( $post->ancestors ) ) {
					if ( is_array( $post->ancestors ) ) {
						$ancestors = array_values( $post->ancestors );
					} else {
						$ancestors = array( $post->ancestors );
					}
				} else {
					$ancestors = array( $post->post_parent );
				}

				$crumbs = array();
				foreach ( $ancestors as $ancestor ) {
					array_unshift(
						$crumbs,
						self::get_breadcrumb_link(
							get_permalink( $ancestor ),
							'',
							get_the_title( $ancestor )
						)
					);
				}

				// Add the current page title.
				$crumbs[] = get_the_title( $post->ID );

				$crumb = implode( self::$args['sep'], $crumbs );
			}
		}

		return apply_filters( 'probb_page_crumb', $crumb, self::$args );

	}

	/**
	 * Get breadcrumb for single attachment, including any parent crumbs.
	 */
	static private function get_attachment_crumb() {

		$post = get_post();

		$crumb = '';
		if ( self::$args['heirarchial_attachments'] ) {
			
			$attachment_parent = get_post( $post->post_parent );
			$crumb = self::get_breadcrumb_link(
				get_permalink( $post->post_parent ),
				'',
				$attachment_parent->post_title,
				self::$args['sep']
			);
		}
		$crumb .= single_post_title( '', false );

		return apply_filters( 'probb_attachment_crumb', $crumb, self::$args );

	}

	/**
	 * Get breadcrumb for single post, including any parent (category) crumbs.
	 */
	static private function get_post_crumb() {

		$categories = get_the_category();

		$cat_crumb = '';

		if ( 1 === count( $categories ) ) {
			// If in single category, show it, and any parent categories.
			$cat_crumb = self::get_term_parents( $categories[0]->cat_ID, 'category', true ) . self::$args['sep'];
		}
		if ( count( $categories ) > 1 ) {
			if ( ! self::$args['heirarchial_categories'] ) {
				// Don't show parent categories
				foreach ( $categories as $category ) {
					$crumbs[] = self::get_breadcrumb_link(
						get_category_link( $category->term_id ),
						'',
						$category->name
					);
				}
				$cat_crumb = implode( self::$args['list_sep'], $crumbs ) . self::$args['sep'];
			} else {
				// Show parent categories - see if one is marked as primary and try to use that.
				$primary_category_id = get_post_meta( get_the_ID(), '_category_permalink', true ); // Support for sCategory Permalink plugin.
				if ( ! $primary_category_id && function_exists( 'yoast_get_primary_term_id' ) ) {
					$primary_category_id = yoast_get_primary_term_id();
				}
				if ( $primary_category_id ) {
					$cat_crumb = self::get_term_parents( $primary_category_id, 'category', true ) . self::$args['sep'];
				} else {
					$cat_crumb = self::get_term_parents( $categories[0]->cat_ID, 'category', true ) . self::$args['sep'];
				}
			}
		}
		
		$crumb = $cat_crumb . single_post_title( '', false );

		return apply_filters( 'probb_post_crumb', $crumb, self::$args, $cat_crumb );

	}

	/**
	 * Get breadcrumb for single custom post type entry, including any parent (CPT name) crumbs.
	 */
	static private function get_cpt_crumb() {

		$post_type = get_query_var( 'post_type' );
		$post_type_object = get_post_type_object( $post_type );

		$cpt_archive_link = get_post_type_archive_link( $post_type );
		if ( $cpt_archive_link ) {
			$crumb = self::get_breadcrumb_link(
				$cpt_archive_link,
				'',
				$post_type_object->labels->name
			);
		} else {
			$crumb = $post_type_object->labels->name;
		}

		$crumb .= self::$args['sep'] . single_post_title( '', false );

		return apply_filters( 'probb_cpt_crumb', $crumb, self::$args );

	}

	/**
	 * Return the category archive crumb.
	 */
	static private function get_category_crumb() {

		$crumb = self::$args['labels']['category'] . self::get_term_parents( get_query_var( 'cat' ), 'category' );
		return apply_filters( 'probb_category_crumb', $crumb, self::$args );

	}

	/**
	 * Return the tag archive crumb.
	 */
	static private function get_tag_crumb() {

		$crumb = self::$args['labels']['tag'] . single_term_title( '', false );

		return apply_filters( 'probb_tag_crumb', $crumb, self::$args );

	}

	/**
	 * Return the taxonomy archive crumb.
	 */
	static private function get_tax_crumb() {

		global $wp_query;

		$term  = $wp_query->get_queried_object();
		$crumb = self::$args['labels']['tax'] . self::get_term_parents( $term->term_id, $term->taxonomy );

		return apply_filters( 'probb_tax_crumb', $crumb, self::$args );

	}

	/**
	 * Return the year archive crumb.
	 */
	static private function get_year_crumb() {

		$year = get_query_var( 'm' ) ? get_query_var( 'm' ) : get_query_var( 'year' );

		$crumb = self::$args['labels']['date'] . $year;

		return apply_filters( 'probb_year_crumb', $crumb, self::$args );

	}

	/**
	 * Return the month archive crumb.
	 */
	static private function get_month_crumb() {

		$year = get_query_var( 'm' ) ? mb_substr( get_query_var( 'm' ), 0, 4 ) : get_query_var( 'year' );

		$crumb = self::get_breadcrumb_link(
			get_year_link( $year ),
			'',
			$year,
			self::$args['sep']
		);

		$crumb .= self::$args['labels']['date'] . single_month_title( ' ', false );
		
		return apply_filters( 'probb_month_crumb', $crumb, self::$args );

	}

	/**
	 * Return the day archive crumb.
	 */
	static private function get_day_crumb() {

		global $wp_locale;

		$year  = get_query_var( 'm' ) ? mb_substr( get_query_var( 'm' ), 0, 4 ) : get_query_var( 'year' );
		$month = get_query_var( 'm' ) ? mb_substr( get_query_var( 'm' ), 4, 2 ) : get_query_var( 'monthnum' );
		$day   = get_query_var( 'm' ) ? mb_substr( get_query_var( 'm' ), 6, 2 ) : get_query_var( 'day' );

		$crumb  = self::get_breadcrumb_link(
			get_year_link( $year ),
			'',
			$year,
			self::$args['sep']
		);
		
		$crumb .= self::get_breadcrumb_link(
			get_month_link( $year, $month ),
			'',
			$wp_locale->get_month( $month ),
			self::$args['sep']
		);

		$crumb .= self::$args['labels']['date'] . $day . date( 'S', mktime( 0, 0, 0, 1, $day ) );

		return apply_filters( 'probb_day_crumb', $crumb, self::$args );

	}

	/**
	 * Return the author archive crumb.
	 */
	static private function get_author_crumb() {

		global $wp_query;

		$crumb = self::$args['labels']['author'] . esc_html( $wp_query->queried_object->display_name );

		return apply_filters( 'probb_author_crumb', $crumb, self::$args );

	}

	/**
	 * Return the post type archive crumb.
	 */
	static private function get_post_type_crumb() {

		$crumb = self::$args['labels']['post_type'] . esc_html( post_type_archive_title( '', false ) );

		return apply_filters( 'probb_post_type_crumb', $crumb, self::$args );

	}

	/**
	 * Return recursive linked crumbs of category, tag or custom taxonomy parents.
	 */
	static private function get_term_parents( $parent_id, $taxonomy, $link = false, array $visited = array() ) {

		$parent = get_term( (int)$parent_id, $taxonomy );

		if ( is_wp_error( $parent ) ) {
			return array();
		}

		if ( $parent->parent && ( $parent->parent != $parent->term_id ) && ! in_array( $parent->parent, $visited ) ) {
			$visited[] = $parent->parent;
			$chain[]   = self::get_term_parents( $parent->parent, $taxonomy, true, $visited );
		}

		if ( $link && !is_wp_error( get_term_link( get_term( $parent->term_id, $taxonomy ), $taxonomy ) ) ) {
			$chain[] = self::get_breadcrumb_link(
				get_term_link( get_term( $parent->term_id, $taxonomy ), $taxonomy ),
				'',
				$parent->name
			);
		} else {
			$chain[] = $parent->name;
		}

		return implode( self::$args['sep'], $chain );

	}

	/**
	 * Return anchor link for a single crumb.
	 */
	static private function get_breadcrumb_link( $url, $title, $content, $sep = '' ) {

		// Empty title, for backward compatibility.
		$title = '';

		$link = sprintf( '%s', esc_attr( $url ), $content );
		$link = '' . apply_filters( 'probb_breadcrumb_link', $link, $url, $title, $content, self::$args ) . '';

		if ( $sep ) {
			$link .= $sep;
		}

		return $link;

	}

}

Rendering Breadcrumb on The Site

Including the class-breadcrumb.php file, creating the object of BB_Breadcrumb class and displaying the breadcrumb horizontal bar above the content area with fl_content_open hook and output() method of  BB_Breadcrumb class.

If you already installed and activated the Yoast plugin then it will display the Yoast plugin’s breadcrumb.

Now open the functions.php file of your bb child theme and add this PHP snippets there.

Displaying the Breadcrumb

//* Includes the file
require_once 'classes/class-breadcrumb.php';

/**
 * Displaying the breadcrumb above the page content
 */
add_action( 'fl_content_open', 'probb_do_breadcrumb', 1 );
function probb_do_breadcrumb()
{
	if( ( class_exists('FLBuilderModel') && FLBuilderModel::is_builder_enabled() ) || 
		( in_array( get_post_type(), array( 'fl-builder-template', 'fl-theme-layout') ) ) || 
		( class_exists('FLThemeBuilderLayoutData') && FLThemeBuilderLayoutData::current_post_is(array( 'singular', 'archive', '404' ) ) )
	) {
		return;
	}

	$show_breadcrumb = FLTheme::get_setting( 'show_breadcrumb' );
	if ( ! is_array( $show_breadcrumb ) || 
		( is_single() && ! in_array( 'breadcrumb_single', $show_breadcrumb ) ) ||
		( ( is_page() && ! is_front_page() ) && ! in_array( 'breadcrumb_page', $show_breadcrumb ) ) ||
		( is_404() && ! in_array( 'breadcrumb_404', $show_breadcrumb ) ) ||
		( ( 'posts' === get_option( 'show_on_front' ) && is_home() ) && ! in_array( 'breadcrumb_front_page', $show_breadcrumb ) ) ||
		( ( 'page' === get_option( 'show_on_front' ) && is_front_page() ) && ! in_array( 'breadcrumb_front_page', $show_breadcrumb ) ) ||
		( ( 'page' === get_option( 'show_on_front' ) && is_home() ) && ! in_array( 'breadcrumb_posts_page', $show_breadcrumb ) ) ||
		( ( is_archive() || is_search() ) && ! in_array( 'breadcrumb_archive', $show_breadcrumb ) )
	) {
		return;
	}

	$breadcrumb_markup_open = '';
	}
	elseif ( function_exists( 'breadcrumbs' ) ) {
		breadcrumbs();
	}
	elseif ( class_exists( 'WPSEO_Breadcrumbs' ) ) {
		yoast_breadcrumb( $breadcrumb_markup_open, '
' ); } elseif( function_exists( 'yoast_breadcrumb' ) && ! class_exists( 'WPSEO_Breadcrumbs' ) ) { yoast_breadcrumb( $breadcrumb_markup_open, '
' ); } else { global $breadcrumb; if ( ! $breadcrumb ) { $breadcrumb = new BB_Breadcrumb; } $breadcrumb->output( $args = array() ); } }
Posted in