Adding a custom taxonomy to WP Job Manager

What follows is how to add and use a custom taxonomy with the WP Job Manager plugin for WordPress.

The documentation for the plugin provides some good hints and code examples for adding additional fields – but for a recent project I needed to use a custom taxonomy, so roll your sleeves up and let get started.

The first thing is to define the custom taxonomy.

/**
 * Register the skill taxonomy for the job_listing post type.
 *
 * @since 1.0.0
 */ 
function gds_create_taxonomy_skill() {
	register_taxonomy(
		'skill',
		'job_listing',
		array(
		'labels' => array(
			'name' => __( 'Skills' ),
			'singular_name' => __( 'Skill' ),
			'search_items' =>  __( 'Search Skills' ),
			'all_items' => __( 'All Skills' ),
			'edit_item' => __( 'Edit Skill' ),
			'update_item' => __( 'Update Skill' ),
			'add_new_item' => __( 'Add New Skill' ),
			'new_item_name' => __( 'New Skill' ),
			'menu_name' => __( 'Job Skills' ),
			),
		'hierarchical' => false,
		'rewrite' => array('slug' => 'skill'),
		)
	);
	
}
add_action('init', 'gds_create_taxonomy_skill');

This creates the skill taxonomy adding it to the job_listing post type. In this case I set hierarchical to false so it behaves just like the default Tags used on regular WordPress Posts.

Next is to add the skill taxonomy to the job submission form used on the frontend of the website, this is done using the submit_job_form_fields filter provided in the plugin. In this case I set the type to use term-checklist but you can use a different field type, take a look in the wp-job-manager/templates/form-fields folder for available types.

/**
 * Add skill field - frontend.
 *
 * @since 1.0.0
 */
function frontend_add_skills_field( $fields ) {
	$fields['job']['skill'] = array(
		'label'       => __( 'Required Skills', 'job_manager' ),
		'type'        => 'term-checklist',
		'required'    => true,
		'placeholder' => 'Choose skills candidates should possess.',
		'priority'    => 5,
		'taxonomy'    => 'skill',
	);
	return $fields;
}
add_filter( 'submit_job_form_fields', 'frontend_add_skills_field' );

The last piece of the puzzle is to make sure the fields are retrieved when a user goes from previewing their job listing back to editing it on the frontend of the website. Again, there’s a filter available submit_job_form_fields_get_job_data to hook into.

/**
 * Retrieve the skill field when submitting/previewing on the frontend.
 *
 * @since 1.0.0
 */
function frontend_retrieve_skills_data( $fields, $job ) {
	$fields[ 'job' ][ 'skill' ]['value'] = wp_get_object_terms( $job->ID, 'skill', array( 'fields' => 'ids' ) );
	return $fields;
}
add_filter( 'submit_job_form_fields_get_job_data', 'frontend_retrieve_skills_data', 10, 2);

The site I was working on uses a custom child theme of mine, so I chose to add this code to the child themes’ functions.php file, but this could just as easily be placed in a site specific plugin to keep it persistent if themes are switched.

Published on in Coding.
0
0

Sign up to email alerts when I publish new posts.

I will never spam you, send you junk mail, sales emails or pass your email address to third party marketing entities.