loading

Extend post types with custom taxonomies

[Fun with code is a multi-part series. Read the rest.]

[You're reading part 2 (of 3). Read part 1. Read part 3.]

For part two of this series, I want to show how I extended the default functionality of WordPress custom post types and taxonomies in order to build a user-friendly way to associate posts from one type to another.

To recap, in part one, we set up a couple of custom post types. To start this tutorial we’ll be using a very similar function to set up the taxonomy which we will leverage for post-to-post association.

The same but different

For the Great Plains Motion Picture Company website, the focus is on their videos. Each video can be assigned directors and editors. I then use that association to output the content to the front-end of the site. For the purposes of this tutorial, I’ll simplify this and create a one-way single association from post to post. In this case, we will just need one taxonomy to be defined. The argument arrays and requirements for registering taxonomies are very similar to post types. If you were on board for part one of this series, this next snippet should not require much explanation.

<?php
function custom_register_taxonomies()  {
	$labels = array(
		'name' => _x( 'Associations', 'taxonomy general name' ),
		'singular_name' => _x( 'Association', 'taxonomy singular name' ),
		'search_items' =>  __( 'Search Associations' ),
		'all_items' => __( 'All Associations' ),
		'parent_item' => __( 'Parent Association' ),
		'parent_item_colon' => __( 'Parent Association:' ),
		'edit_item' => __( 'Edit Association' ), 
		'update_item' => __( 'Update Association' ),
		'add_new_item' => __( 'Add New Association' ),
		'new_item_name' => __( 'New Association Name' ),
		'menu_name' => __( 'Association' )
	);
	$capabilities = array(
		'manage_terms' => 'nobody',
		'edit_terms' => 'nobody',
		'delete_terms' => 'nobody'
	);
	$rewrite = array(
		'slug' => 'association'
	);
	$args = array(
		'hierarchical' => true,
		'labels' => $labels,
		'show_ui' => true,
		'query_var' => 'association',
		'capabilities' => $capabilities,
		'rewrite' => $rewrite
	);
	register_taxonomy('association', 'products', $args);
}
add_action( 'init', 'custom_register_taxonomies', 1);
?>

The only real differences here are the new set of options for a taxonomy instead of a post type, and the use of a different function,<?php register_taxonomy('association', 'products', $args); ?>. Simply put, that function requires that you first supply a unique name for the taxonomy to register, a post type to apply it to, and the array of arguments you’ve set up for it. The post type can be given as a single string, if only one is to be used, or as an array of strings if it is to be associated with multiple post types. Finally we call<?php add_action( 'init', 'custom_register_taxonomies', 1); ?>, this attaches the function to WordPress at the appropriate time, and giving it the priority setting of 1, insures that it will fall in the appropriate order, after the post types are set up.

Hack it up

The trick here is the $capabilities array. For most instances where you would use custom taxonomies, we wouldn’t even need to include this at all. Setting these capabilities to ‘nobody’ basically makes them match none of the available conditions for capability allowances to match up, effectively ‘locking down’ the taxonomy, restricting the user from adding or deleting their own entries to the list of terms or categories. This is the first step towards the big finish. Now we need to build a function that uses the posts filed under the ‘People’ post type to create the terms instead of requiring the user to manually add them.

22 Sep 2019

‘Enter’ to submit

SVG is a thing now; you should use it.

With all this talk about resolution independence and responsive design, how many times have you built something for the internet and thought, hey, it sure would be nice if I could have a vector graphic here instead of a series of pre-saved bulky images switching in and out some way or another? For example: every single logo ever, social media buttons, line art of any type. If your concern is responsiveness and you’re using images, then you’re in for trouble.…

The multiply effect is a lie

This year’s fabulous Big Omaha website is showcasing a little visual trick. Early on, when Nathan and I were discussing the intended visuals on the site, I failed to notice he was using the multiply effect in Photoshop to achieve the appearance of the red overlays – and he went forward working that into the design. To my dismay, when I went to add the red layers by simply overlaying a slightly transparent layer of red over the top of…

Fun with code at Meet The Pros

Last week I spoke at Meet The Pros and, not surprisingly, my presentation was titled Fun with code. When I had volunteered to speak way back when, I envisioned myself talking about a bunch of code stuff and making it really interesting and fun. If you’re a regular reader of the Fun with code series here, you probably don’t remember any of it being terribly interesting or fun. It may be really helpful and useful, but it’s certainly too specialized…

Detecting a retina display

A few weeks ago, we announced that oxidedesign.com was fully retina optimized. For those of you who do not know, a retina display is a monitor or device that has more pixels per inch of space than a normal display. This basically means it is harder to tell where the edges of pixels are, allowing for an extremely crisp and clean appearance. This works great on the web for things like text, but a problem arises when displaying images. Since…

Access Restriction for Live Staging Sites

We have a lot of active, ongoing projects here at Oxide, especially when it comes to the web. In order to build and make changes to sites which can still be viewable for internal or client review, we run live staging versions of them on subdomains of oxidedesign.com. Don’t bother digging around looking for a secret gem though, because you can’t get in. At least, not anymore.

Using custom post types for WordPress

Over the past few years, I’ve written quite a bit of code and I’ve had a lot of fun doing it. I’ve got tons of useful snippets in my back pocket and I’m just itching to share them with the world. I’m going to take this opportunity to start a new series here on the Oxide blog, Fun with code. To get things started, let’s outline a problem and uncover a situation which creative code can solve.