以下是我以前使用过的一种更改帖子类别(或任何术语/分类法)的解决方案,请确保设置正确$post_type,$append,$taxonomy,$terms
<?php
/*
Plugin Name: Simple Ajax set cat
Plugin URI: http://en.bainternet.info
Description: dead simple lightweight plugin to add a post row action link to set a post category by ajax
Version: 0.1
Author: Bainternet
Author Email: admin@bainternet.info
License:
Copyright 2013 Bainternet (admin@bainternet.info)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* SimpleCat
* @author Ohad Raz <admin@bainternet.info>
*/
class SimpleArchive
{
public $post_type = \'post\';
public $append = false;
public $taxonomy = \'category\';
public $terms = array(1); //you can change this to string of term names
/**
* Class constructor
*
* @author Ohad Raz <admin@bainternet.info>
* @since 0.1
* @param array $args optional for feuture
*/
function __construct($args = array()){
//add row action link
add_filter(\'post_row_actions\',array($this,\'_action_row\'), 10, 2);
//add javascript fp ajax call
add_action(\'admin_footer-edit.php\',array($this,\'addJS\'));
//ajax function
add_action(\'wp_ajax_setToCat\', array($this,\'ajaxSetToCat\'));
}
/**
* adds an action link to post listing row
* @author Ohad Raz <admin@bainternet.info>
* @since 0.1
* @param array $actions row actions
* @param onject $post Post object
* @return array modified actions
*/
function _action_row($actions, $post){
//check for your post type
if ($post->post_type == $this->post_type){
$actions[\'archive\'] = \'<a href="#" class="move_TO_CAT" data-pid="\'.$post->ID.\'">\'.__(\'Archive\').\'</a>\';
}
return $actions;
}
/**
* Adds the javascript magic to make the ajax request
*
* @author Ohad Raz <admin@bainternet.info>
* @since 0.1
* @return void
*/
function addJS(){
wp_enqueue_script( \'jquery\');
?>
<div id="status_update_working" style="background-color: green; color: #fff; font-wieght: bolder; font-size: 22px; height: 33px; left: 40%; padding: 35px; position: fixed; top: 100px; width: 350px; display:none !important; "><?php _e(\'Changing status...\'); ?></div>
<script type="text/javascript">
jQuery(document).ready(function($){
function ajaxSetCat(p){
jQuery("#status_update_working").show(\'fast\');
jQuery.getJSON(ajaxurl,
{ pid: p.attr("data-pid"),
action: "setToCat",
_nonce: "<?php echo wp_create_nonce(\'setToCat\'); ?>"
},
function(data) {
if (data.error){
alert(data.error);
}else{
alert(data.text);
}
}
);
jQuery("#status_update_working").hide(\'9500\');
}
$(\'.move_TO_CAT\').click(function(){
ajaxSetCat($(this))
});
});
</script>
<?php
}
/**
* ajax callback function to actually set the category
*
* @author Ohad Raz <admin@bainternet.info>
* @since 0.1
* @return void
*/
function ajaxSetToCat(){
//minor validation
if (!isset($_GET[\'pid\']) || ! wp_verify_nonce($_GET[\'_nonce\'], \'setToCat\')){
$re[\'error\'] = __(\'something went wrong ...\');
echo json_encode($re);
die();
}
$results = wp_set_post_terms( intval($_GET[\'pid\']), $this->terms, $this->taxonomy, $this->append );
if ( is_wp_error( $results ) ){
$re[\'error\'] = __(\'something went wrong ...\') ." ". $results->get_error_message();
}elseif($results === false || !is_array($results)){
$re[\'error\'] = __(\'something went wrong ...\');
}else{
$re[\'text\'] = __(\'Set to cat successful\');
}
echo json_encode($re);
die();
}
}//end class
new SimpleArchive();