Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Function code cleanup WordPress

  • SOLVED

I have put together some function code according to a tutorial, which adds custom taxonomy filter columns to the custom post list screen.

It works really well, however, I have multiple filters and I have had to repeat the function for each taxonomy I want to filter by.

This project is very time sensitive so I don't have the luxury of messing around with it, but I think it could be much more efficient. I'd like if possible for this to be combined into one function using arrays for the taxonomies. The code is below:

/* -------------------- Customise Job Listing admin --------------------- */

//Filter by level

add_filter('manage_job_posts_columns', 'add_levels_column_to_job_list');
function add_levels_column_to_job_list( $posts_columns ) {
if (!isset($posts_columns['author'])) {
$new_posts_columns = $posts_columns;
} else {
$new_posts_columns = array();
$index = 0;
foreach($posts_columns as $key => $posts_column) {
if ($key=='author') {
$new_posts_columns['levels'] = null;
}
$new_posts_columns[$key] = $posts_column;
}
}
$new_posts_columns['levels'] = 'Level';
return $new_posts_columns;
}

add_action('manage_posts_custom_column', 'show_levels_column_for_job_list',10,2);
function show_levels_column_for_job_list( $column_id,$post_id ) {
global $typenow;
if ($typenow=='job') {
$taxonomy = 'level';
switch ($column_id) {
case 'levels':
$levels = get_the_terms($post_id,$taxonomy);
if (is_array($levels)) {
foreach($levels as $key => $level) {
$edit_link = get_term_link($level,$taxonomy);
$levels[$key] = '<a href="'.$edit_link.'">' . $level->name . '</a>';
}
echo implode(' | ',$levels);
}
break;
}
}
}


add_action('restrict_manage_posts','restrict_jobs_by_level');
function restrict_jobs_by_level() {
global $typenow;
global $wp_query;
if ($typenow=='job') {
$taxonomy = 'level';
$level_taxonomy = get_taxonomy($taxonomy);
wp_dropdown_categories(array(
'show_option_all' => __("Show All {$level_taxonomy->label}s"),
'taxonomy' => $taxonomy,
'name' => 'level',
'orderby' => 'name',
'selected' => $wp_query->query['term'],
'hierarchical' => true,
'depth' => 3,
'show_count' => true, // This will give a view
'hide_empty' => true, // This will give false positives, i.e. one's not empty related to the other terms. TODO: Fix that
));
}
}

add_filter('parse_query','convert_level_id_to_taxonomy_term_in_query');
function convert_level_id_to_taxonomy_term_in_query($query) {
global $pagenow;
$qv = &$query->query_vars;
if ($pagenow=='edit.php' &&
isset($qv['taxonomy']) && $qv['taxonomy']=='level' &&
isset($qv['term']) && is_numeric($qv['term'])) {
$term = get_term_by('id',$qv['term'],'level');
$qv['term'] = $term->slug;
}
}


//Filter by jobterm

add_filter('manage_job_posts_columns', 'add_jobterms_column_to_job_list');
function add_jobterms_column_to_job_list( $posts_columns ) {
if (!isset($posts_columns['author'])) {
$new_posts_columns = $posts_columns;
} else {
$new_posts_columns = array();
$index = 0;
foreach($posts_columns as $key => $posts_column) {
if ($key=='author') {
$new_posts_columns['jobterms'] = null;
}
$new_posts_columns[$key] = $posts_column;
}
}
$new_posts_columns['jobterms'] = 'Job term';
return $new_posts_columns;
}

add_action('manage_posts_custom_column', 'show_jobterms_column_for_job_list',10,2);
function show_jobterms_column_for_job_list( $column_id,$post_id ) {
global $typenow;
if ($typenow=='job') {
$taxonomy = 'jobterm';
switch ($column_id) {
case 'jobterms':
$jobterms = get_the_terms($post_id,$taxonomy);
if (is_array($jobterms)) {
foreach($jobterms as $key => $jobterm) {
$edit_link = get_term_link($jobterm,$taxonomy);
$jobterms[$key] = '<a href="'.$edit_link.'">' . $jobterm->name . '</a>';
}
echo implode(' | ',$jobterms);
}
break;
}
}
}


