Code snippets and customisations

Below are some popular code snippets that we have used for different clients for a variety of different reasons.

Snippets on this page

Forcing the value of a specific field

Sometimes you may want a value for a field to always be a specific value, rather than the value that is sent from the posting service.

The code below will allow you to do this – either add to your themes functions.php file or include as part of a plugin. In this example we use the company name field.

/**
 * Override a JobRelay field for all jobs.
 *
 * @param  array $job_data The array of job data sent from JobRelay.
 * @return array $job_data The modified job data array.
 */
function hd_overide_jobrelay_field( $job_data ) {

	// set the new value for the field - company name in this example
	$job_data['company_name'] = 'Highrise Digtial';

	// return the job data.
	return $job_data;

}

add_filter( 'jobrelay_wpcon_job_data', 'hd_overide_jobrelay_field', 10, 1 );

Check if a job has been delivered from JobRelay

Sometimes a website owner or developer, may want do something specific for JobRelay jobs only. To do this you can use the code below in your templates to check whether a job was delivered from JobRelay.

<?php
/**
 * Check if a job is delivered via JobRelay.
 *
 * All jobs that are delivered via JobRelay are assigned the "External" term in the jobrelay_source taxonomy.
 */
if ( has_term( 'External', 'jobrelay_source' )  ) {

	// do stuff here for external jobs only.

}

Save a team member post as meta against the job

Often a site may use a custom post type to store team members or consultants. Each consultant then would have their own page in WordPress using the single template for the custom post type.

To associate a particular team member with a job, you may want to store the post. ID of the team member post that represents the consultant that posted the job.

To do this we can use the contact_email field in the JobRelay feed, which should be the consultants email address. When the job is posted we can find the team member post that has the same email address and save it the post ID against the job.

Here is some code that would do this:

<?php
/**
 * Save the consultant post ID against the newly created job.
 *
 * @param integer $post_id  The newly created job post ID.
 * @param array   $job_data An array of the job data sent.
 */
function jobrelay_save_team_member( $post_id, $job_data ) {

	// if we don't have a contact email address.
	if ( empty( $job_data['contact_email'] ) ) {
		return;
	}

	// find the team member post which has this email.
	$team_members = new WP_Query(
		array(
			'posts_per_page' => 1,
			'post_type'      => 'team_member',
			'fields'         => 'ids',
			'meta_key'       => 'email_address',
			'meta_value'     => sanitize_email( $job_data['contact_email'] ),
		)
	);

	// get he returned posts array.
	$team_members = $team_members->posts;

	// if no team members are returned.
	if ( empty( $team_members ) ) {
		return;
	}
	
	// update the meta to store the team member.
	update_post_meta( $post_id, '_team_member_id', array_shift( $team_members ) );

	// reset the query.
	wp_reset_postdata();

}

add_action( 'jobrelay_wpcon_job_inserted_complete', 'jobrelay_save_team_member', 10, 2 );

The team member post type in this instance is called team_member and we are saving the post ID of the team member found as meta data against the job with the meta key of _team_member_id.

You could place this code in either your themes functions.php file or better still as its own, or part of a plugin.