Mapping Terms to Posts in Breadcrumb Trails

In certain site configurations, it makes sense to replace the WordPress built-in term archives with a post (an instance of some post type). Typically, a page (instance of the page post type) is used in place of the term archive. Since version 1.5, Breadcrumb NavXT Paths supports mapping terms to pages (with regards to where the breadcrumb links to). To facilitate this, Breadcrumb NavXT Paths provides a metabox in the term editor for establish this mapping.

Sometimes, the page post type does not make sense for this mapping. In that case, the bcn_paths_map_term_post_args can be used to change the post type that the term editor metabox added by Breadcrumb NavXT displays. Note that this filter was introduced in Breadcrumb NavXT Paths 1.6.0.

The Code

Placing the following code into a site specific plugin, will allow Breadcrumb NavXT Paths to map terms to the post post type:

add_filter('bcn_paths_map_term_post_args', 'my_bcn_paths_map_term_post_args', 10, 3);
function my_bcn_paths_map_term_post_args($args, $term, $taxonomy)
{
     $args['post_type'] = 'post';
     return $args;
}

Naturally, this can be modified to specify post types other than post. Should multiple post types be desired, just replace the string 'post' with an array of the post type names. Since this filter affects the arguments sent to get_post, other arguments may be set as well.

-John Havlik

Notes on LUKS + EFISTUB

Running off of an encrypted root filesystem has been one of those things that never seemed to float to the top of the todo list. However, back in December (2018, this article lived in the drafts bin for quite some time), it finally made it to the top of the todo list. At the time, one was preparing the Dell XPS 15 9550 to replace the Ideapad s405 for travel. Encrypting everything seemed prudent for a travel laptop.

As of the writing of this article, LUKS is the standard way of encrypting a filesystem in Linux. Generally, a boot loader is used to kick off an initramfs which loads the basics (need LVM, dm-crypt, and LUKS) and prompts for the passphrase for decrypting the root filesystem. If you’re fine with running a boot loader, most guides will get you going with LUKS quite quickly.

However, running a full boot loader on UEFI systems feels archaic. There is just something about using the kernel’s built-in EFISTUB that feels more elegant. And, this is where things divert from the bog-standard path. Typically, when using the EFISTUB, one does not bother with an initramfs (compile the kernel for you known hardware set and you’re good to go). However, an initramfs is integral to having an encrypted root partition.

initramfs Woes

The first problem started with trying to get a working initramfs. Since one had not used an initramfs with EFISTUB previously, there were a few hurdles to overcome. Initially, one tried to use an external initramfs. However, the 9550 does not allow/pass UEFI parameters nicely, and using the built-in kernel command line to specify an external initrfamfs in the EFI boot partition did not work. So, the initramfs needs to be built into the kernel for the XPS15 9550. This lead to a second problem.

Initially, the initramfs that genkernel builds was tried. Unfortunately, it appears this is (as of late 2018) broken/not-suitable for situations where the initramfs needs to be bundled into the kernel. Luckily, betterinitramfs can be bundled into the kernel.

Naturally, there is one gotcha to keep in mind regarding betterinitramfs. As distributed, betterinitramfs does not populate /dev/disk/by-uuid et al. as it does not provide udev (or eudev). The end result is real root needs to be specified using /dev/BLOCKDEVICENAME rather than using PARTUUID.

Conclusion

While the setup of using EFISTUB with an LUKS encrypted root partition is a little esoteric, it is possible to get working. There are a bunch of UEFI related pitfalls waiting to snare you—different platforms will have a different mix of issues. Then again, all UEFI systems should be able to use the initramfs embedded in the kernel when using the EFISTUB boot loader. Regardless, this path is not advised for those learning about/using LUKS for the first time.

-John Havlik

Remove ‘Private:’ from bbPress Private Forum and Topic Titles