add_action('restrict_manage_posts','restrict_jobs_by_jobterm');
function restrict_jobs_by_jobterm() {
global $typenow;
global $wp_query;
if ($typenow=='job') {
$taxonomy = 'jobterm';
$jobterm_taxonomy = get_taxonomy($taxonomy);
wp_dropdown_categories(array(
'show_option_all' => __("Permanent and Temp"),
'taxonomy' => $taxonomy,
'name' => 'jobterm',
'orderby' => 'name',
'selected' => $wp_query->query['term'],
'hierarchical' => true,
'depth' => 3,
'show_count' => true, // This will give a view
'hide_empty' => true, // This will give false positives, i.e. one's not empty related to the other terms. TODO: Fix that
));
}
}

add_filter('parse_query','convert_jobterm_id_to_taxonomy_term_in_query');
function convert_jobterm_id_to_taxonomy_term_in_query($query) {
global $pagenow;
$qv = &$query->query_vars;
if ($pagenow=='edit.php' &&
isset($qv['taxonomy']) && $qv['taxonomy']=='jobterm' &&
isset($qv['term']) && is_numeric($qv['term'])) {
$term = get_term_by('id',$qv['term'],'jobterm');
$qv['term'] = $term->slug;
}
}


//Filter by Client

add_filter('manage_job_posts_columns', 'add_clients_column_to_job_list');
function add_clients_column_to_job_list( $posts_columns ) {
if (!isset($posts_columns['author'])) {
$new_posts_columns = $posts_columns;
} else {
$new_posts_columns = array();
$index = 0;
foreach($posts_columns as $key => $posts_column) {
if ($key=='author') {
$new_posts_columns['clients'] = null;
}
$new_posts_columns[$key] = $posts_column;
}
}
$new_posts_columns['clients'] = 'Client';
return $new_posts_columns;
}

add_action('manage_posts_custom_column', 'show_clients_column_for_job_list',10,2);
function show_clients_column_for_job_list( $column_id,$post_id ) {
global $typenow;
if ($typenow=='job') {
$taxonomy = 'client';
switch ($column_id) {
case 'clients':
$clients = get_the_terms($post_id,$taxonomy);
if (is_array($clients)) {
foreach($clients as $key => $client) {
$edit_link = get_term_link($client,$taxonomy);
$clients[$key] = '<a href="'.$edit_link.'">' . $client->name . '</a>';
}
echo implode(' | ',$clients);
}
break;
}
}
}


add_action('restrict_manage_posts','restrict_jobs_by_client');
function restrict_jobs_by_client() {
global $typenow;
global $wp_query;
if ($typenow=='job') {
$taxonomy = 'client';
$client_taxonomy = get_taxonomy($taxonomy);
wp_dropdown_categories(array(
'show_option_all' => __("Show All {$client_taxonomy->label}"),
'taxonomy' => $taxonomy,
'name' => 'client',
'orderby' => 'name',
'selected' => $wp_query->query['term'],
'hierarchical' => true,
'depth' => 3,
'show_count' => true, // This will give a view
'hide_empty' => true, // This will give false positives, i.e. one's not empty related to the other terms. TODO: Fix that
));
}
}

add_filter('parse_query','convert_client_id_to_taxonomy_term_in_query');
function convert_client_id_to_taxonomy_term_in_query($query) {
global $pagenow;
$qv = &$query->query_vars;
if ($pagenow=='edit.php' &&
isset($qv['taxonomy']) && $qv['taxonomy']=='client' &&
isset($qv['term']) && is_numeric($qv['term'])) {
$term = get_term_by('id',$qv['term'],'client');
$qv['term'] = $term->slug;
}
}

//Filter by sector

