Last active
January 4, 2016 21:19
-
-
Save pommiegranit/8679843 to your computer and use it in GitHub Desktop.
A plugin that adds a new "remove terms" action to the All Posts Bulk Actions menu to allow categories and terms to be removed from multiple posts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Bulk Term Remover | |
Plugin URI: | |
Description: Remove tags or categories from multiple posts in the post list screen | |
Author: Chris Knowles based on plugin by Justin Stern. | |
Author URI: http://premium.wpmudev.org | |
Version: 0.1 | |
Original URL: http://www.foxrunsoftware.net/articles/wordpress/add-custom-bulk-action/ | |
Original Author: Justin Stern | |
Original Author URI: http://www.foxrunsoftware.net | |
Original Licence Info: | |
Copyright: © 2012 Justin Stern (email : justin@foxrunsoftware.net) | |
License: GNU General Public License v3.0 | |
License URI: http://www.gnu.org/licenses/gpl-3.0.html | |
*/ | |
if ( is_admin() ) { | |
add_action('admin_footer-edit.php' , 'custom_bulk_admin_footer' ); | |
add_action('load-edit.php' , 'custom_bulk_action' ); | |
add_action('admin_notices', 'custom_bulk_admin_notices' ); | |
} | |
function custom_bulk_admin_footer() { | |
global $post_type; | |
if($post_type == 'post') { | |
?> | |
<script type="text/javascript"> | |
jQuery(document).ready(function() { | |
jQuery('<option>').val('remove').text('<?php _e('Remove Terms')?>').appendTo("select[name='action']"); | |
jQuery('input#doaction').before('<input type="text" name="action_tags" value="Enter tags">'); | |
jQuery('input#doaction').before('<select name="action_cats" id="action_cats"><option value="0">Select categories</option></select>'); | |
jQuery('select#cat option[value!=0]').clone().appendTo('select#action_cats'); | |
}); | |
</script> | |
<?php | |
} | |
} | |
function custom_bulk_action() { | |
global $typenow; | |
$post_type = $typenow; | |
if ( $post_type == 'post' ) { | |
// get the action | |
$wp_list_table = _get_list_table( 'WP_Posts_List_Table' ); | |
$action = $wp_list_table->current_action(); | |
// we have a rogue action | |
if ( $action != 'remove' ) return; | |
// security check | |
check_admin_referer( 'bulk-posts' ); | |
// set the tags and categories | |
$tags = ( $_REQUEST['action_tags'] == 'Enter tags' ) ? '' : $_REQUEST['action_tags']; | |
$cats = ( $_REQUEST['action_cats'] == '0' ) ? '' : $_REQUEST['action_cats']; | |
if ( $tags == '' && $cats == '' ) return; | |
// make sure ids are submitted. depending on the resource type, this may be 'media' or 'ids' | |
if ( isset( $_REQUEST['post'] ) ) { | |
$post_ids = array_map( 'intval' , $_REQUEST['post'] ); | |
} | |
// no ids selected | |
if ( empty( $post_ids ) ) return; | |
// this is based on wp-admin/edit.php | |
$sendback = remove_query_arg( array('tagged', 'untrashed', 'deleted', 'ids'), wp_get_referer() ); | |
if ( ! $sendback ) $sendback = admin_url( 'edit.php?post_type=$post_type' ); | |
$pagenum = $wp_list_table->get_pagenum(); | |
$sendback = add_query_arg( 'paged', $pagenum , $sendback ); | |
$tagged = 0; | |
foreach( $post_ids as $post_id ) { | |
if ( $cats != '' ) { | |
if ( remove_terms( $post_id , $cats , 'category' ) ) $tagged++; | |
} | |
if ( $tags != '' ) { | |
if ( remove_terms( $post_id , $tags , 'post_tag' ) ) $tagged++; | |
} | |
} | |
$sendback = add_query_arg( array('tagged' => $tagged, 'ids' => join(',', $post_ids) ), $sendback ); | |
$sendback = remove_query_arg( array('action', 'action_tags', 'action_cats' , 'post_author', 'comment_status', 'ping_status', '_status', 'post', 'bulk_edit', 'post_view'), $sendback ); | |
wp_redirect($sendback); | |
exit(); | |
} //if | |
} | |
function custom_bulk_admin_notices() { | |
global $post_type, $pagenow; | |
if( $pagenow == 'edit.php' && $post_type == 'post' && isset($_REQUEST['tagged']) && (int) $_REQUEST['tagged'] ) { | |
$message = sprintf( _n( 'Post updated.', '%s posts updated.', (int) $_REQUEST['tagged'] ), number_format_i18n( $_REQUEST['tagged'] ) ); | |
echo "<div class=\"updated\"><p>{$message}</p></div>"; | |
} | |
} | |
function remove_terms( $post_id , $the_terms , $taxonomy ) { | |
// as there is no function to remove tags, we need to make a list of current tags, | |
// remove the passed tags from the list and then reapply | |
// turn $tags into an array | |
$term_list = explode( ',', trim( $the_terms , " \n\t\r\0\x0B," ) ); | |
$fields = ( $taxonomy == 'category' ) ? 'ids' : 'names'; | |
// get current tags | |
$post_terms = wp_get_post_terms( $post_id , $taxonomy , array( 'fields' => $fields ) ); | |
// remove passed tags from current tags | |
$post_terms = array_diff( $post_terms , $term_list ); | |
// assign new list | |
$result = wp_set_post_terms( $post_id , $post_terms , $taxonomy , false ); | |
if ( is_array( $result ) ) { | |
return true; | |
} else { | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment