Last active
January 7, 2020 01:23
-
-
Save tripflex/c36b2e7a3f212bbeb8cefbf530080ea0 to your computer and use it in GitHub Desktop.
Add additional Structured Data (schema) to WP Job Manager when using WP Job Manager Field Editor (baseSalary in this example)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'wpjm_get_job_listing_structured_data', 'smyles_add_field_to_job_structured_data', 10, 2 ); | |
/** | |
* Add baseSalary to Structured Data | |
* | |
* This is just an EXAMPLE of how to set this up, it's up to you to match the correct format required, | |
* and to determine what fields to use, and how to set it up based on your site. | |
* | |
* @param $data | |
* @param $post | |
* | |
* @return mixed | |
* @since @@version | |
* | |
*/ | |
function smyles_add_field_to_job_structured_data( $data, $post ){ | |
if( $post && $post->ID ){ | |
$salary = get_post_meta( $post->ID, '_job_salary', true ); | |
// Here you can add values that would be considered "not a salary" to skip output for | |
$no_salary_values = array( 'Not Disclosed', 'N/A', 'TBD' ); | |
// Don't add anything if empty value, or value equals something above in no salary values | |
if( empty( $salary) || in_array( strtolower( $salary ), array_map( 'strtolower' , $no_salary_values ) ) ){ | |
return $data; | |
} | |
// Determine float value, stripping all non-alphanumeric characters | |
$salary_float_val = (float) filter_var( $salary, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ); | |
if( ! empty( $salary_float_val ) ){ | |
// @see https://schema.org/JobPosting | |
// Simple value: | |
//$data['baseSalary'] = $salary_float_val; | |
// Or using Google's Structured Data format | |
// @see https://developers.google.com/search/docs/data-types/job-posting | |
// This is the format Google really wants it in, so you should customize this yourself | |
// to match your setup and configuration | |
$data['baseSalary'] = array( | |
'@type' => 'MonetaryAmount', | |
'currency' => 'USD', | |
'value' => array( | |
'@type' => 'QuantitativeValue', | |
'value' => $salary_float_val, | |
// HOUR, DAY, WEEK, MONTH, or YEAR | |
'unitText' => 'YEAR' | |
) | |
); | |
} | |
} | |
return $data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Make sure you check https://developers.google.com/search/docs/data-types/job-posting and match the correct format required