• Resolved kivo7

    (@kivo7)


    Hi,

    I am trying to show the new filed ‘job_experience’ which I have created via your tutorial. But it is not text value but array.

    add_filter( 'job_manager_job_listing_data_fields', 'admin_add_experience_field' );
    function admin_add_experience_field( $fields ) {
      $fields['_job_exprience'] = array(
        'label'       => __( 'Опыт', 'job_manager' ),
        'type'        => 'select',
        'required'    => false,
        'priority'    => 8,
        'options' => array(
                           'option[1]' => 'no experience',  // 'value'=>'label'
                           'option[2]' => '1 year',
                           'option[3]' => '2 year',
                           'option[4]' => '3 year' )
      );
      return $fields;
    }

    To show on the job page I use this code:

    add_action( 'single_job_listing_meta_end', 'display_job_experience_data' );
    function display_job_experience_data() {
      global $post;
    
      $experience = get_post_meta( $post->ID, '_job_experience', true );
    
      if ( $experience ) {
        echo '<li>' . __( 'Experience: ' ) . esc_html( $experience ) . '</li>';
      }
    }

    But it returns only key of array like ‘option[1]’, not a value ‘no experience’. What should I change in code to solve this problem?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Davor

    (@davoraltman)

    Hi there,

    Please use the filter submit_job_form_fields to add a new field. For WP Admin area, please use job_manager_job_listing_data_fields. For details on this, check out our tutorial here:

    https://wpjobmanager.com/document/editing-job-submission-fields/

    Also, I see that you missed an e in $fields['_job_exprience'] in the first snippet.

    Let me know how it goes.

    Thanks,
    Davor

    Thread Starter kivo7

    (@kivo7)

    Hi, Davor

    Actually I read your tutorial several times and even made some fields with succes. But there were textarea fields, not selectboxes.

    My full code is:

    //** Add field "job_experience" to the frontend
    add_filter( 'submit_job_form_fields', 'frontend_add_experience_field' );
    function frontend_add_experience_field( $fields ) {
      $fields['job']['job_experience'] = array(
        'label'       => __( 'Experience' ),
        'type'        => 'select',
        'required'    => false,
        'priority'    => 8,
        'options' => array(
                           'option1' => 'no experience',  // 'value'=>'label'
                           'option2' => '1 year',
                           'option3' => '2 years',
                           'option4' => '5 years' )
      );
     
      return $fields;
    }
    
    //** Add field "job_experience" to admin
    add_filter( 'job_manager_job_listing_data_fields', 'admin_add_experience_field' );
    function admin_add_experience_field( $fields ) {
      $fields['_job_experience'] = array(
        'label'       => __( 'Experience' ),
        'type'        => 'select',
        'required'    => false,
        'priority'    => 8,
        'options' => array(
                           'option1' => 'no experience',  // 'value'=>'label'
                           'option2' => '1 year',
                           'option3' => '2 years',
                           'option4' => '5 years' )
      );
      return $fields;
    }
    
    //** Display "job_experience" on the single job page
    add_action( 'single_job_listing_meta_end', 'display_job_experience_data' );
    function display_job_experience_data() {
      global $post;
    
      $experience = get_post_meta( $post->ID, '_job_experience', true );
    
      if ( $experience ) {
        echo '<li>' . __( 'Experience: ' ) . esc_html( $experience ) . '</li>';
      }
    }

    Please let me know how to display the label of my filed on the single job page. Now I have only the value: http://prntscr.com/ggx9a2 . But I need to display not “option1”, but “no experience”.

    Hi there,

    Thanks for your screenshots and the code.

    That’s reasonable because you’re saving option1 in your database. The following code is pulling that value only.

    $experience = get_post_meta( $post->ID, '_job_experience', true );

    To resolve this, you can do something like this.

    Change the code

    //** Display "job_experience" on the single job page
    add_action( 'single_job_listing_meta_end', 'display_job_experience_data' );
    function display_job_experience_data() {
      global $post;
    
      $experience = get_post_meta( $post->ID, '_job_experience', true );
    
      if ( $experience ) {
        echo '<li>' . __( 'Experience: ' ) . esc_html( $experience ) . '</li>';
      }
    }

    to

    //** Display "job_experience" on the single job page
    add_action( 'single_job_listing_meta_end', 'display_job_experience_data' );
    function display_job_experience_data() {
      global $post;
    
      $value_label =  array(
                           'option1' => 'no experience',  // 'value'=>'label'
                           'option2' => '1 year',
                           'option3' => '2 years',
                           'option4' => '5 years' )
      );
    
      $experience_value = get_post_meta( $post->ID, '_job_experience', true );
      $experience = $value_label[ 'experience_value' ]; // Get label
    
      if ( $experience ) {
        echo '<li>' . __( 'Experience: ' ) . esc_html( $experience ) . '</li>';
      }
    }

    Please note that: this is just an example. You might handle your code in some cases like there is no value, etc. If not, your code might break somehow.

    Cheers,

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Adding extra fields to WPJM’ is closed to new replies.