do_action( ‘jobrelay_wpcon_meta_added’, $post_id int, $key string, $value string, $job_data array)

In this article

Fires after a mapped meta value has been successfully saved.

Parameters

More information

This hook fires once a mapped meta has been saved agains the newly created job post. It can be used to take action after a specific meta field has been saved by JobRelay.

Example usage

Example 1 – splitting data to save what you need

Example 1 – splitting data to save what you need

As the job reference needs to be unique, most posting services append a data timestamp to the reference. Therefore instead of the reference entered by the consultant, you end up with something slightly different.

For example a consultant enters test123 as the reference but the reference saved in WordPress ends up as test123_1673536910.

Often the website should only display the reference without the timestamp. One way of getting around this, is to save an additional reference without the timestamp.

The code below uses the jobrelay_wpcon_meta_added action in order to save a non timestamped reference. It is saved with a meta key of _job_reference_display.

<?php
/**
 * Save a display version of the job reference without the timestamp.
 *
 * @param integer $post_id – this is the ID of then newly created job post.
 * @param string  $meta_key – the meta key of the saved meta data.
 * @param string  $meta_value – the value of the meta data just saved.
 * @param array   $job_data – this is the array of job data from the posting service.
 */
function hd_save_display_job_reference( $post_id, $meta_key, $meta_value, $job_data ) {

	// if the meta just added, does not have the job reference meta key.
	// change "_job_reference" to match the meta key you are using for the reference.
	if ( '_job_reference' !== $meta_key ) {
		return; // do nothing.
	}

	// split the job reference value at the underscore.
	$reference_parts = explode( '_', $meta_value );

	// save the first part of the array - the refernce the consultant entered.
	$job_reference = $reference_parts[0];

	// if we have a job reference to save.
	if ( ! empty( $job_reference ) ) {

		// save this as meta against the new job.
		update_post_meta( $post_id, '_job_reference_display', sanitize_text_field( $job_reference ) );

	}

}

add_action( 'jobrelay_wpcon_meta_added', 'hd_save_display_job_reference', 10, 4 );

Now that you have a new custom field saved as reference display, you can output this on the front end of your site like so:

<?php
// get the reference display.
$reference_display = get_post_meta( $post->ID, '_job_reference_display', true );

// if we have a reference.
if ( ! empty( $reference_display ) ) {

	// output the reference.
	echo esc_html( $reference_display );

}
Example 2 – Changing “yes/no” strings to 0 or 1

Example 2 – Changing “yes/no” strings to 0 or 1

You may want to save data as a 0 or 1 in WordPress, often the case when a custom field checkbox is saved. However, the data may be sent from the posting service as either yes, or no.

This snippet of code allows you to maniplulate the date sent before it saved. In this example, if the sent value is yes we will save 1 and if the sent value is no we will save 0.

<?php
/**
 * Save a display version of the job reference without the timestamp.
 *
 * @param integer $post_id – this is the ID of then newly created job post.
 * @param string  $meta_key – the meta key of the saved meta data.
 * @param string  $meta_value – the value of the meta data just saved.
 * @param array   $job_data – this is the array of job data from the posting service.
 */
function hd_manipulate_yes_no_values( $post_id, $meta_key, $meta_value, $job_data ) {

	// set the meta key to run this function on.
	$meta_key_to_action = 'display_on_home';

	// If the meta key is not the one we want, bail.
	// Change the "display_on_home" meta key to match your meta key.
	// This will be the key you have mapped a value to in the meta mappings settings.
	if ( $meta_key_to_action !== $meta_key ) {
		return;
	}

	// If the meta value is empty, bail.
	if ( empty( $meta_value ) ) {
		return;
	}

	// if the value sent is a yes.
	if ( 'yes' === $meta_value || 'Yes' === $meta_value ) {
		
		// Set the value to 1 - if this needs to be a string, you would use '1'.
		$meta_value = 1;

	} else {
		
		// Set the value to 0 - if this needs to be a string, you would use '0'.
		$meta_value = 0;

	}

	// Update the post meta with the new value - zero or one.
	update_post_meta( $post_id, $meta_key_to_action, $meta_value );
	
}

add_action( 'jobrelay_wpcon_meta_added', 'hd_manipulate_yes_no_values', 10, 4 );