jobrelay_wpcon_meta_added
The jobrelay_wpcon_meta_added
action hook fires directly after a specific meta field has been processed.
Functions added to this hook are passed the following arguments:
$job_post_id
– this is the ID of then newly created job postmeta_key
– the meta key of the saved meta datameta_value
– the value of the meta data just saved$job_data
– this is the array of job data from the posting service
This action is a good action to use if you want to fire a function after a specific piece of meta data has been processed, or you want to do something if a something has a specific meta value.
Example
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 );
}