add_filter('manage_job_posts_columns', 'add_sectors_column_to_job_list');
function add_sectors_column_to_job_list( $posts_columns ) {
if (!isset($posts_columns['author'])) {
$new_posts_columns = $posts_columns;
} else {
$new_posts_columns = array();
$index = 0;
foreach($posts_columns as $key => $posts_column) {
if ($key=='author') {
$new_posts_columns['sectors'] = null;
}
$new_posts_columns[$key] = $posts_column;
}
}
$new_posts_columns['sectors'] = 'Sector';
return $new_posts_columns;
}

add_action('manage_posts_custom_column', 'show_sectors_column_for_job_list',10,2);
function show_sectors_column_for_job_list( $column_id,$post_id ) {
global $typenow;
if ($typenow=='job') {
$taxonomy = 'sector';
switch ($column_id) {
case 'sectors':
$sectors = get_the_terms($post_id,$taxonomy);
if (is_array($sectors)) {
foreach($sectors as $key => $sector) {
$edit_link = get_term_link($sector,$taxonomy);
$sectors[$key] = '<a href="'.$edit_link.'">' . $sector->name . '</a>';
}
echo implode(' | ',$sectors);
}
break;
}
}
}


add_action('restrict_manage_posts','restrict_jobs_by_sector');
function restrict_jobs_by_sector() {
global $typenow;
global $wp_query;
if ($typenow=='job') {
$taxonomy = 'sector';
$sector_taxonomy = get_taxonomy($taxonomy);
wp_dropdown_categories(array(
'show_option_all' => __("Show All {$sector_taxonomy->label}"),
'taxonomy' => $taxonomy,
'name' => 'sector',
'orderby' => 'name',
'selected' => $wp_query->query['term'],
'hierarchical' => true,
'depth' => 3,
'show_count' => true, // This will give a view
'hide_empty' => true, // This will give false positives, i.e. one's not empty related to the other terms. TODO: Fix that
));
}
}

add_filter('parse_query','convert_sector_id_to_taxonomy_term_in_query');
function convert_sector_id_to_taxonomy_term_in_query($query) {
global $pagenow;
$qv = &$query->query_vars;
if ($pagenow=='edit.php' &&
isset($qv['taxonomy']) && $qv['taxonomy']=='sector' &&
isset($qv['term']) && is_numeric($qv['term'])) {
$term = get_term_by('id',$qv['term'],'sector');
$qv['term'] = $term->slug;
}
}

Answers (1)

2011-02-07

John Cotton answers:

Hi James

Well, it's just a case of going through your functions and making them a bit more generic.

So, for example, all your parse_query hooks could be rewritten into one like this:



add_filter('parse_query','convert_tax_id_to_tax_term_in_query');
function convert_tax_id_to_tax_term_in_query($query) {

$qv = &$query->query_vars;

if( isset($qv['taxonomy']) ) {

if( $qv['taxonomy']=='sector' || $qv['taxonomy']=='client' || $qv['taxonomy']=='client' || $qv['taxonomy']=='jobterm' || $qv['taxonomy']=='level' ) {
global $pagenow;

if ($pagenow=='edit.php' && isset($qv['term']) && is_numeric($qv['term'])) {
$term = get_term_by('id',$qv['term'], $qv['taxonomy']);
$qv['term'] = $term->slug;
}
}
}
}


For the others you'd need to determine which taxonomy you're working with which you could do by, for example, an extra function call like this:



add_action('manage_posts_custom_column', 'show_sectors_column_for_job_list',10,2);

function show_sectors_column_for_job_list( $column_id,$post_id ) {

if ($typenow=='job') {
show_generic_column_for_job_list( $column_id,$post_id, 'sector');
}

}

function show_sectors_column_for_job_list( $column_id,$post_id, $taxonomy ) {

global $typenow;



switch ($column_id) {

case 'sectors':

$terms= get_the_terms($post_id,$taxonomy);

if (is_array($terms)) {

foreach($terms as $key => $term) {

$edit_link = get_term_link($term,$taxonomy);

$terms[$key] = '<a href="'.$edit_link.'">' . $term->name . '</a>';

}

echo implode(' | ',$terms);

}

break;

}

}



...etc

JC