Home » Php » php – Overwrite the WP Job Manager category list Walker

php – Overwrite the WP Job Manager category list Walker

Posted by: admin February 25, 2020 Leave a comment

Questions:

I wanted to set category parent for the “job_listing_category” taxonomy as optgroup if it has children. Like this!

I managed to fix this by replacing the WP job manager plugin core file class-wp-job-manager-category-walker.php with:

<?php
/**
 * File containing the class WP_Job_Manager_Category_Walker.
 *
 * @package wp-job-manager
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Walks through categories.
 *
 * @extends Walker
 * @package wp-job-manager
 * @since 1.0.0
 */
class My_WP_Job_Manager_Category_Walker extends WP_Job_Manager_Category_Walker {

    /**
     * Tree type that the class handles.
     *
     * @var string
     */
    public $tree_type = 'category';

    /**
     * Database fields to use.
     *
     * @var array
     */
    public $db_fields = [
        'parent' => 'parent',
        'id'     => 'term_id',
        'slug'   => 'slug',
    ];


  function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {

    $pad = str_repeat('&nbsp;', $depth * 3);



    $cat_name = apply_filters('list_cats', $category->name, $category);
    if ('list' == $args['has_children'] && $args['taxonomy'] == 'job_listing_category'){
//    $this->optgroup = true;
      $output .= "<optgroup class=\"level-$depth\" label=\"".$cat_name."\" >";
    } else {
      $output .= "<option class=\"level-$depth\" value=\"".$category->term_id."\"";
      if ( $category->term_id == $args['selected'] )
              $output .= ' selected="selected"';
      $output .= '>';
      $output .= $pad.$cat_name;
      if ( $args['show_count'] )
              $output .= '&nbsp;&nbsp;('. $category->count .')';
      $output .= "</option>";
    }

  }

  function end_el(&$output,$object,$depth) {
    if ('list' == $args['has_children'] && $args['taxonomy'] == 'job_listing_category') {
      $output .= '</optgroup>';
    }
  }
}

The problem I now face is the fact that this can be overwritten whenever the WP job admin plugin will be updated!

Therefor I tried to follow this suggestion in order to fix this problem, so far none of the suggestions seems to work. I might have the wrong filter.

function wp_job_manager_optgroup_parent_category( $args ){

       // include new walker class - where ever you saved/named it
       //include_once( 'wp-job-manager/class-wp-job-manager-categroy-walker.php' ); 
       // set the name as the new walker for the widget args
       $args['walker'] = new My_WP_Job_Manager_Category_Walker;
       return $args;
}
add_filter( 'job_manager_job_filters_search_jobs_start', 'wp_job_manager_optgroup_parent_category' );

have also tried the following filters:

  • job_manager_job_filters_search_jobs_start
  • job_manager_job_filters_before
  • job_manager_term_multiselect_field_args
  • job_manager_dropdown_categories

Any suggestions on how to fix this?

How to&Answers: