Breadcrumb NavXT and Bootstrap 4 Breadcrumb

Out of the box Breadcrumb NavXT generates markup that is ready for Schema.org BreadcrumbList compliance. While this is great for most cases, for users of frameworks, some markup changes may be desireable. This article looks at what is necessary to get Breadcrumb NavXT to generate markup compliant with Bootstrap 4’s Breadcrumb component.

The Code

To generate Bootstrap 4 breadcrumb compliant markup, two bits of code are needed. The first is the appropriate calling code (including the proper wrapping markup). Place the following calling code in the appropriate theme file(s) (usually header.php):

<?php if(function_exists('bcn_display_list')):?>
<nav class="breadcrumbs" typeof="BreadcrumbList" vocab="https://schema.org/" aria-label="breadcrumb">
    <ol class="breadcrumb">
        <?php bcn_display_list();?>
    </ol>
</nav>
<?php endif; ?>

The second bit of code goes into a site specific plugin:

add_filter('bcn_display_attributes', my_display_attributes_filter, 10, 3);
function my_display_attributes_filter($attribs, $types, $id)
{
    $extra_attribs = array('class' => array('breadcrumb-item'));
    //For the current item we need to add a little more info
    if(is_array($types) && in_array('current-item', $types))
    {
        $extra_attribs['class'][] = 'active';
        $extra_attribs['aria-current'] = array('page');
    }
    $atribs_array = array();
    preg_match_all('/([a-zA-Z]+)=["\']([a-zA-Z0-9\-\_ ]*)["\']/i', $attribs, $matches);
    if(isset($matches[1]))
    {
        foreach ($matches[1] as $key => $tag)
        {
            if(isset($matches[2][$key]))
            {
                $atribs_array[$tag] = explode(' ', $matches[2][$key]);
            }
        }
    }
    $merged_attribs = array_merge_recursive($atribs_array , $extra_attribs);
    $output = '';
    foreach($merged_attribs as $tag => $vals)
    {
        $output .= sprintf(' %1$s="%2$s"', $tag, implode(' ', $vals));
    }
    return $output;
}

This code adds the breadcrumb-item to every breadcrumb in the trail. Additionally it adds the active class and the arria-current attribute to the current item. While the first could be easily achieved by updating every Breadcrumb template, this solution is easier to implement and allows us to use bcn_display_list().

Unfortunately, the bcn_display_attributes filter available since Breadcrumb NavXT 6.0 is not the easiest use in an efficient and robust manner. In the future a new filter will be added to address this deficiency. As presented, my_display_attributes_filter will be greatly simplified with said new filter (lines 11-22 and 24-28 go away). This article will be updated at that time to reflect the changes in API.

-John Havlik

[end of transmission, stay tuned]

2 Comments Updated:

Fix Bluetooth Mouse Pairing but not Moving Cursor in Linux

I recently picked up a Logitech M585 to replace my old M577 which was randomly registering multiple clicks for each left click. This was the second button to go bad on that mouse so it was time for an upgrade (the back click action on the mouse wheel had gone out a year back).

While I had no problems pairing the M585 with my laptop running Linux, it was not moving the cursor and mouse clicks were not registering. This is not the first mouse that I’ve had issues with on this laptop, the Microsoft Bluetooth Mobile Mouse 3600 exhibited a similar behavior. Checking the system logs, I found the following clue:

[bluetoothd] input-hog profile accept failed for XX:XX:XX:XX:XX:XX

Where XX:XX:XX:XX:XX:XX is the bluetooth address for the mouse. After some digging, I came across the solution. CONFIG_UHID needs to be set to ‘y’ in the kernel config to enable userspace I/O driver support for the HID subsystem.

CONFIG_UHID=y

After making this change, recompiling the kernel and rebooting the M585 pairs and works properly as a mouse. Additionally, the MS Bluetooth Mobile Mouse 3600 now works properly as well.

-John Havlik

[end of transmission, stay tuned]

Enabling the Breadcrumb NavXT REST API

Beginning in Breadcrumb NavXT 6.2, the REST API endpoints provided by Breadcrumb NavXT are no longer automatically enabled. Instead, the specific endpoints must be requested using the bcn_register_rest_endpoint filter. Currently, there are three available endpoints: post, taxonomy, and author.

Below is an example of how to enable the Breadcrumb NavXT post endpoint. It is recommended to run this in the WordPress rest_api_init action at a priority of 9 or higher. This is to ensure the filter is registered before Breadcrumb NavXT attempts to run it.

add_filter('bcn_register_rest_endpoint', 'my_bcn_endpoint_filter', 10, 4);
function my_bcn_endpoint_filter($register_rest_endpoint, $endpoint, $version, $methods)
{
	if($endpoint === 'post')
	{
		$register_rest_endpoint = true;
	}
	return $register_rest_endpoint;
}

Enabling multiple endpoints is as simple as ORing checks of the value of $endpoint in the if statement. One important behavior to note: if BCN_DISABLE_REST_API is set to true, the Breadcrumb NavXT REST API will not be available. That is, an endpoint will be enabled if it is requested via the bcn_register_rest_endpoint filter and BCN_DISABLE_REST_API is set to false or is not set.

-John Havlik

[end of transmission, stay tuned]

Disabling Breadcrumb NavXT’s REST API

One of the new features in Breadcrumb NavXT 6.1.0 is the introduction of several REST API endpoints. These endpoints allow for the retrieval of Schema.org BreadcrumbList compliant JSON-LD formatted breadcrumb trails for posts, terms, and author archives. This quick guide presents an example of how to disable the Breadcrumb NavXT REST API.

Given that not all sites want or need REST API access to breadcrumb trails, Breadcrumb NavXT 6.1.0 also allows its REST API to be completely disabled. This is accomplished through setting the BCN_DISABLE_REST_API named constant.

The Code

define('BCN_DISABLE_REST_API', true);

Placing the above code into wp-config.php will disable the Breadcrumb NavXT REST API for that WordPress install. It is as simple as that.

-John Havlik

[end of transmission, stay tuned]

Changing the Home Breadcrumb Title

Prior to Breadcrumb NavXT 4.3.0, there was an option for setting the Home and Mainsite breadcrumb titles. However, these were removed as their functionality was completely redundant—the same behavior could be achieved by modifying the Home and Mainsite breadcrumb templates, replacing %title% and %htitle% with the desired title.

This works fine for most use cases. However, it does not work with bcn_display_json_ld() (introduced in 5.7.0). Luckily, there is an even better way to define the titles for these breadcrumbs—using the bcn_breadcrumb_title filter.

The Code

add_filter('bcn_breadcrumb_title', 'my_breadcrumb_title_swapper', 3, 10);
function my_breadcrumb_title_swapper($title, $type, $id)
{
	if(in_array('home', $type))
	{
		$title = __('Home');
	}
	return $title;
}

To force the Home breadcrumb to use ‘Home’ as the title, just place this code into your site specific plugin and activate. Want to use something else as the title? Change ‘Home’ to whatever you wish to display for the home breadcrumb.

Consider the Following

  1. Combining this method of specifying the Home title with the method of replacing %title% and %htitle% with the desired title in the Home breadcrumb templates is not recommended—the Home breadcrumb templates will take precedence.
  2. This example may be extended to modify the title for the Mainsite breadcrumb. To do so, look for main-home in the $types array instead of  home.

-John Havlik

[end of transmission, stay tuned]