链接答案中的解决方案存在一些问题。但这是一个很好的起点。
基本上你只需要改变方法ajax_change_status()
. 此方法会更改帖子状态,但您希望更改帖子元。
简而言之,这是最后一堂课,有一些固定内容
/**
* Plugin Name: Change Book Availability On Post List
* Plugin URI: http://wordpress.stackexchange.com/questions/98295/change-post-meta-via-ajax-from-the-posts-list-table
* Description: Changing the status of availability in post list
* Version: 0.1
* Author: Ralf Albert
* Author URI: http://yoda.neun12.de
* License: GPLv3
*/
if ( ! class_exists( \'Ajax_Book_Status\' ) ) {
class Ajax_Book_Status
{
public $metakey = \'book_status\';
//constarctor
public function __construct() {
global $pagenow,$typenow; //&& $typenow ==\'page\'
if ( is_admin() && $pagenow==\'edit.php\' )
add_filter(\'admin_footer\',array( $this,\'insert_ajax_status_script\' ) );
add_filter( \'manage_edit-post_columns\', array( $this,\'add_new_columns\' ) );
add_action( \'manage_post_posts_custom_column\', array( $this, \'manage_columns\' ), 10, 2 );
//manage columns
add_filter( \'manage_pages_columns\', array( $this,\'add_new_columns\' ) );
add_action( \'manage_pages_custom_column\', array( $this, \'manage_columns\' ), 10, 2 );
//ajax function
add_action( \'wp_ajax_change_book_status\', array( $this,\'ajax_change_book_status\' ) );
}
/**
* the function that will actually change the post status
*
* $post_id - The ID of the post you\'d like to change.
* $status - The post status yes|no
*/
public function ajax_change_book_status(){
$result = \'\';
$new_postmeta = \'\';
$postid = filter_input( INPUT_GET, \'post_id\', FILTER_SANITIZE_NUMBER_INT );
if ( ! isset( $postid ) )
$result = \'something went wrong ...\';
$old_postmeta = (string) get_post_meta( $postid, $this->metakey, true );
// if no status was saved, predefine the new status to no.
if ( ! in_array( $old_postmeta, array( \'yes\', \'no\' ) ) ) {
$old_postmeta = \'no\';
}
$new_postmeta = ( $old_postmeta === \'yes\' ) ?
\'no\' : \'yes\';
if ( empty( $result ) )
$result = \'change status to \' . $new_postmeta;
update_post_meta( $postid, $this->metakey, $new_postmeta, $old_postmeta );
header( \'Content-type: application/json\' );
die( json_encode( array( \'data\' => $result, \'text\' => ucfirst( $new_postmeta ) ) ) );
}
/*
****************************
* manage columns functions *
****************************
*/
//add new columns function
public function add_new_columns( $columns ){
$columns[\'status\']= __(\'Status\');
return $columns;
}
//render columns function
public function manage_columns( $column_name, $id ) {
// bail if it is not our column
if ( \'status\' !== $column_name )
return;
$status = get_post_meta( $id, $this->metakey, true );
if ( ! in_array( $status, array( \'yes\', \'no\' ) ) )
$status = \'no\';
printf( \'<div class="book-status"><a href="#" class="book-availability" pid="%d">%s</a></div>\', $id, ucfirst( $status ) );
}
//js/jquery code to call ajax
public function insert_ajax_status_script(){
?>
<script type="text/javascript">
jQuery(document).ready(
function($){
$( \'.book-availability\' ).click(
function(){
link = $(this);
$.get(
ajaxurl,
{
\'action\' : \'change_book_status\',
\'post_id\' : $(this).attr( \'pid\' )
},
function(result){
if( \'\' !== result.text )
link.text( result.text );
else
alert( result.data );
}
);
}
);
}
);
</script>
<?php
}
}
}
add_action( \'plugins_loaded\', \'bookstatuscolumn\' );
function bookstatuscolumn(){
new Ajax_Book_Status();
}
如您所见,插入新列的部分几乎未被触及。只是一些调整。
基本更改是注册方法ajax_change_book_status()
作为ajax回调。该方法请求Posteta,在yes
和no
并存储新状态。仅此而已。
您必须调整metakey
计算开始时的值到您在自己的Posteta中使用的键。