Helpfully, and in some cases, annoyingly, WordPress, by default, prepends the text ‘Private: ‘ to the titles of private posts. While this may be helpful for the user, sometimes it is unnecessary. In the case for bbPress, it may get tiresome to see ‘Private: ‘ everywhere, and for logged in users with permissions to see said forums and topics, it is redundant. Luckily, WordPress provides the handy private_title_format filter which can be used to control the format of titles for private posts.

The Code

The below code, when placed in a site specific plugin, will remove the ‘Private: ‘ text from private forums and topics:

add_filter('private_title_format', 'my_private_title_format', 10, 2);
function my_private_title_format($format, $post)
{
	if($post instanceof WP_Post and ($post->post_type === 'forum' || $post->post_type === 'topic'))
	{
		$format = '%s';
	}
	return $format;
}

Note that this can be adapted to apply to any post type, or post types that are not forum and topic. Simply, change the post type checks in the if statement to target the desired post type(s).

-John Havlik

Showing Private Posts in the Breadcrumb Trail

In Breadcrumb NavXT 6.4.0 the default behavior of Breadcrumb NavXT was changed to automatically exclude private posts from the breadcrumb trail. As part of this change, a new filter bcn_show_post_private was introduced to allow control over what private posts are included in the breadcrumb trail.

Basic Usage – Including All Private Posts

The following code, when placed in a site specific plugin, will enable display of all private posts in the breadcrumb trail.

add_filter('bcn_show_post_private', 'my_bcn_show_post_private', 10, 2);
function my_bcn_show_post_private($show, $ID)
{
	return true;
}

Something More Advanced

Rather than display all private posts in the breadcrumb trail, prehaps it is appropriate to show private posts of a specific post type. For example, the below will include private posts of the type forum (e.g. from bbPress) in the breadcrumb trail, while private posts from other post types will remain excluded.

add_filter('bcn_show_post_private', 'my_bcn_show_post_private', 10, 2);
function my_bcn_show_post_private($show, $ID)
{
	$post = get_post($ID);
	if($post instanceof WP_Post and $post->post_type === 'forum')
	{
		$show = true;
	}
	return $show;
}

This is not the only way private posts can be selectively included. Some other possibilities include checking if the user is logged in, checking if the user has specific permissions, or specifically include posts based on the post ID.

-John Havlik

Non-Hierarchical Taxonomies and JSON-LD Compatibility

While Breadcrumb NavXT for some time has provided methods for generating JSON-LD formatted BreadcrumbLists, the use of non-hierarchical taxonomies within a breadcrumb trail may result in non-compliant breadcrumb trails. For the case of a post only being a member of a single term within a non-hierarchical taxonomy, since Breadcrumb NavXT 6.4, compliant JSON-LD is generated. However, when a post is a member of multiple non-hierarchical terms within the same taxonomy, we run into problems.

When using the normal display functions, all of the terms in the non-hierarchical taxonomy that the post is a member of will be displayed, separated by commas. This behavior has been more-or-less the same since support for tags was added in 2.1.0. In essence, when there are multiple terms, there is a second dimension to the breadcrumb trail for that specific breadcrumb.

Unfortunately, it does not appear that Schema.org BreadcrumbList has the facility to support multi-dimensional breadcrumbs. Hence, a single term needs to be selected when there are multiple for a post. Luckily, the bcn_post_terms filter can be used to do this.

The Code

Introduced in Breadcrumb NavXT 5.4, bcn_post_terms allows us to filter out all of the terms returned by post_terms(). In this case, only the first term is wanted.

add_filter('bcn_post_terms','my_bcn_post_term_selector', 10, 3);
function my_bcn_post_term_selector($terms, $taxonomy, $id)
{
	//Check to ensure the terms list is an array and there is more than one item in it
	if(is_array($terms) and count($terms) > 1)
	{
		//Return the fist item in a new array
		return array(0 => reset($terms));
	}
	return $terms;
}

To get started quickly, just copy and paste the above code into a site specific plugin and start playing.

Note: This code relies on behavior introduced in Breadcrumb NavXT 6.4 and will not work in prior versions.

-John Havlik