Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matadorjobs/0cf019b39f88934610585e2d931b76b3 to your computer and use it in GitHub Desktop.
Save matadorjobs/0cf019b39f88934610585e2d931b76b3 to your computer and use it in GitHub Desktop.
Modify Job Meta Fields Before Presentation
// If using matador_the_job_meta(), matador_get_the_job_meta(), or the [matador_job_field name="field"] shortcode,
// filter the output on its way out to your template using one of the following filters.
/**
* Dynamic Filter: Matador Template The Job Meta
*
* @since 3.4.0
*
* @param string $meta Meta value.
* @param int $id ID of job.
* @param string $context Template context, for filtering purposes.
*
* @return string
*/
// apply_filters( 'matador_template_the_job_meta_' . $key, $meta, $id, $context );
/**
* Filter: Matador Template The Job Meta
*
* @since 3.4.0
*
* @param string $meta Meta value.
* @param string $key Meta key name
* @param int $id ID of job.
* @param string $context Template context, for filtering purposes.
*
* @return string
*/
// $meta = apply_filters( 'matador_template_the_job_meta', $meta, $key, $id, $context );
// Example 1: Filter date objects to modify format
/**
* Matador The Job Meta Filter
*
* @since 2022-05-12
*
* @see @wordpress-filter `matador_template_the_job_meta` in /includes/template-functions.php
*
* @param string $meta Meta value.
* @param string $key Meta key name
* note, this example omits the additional $id and $context values
*
* @return string
* @author @jeremyescott, Jeremy Scott, Matador US LP dba Matador Software
*
*/
function example_matador_template_the_job_meta( $meta, $key ) {
// Even though we access this with a shortcode or template function on the front-end, make extra sure we don't
// do anything when we aren't supposed to.
if ( is_admin() || wp_doing_ajax() || wp_doing_cron() ) {
return $meta;
}
// Don't do anything if the job meta keys aren't the ones we need.
if ( ! in_array( $key, [ 'dateStart', 'dateEnd' ], true ) ) {
return $meta;
}
// Create a PHP DateTimeImmutable from the MySQL formatted date.
$date = DateTimeImmutable::createFromFormat( 'Y-m-d H:i:s', $meta );
// DateTimeImmutable::createFromFormat() will return false if it fails
if ( false === $date ) {
return $meta;
}
// Format the date to the example "12-May-2022" and return
return $date->format( 'D-F-Y' );
}
add_filter( 'matador_template_the_job_meta', 'example_matador_template_the_job_meta', 10, 2 );
// Example 2: Filter single 'salary' field into currency format
/**
* Matador The Job Meta {$key} Filter
*
* Example of the dynamic per-key filter on the salary field. Takes an interger salary number and formats to US
* currency, prepended by a $ dollar sign.
*
* @since 2022-05-12
*
* @see @wordpress-filter `matador_template_the_job_meta` in /includes/template-functions.php
*
* @param string $key Meta key name
* note, this example omits the additional $id and $context values
*
* @return string
* @author @jeremyescott, Jeremy Scott, Matador US LP dba Matador Software
*
*/
function example_matador_template_the_job_meta_salary( $meta ) {
// Even though we access this with a shortcode or template function on the front-end, make extra sure we don't
// do anything when we aren't supposed to.
if ( is_admin() || wp_doing_ajax() || wp_doing_cron() ) {
return $meta;
}
// 0 and '' are true for empty(), so return
if ( empty( $meta ) ) {
return $meta;
}
// 100000 and 100000.00 becomes $100,000
// 25000 and 25000.10 becomes $25,000
// @see https://www.php.net/manual/en/function.number-format.php
return "$" . number_format( $meta, 0, '.', ',' );
}
add_filter( 'matador_template_the_job_meta_salary', 'example_matador_template_the_job_meta_salary', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment