Custom Fields Rendering

WPJobBoard has pretty powerful Custom Fields system, but sometimes you might want to display data in the frontend (on job/resumes/company details page)  differently then on the form, for example you might want the custom url user enters in the form to display as clickable link or maybe as a YouTube video, this article will explain how to do this easily without modifying original files.

This will require some programming, all the code that will be shown below you can either add to your theme functions.php file or even better create a new WordPress plugin and put the code there.

First thing we will need is to go to wp-admin / Settings (WPJB) / Custom Fields / Job Form panel, and add a Textbox and name it for example custom_url, as on the screen below:

wpjb-custom-renderer-cf

 

Now save the form and go to wp-admin / Job Board / Jobs panel, edit one of existing jobs, fill the “Custom URL” field and save the job. This is technically optional step but we want to have some data to test on.

Of course if you already have a custom field created you can skip this step.

 Custom Renderers (requires WPJobBoard 4.2.1)

First, we need to create wpjb_scheme filter that will add custom rendering function for “custom_url” field, this requires pretty basic PHP code and almost always the code will be the same for your custom fields, in the code snippet you will be only changing “custom_url” to your custom field actual name.

add_filter("wpjb_scheme", "my_wpjb_scheme", 10, 2);
function my_wpjb_scheme($scheme, $object) {
 if(isset($object->meta->custom_url)) {
   $scheme["field"]["custom_url"]["render_callback"] = "my_render_as_link";
 }
 return $scheme;
}

Next we need “my_render_as_link” function that will be used to render custom_url on job details page instead of default rendering function.

function my_render_as_link($object) {
 $url = $object->meta->custom_url->value();
 echo sprintf('<a href="%s" target="_blank">%s</a>', esc_attr($url), esc_html($url));
}

Below you can see the result:

wpjb-custom-renderer-result

 

What if all your URLs will be links pointing to some video or audio files, or YouTube URLs? You will probably want to allow users to play them on site without sending them to third party site, if that’s the case simply modify your my_render_as_link function to look like this:

function my_render_as_link($object) {
 echo wp_video_shortcode(array("src"=>$object->meta->custom_url->value()));
}

That’s it. The code basically executes video shortcode, so you can add some parameters to the function if you want to customize how the video will be played, also note that if you are going to play videos not uploaded to your site there is (for security reasons) curated list of sites from which you can embed content, see more here.

Last trick, if you would made the custom_url, File field instead of Textfield, you could modify your my_render_as_link function like in the example below to play your files on site instead of allowing to download them:

function my_render_as_link($object) {
 echo wp_video_shortcode(array("src"=>$object->file->custom_url[0]->url));
}

 

  1. its Work ! (Under Resumes -youtube Video Interviews).Thank you so much Greg.And you are such a genius.

    • Greg
      Reply

      The code you can add in either your theme functions.php file or even better create a new plugin and paste the code there.

  2. shakti
    Reply

    is it possible to change the apply button on the job page to just a link rather than a form?
    this link needs to point to an external site for some cases.
    how can i achieve that?

  3. pete
    Reply

    Do you have an example for displaying either label or value for the custom field? “if field exists” echo “field value”…

    • Greg
      Reply

      This is not really possible i am afraid, if the value is not provided then the whole row will not display at all. In this case the only way to display the field would be to use wpjb_template_job_meta_text filter


      add_filter( "wpjb_template_job_meta_text", "my_wpjb_template_job_meta_text" );
      function my_wpjb_template_job_meta_text( $job ) {
      ?>
      <div class="wpjb-grid-row">
      <div class="wpjb-grid-col wpjb-col-35">Label</div>
      <div class="wpjb-grid-col wpjb-col-60">
      <?php if( $job->meta->custom_field_name->value() ): ?>
      <?php echo esc_html( $job->meta->custom_field_name->value() ) ?>
      <?php else: ?>
      No Value Provided
      <?php endif; ?>
      </div>
      </div>
      <?php
      }

      In the custom fields editor you would also need to make this field visible in Forms Only, and of course in the code change the custom_field_name to actual custom field name you are using.

Leave a Reply

Your email address will not be published.