Output custom field values using PHP

To output any custom fields you have created you can either use Shortcodes or one of the following PHP functions to output the custom field values.

Right now there are multiple functions available, one for job fields, company fields, resume fields, etc. This is for any future enhancements, but currently all functions do the same thing (so technically you could use get_resume_field and with details for a job listing and it will still display right. My Suggestion is to use job, company, or resume for the specific field, and if you are not sure then you can use custom field function.

You can find all of these functions and details on them in the includes/functions.php file under the wp-job-manager-field-editor plugin directory.

I also recommending looking through my Gist on GitHub as this is where I frequently post examples (PHP, JS, etc) for support tickets and I may already have some example code you need:

https://gist.github.com/tripflex

Available Functions


get_job_field / the_job_field

get_job_field( $field_slug, $job_id, $args ) or the_job_field( $field_slug, $job_id, $args )

  • $field_slug is the meta key for the field
  • $job_id optional The job ID you want to get the value from, if $job_id is not specified it will use the current job listing.
  • $args optional Array with key => value configuration (can be any configuration attribute available in shortcodes). See below for more details.
  • Using get_job_field will return the value, using the_job_field will echo/print out the field.

get_company_field / the _company_field

get_company_field( $field_slug, $job_id, $args ) or the_company_field( $field_slug, $job_id, $args )

  • $field_slug is the meta key for the field
  • $job_id optional The job ID you want to get the value from, if $job_id is not specified it will use the current job listing.
  • $args optional Array with key => value configuration (can be any configuration attribute available in shortcodes). See below for more details.
  • Using get_company_field will return the value, using the_company_field will echo/print out the field.

get_resume_field / the_resume_field

get_resume_field( $field_slug, $resume_id, $args ) or the_resume_field( $field_slug, $resume_id, $args )

  • $field_slug is the meta key for the field
  • $resume_id optional The resume ID you want to get the value from, if $resume_id is not specified it will use the current resume listing.
  • Using get_resume_field will return the value, using the_resume_field will echo/print out the field.

get_custom_field / the_custom_field

get_custom_field( $field_slug, $post_id, $args ) or the_custom_field( $field_slug, $post_id, $args )

  • $field_slug is the meta key for the field
  • $post_id optional The resume/job ID you want to get the value from, if not specified it will use the current resume/job listing.
  • $args optional Array with key => value configuration (can be any configuration attribute available in shortcodes). See below for more details.
  • Using get_custom_field will return the value, using the_custom_field will echo/print out the field.

Example Usage

Say I wanted to output a new custom field I created with a meta key of job_salary, this is the PHP code you would add to your job listing template file:

Using `get_job_field`

$salary = get_job_field( 'job_salary' );
echo $salary;

Or you could simply use the_job_field function instead:

Using the_job_field

the_job_field( 'job_salary' );

Both of the examples above would output the value of the field if it was set for the job listing. This same method can be used for company, resumes, or any other custom fields.

Show image from custom upload field

$image_url = get_company_field( 'company_office_photo' );
echo '<img src="' . $image_url '" />';

 

Passing $args configurations

You can pass any configuration arguments/attributes available in shortcodes, when calling PHP functions.  If you do not want to define the listing ID, and want to force my plugin to automagically detect the listing id, just pass null to the listing id, like so:

the_job_field( 'some_job_field', null, array( 'output_as' => 'text' ) );

As you can see above, just pass null as the second parameter, and then in the third parameter you can define configurations.

Here’s a few more examples for reference:

Output “another_custom_field” showing label, and with full wrapper HTML element enabled

the_custom_field( 'another_custom_field', null, array( 'output_show_label' => true, 'output_enable_fw' => true ) );

Define arguments configuration in separate variable

$website_link_args = array(
    'output_as' => 'link',
    'output_show_label' => true,
    'output_classes' => 'some-class another-one',
);

the_custom_field( 'website_link', null, $website_link_args );

 

Total 24 Votes

Tell us how can we improve this post?

+ = Verify Human or Spambot ?