Tutorial: Adding a salary field for jobs

This tutorial shows how to edit fields based on the Editing Job Submission fields doc and then output it on a single job listing.

↑ Back to Top Add the field to the frontend

Open up your theme functions.php field and create a function to append a new field to the jobs section. First hook it in:

add_filter( 'submit_job_form_fields', 'frontend_add_salary_field' );

Then write the function:

function frontend_add_salary_field( $fields ) {
  $fields['job']['job_salary'] = array(
    'label'       => __( 'Salary ($)', 'job_manager' ),
    'type'        => 'text',
    'required'    => true,
    'placeholder' => 'e.g. 20000',
    'priority'    => 7
  );
  return $fields;
}

This adds a salary text field at the bottom of the jobs form that has the label “Salary”, is required, has no placeholder, and is ordered in 7th position.

The salary field

The salary field

Fields added using the above code will be saved to the job listing automatically.

↑ Back to Top Add the field to admin

Again in theme functions.php, hook in your custom function:

add_filter( 'job_manager_job_listing_data_fields', 'admin_add_salary_field' );

Then write your custom function:

function admin_add_salary_field( $fields ) {
  $fields['_job_salary'] = array(
    'label'       => __( 'Salary ($)', 'job_manager' ),
    'type'        => 'text',
    'placeholder' => 'e.g. 20000',
    'description' => ''
  );
  return $fields;
}

This adds a text field to the admin meta box named “salary”.

The admin salary field

The admin salary field

Note, the field name is prepended with a ‘_’. This is because Job Manager makes your new fields hidden meta by prepending theme with an underscore. This is normal.

↑ Back to Top Display "Salary" on the single job page

Our final task is to display the salary somewhere. There are two ways we can do this:

  1. We can override the template for the single job listing and output the meta data there using get_post_meta().
  2. We can use another filter and display the meta via our own function.

In this tutorial we’ll do it using option #2 and add the salary underneath the job title.

First we need to again hook our function into the correct location. This time we use an action hook:

add_action( 'single_job_listing_meta_end', 'display_job_salary_data' );

Then we write a function which gets the value of the meta and outputs it in list format:

function display_job_salary_data() {
  global $post;

  $salary = get_post_meta( $post->ID, '_job_salary', true );

  if ( $salary ) {
    echo '<li>' . __( 'Salary:' ) . ' $' . esc_html( $salary ) . '</li>';
  }
}

Now when we view a job with a salary entered we will see:

2014-03-09 at 12.54

↑ Back to Top Adding a Salary Filter to the Job Search Form (advanced)

It is possible to add new filters to the search form by adding a field and then modifying the search queries using filters. The below example adds a dropdown ‘salary’ field, which then filters the listings based on pre-defined ranges. This only works with numeric values and requires Job Manager 1.23.6 to work correctly.