我正在尝试将自定义“批量操作”添加到WP 3.5新媒体库项目中。我从这里开始excellent tutorial 这正是我所需要的,但有固定的帖子;因此,我尝试将其应用于媒体库项目。
从最初的插件代码开始,下面是我到目前为止(在functions.php中)的想法:
/*
Plugin Name: Custom Bulk Action Demo
Plugin URI: http://www.foxrunsoftware.net/articles/wordpress/add-custom-bulk-action/
Description: A working demonstration of a custom bulk action
Author: Justin Stern
Author URI: http://www.foxrunsoftware.net
Version: 0.1
Copyright: © 2012 Justin Stern (email : [email protected])
License: GNU General Public License v3.0
License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
if(is_admin()) {
// admin actions/filters
add_action(\'admin_footer\', \'custom_bulk_admin_footer\');
add_action(\'load-upload.php\', \'custom_bulk_action\');
add_action(\'admin_notices\', \'custom_bulk_admin_notices\');
}
/**
* Step 1: add the custom Bulk Action to the select menus
*/
function custom_bulk_admin_footer() {
global $pagenow;
if($pagenow == \'upload.php\') {
?>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery(\'<option>\').val(\'appr_1\').text(\'<?php _e(\'Approva per album privato\')?>\').appendTo("select[name=\'action\']");
jQuery(\'<option>\').val(\'appr_1\').text(\'<?php _e(\'Approva per album privato\')?>\').appendTo("select[name=\'action2\']");
});
</script>
<?php
}
}
/**
* Step 2: handle the custom Bulk Action
*
* Based on the post http://wordpress.stackexchange.com/questions/29822/custom-bulk-action
*/
function custom_bulk_action() {
global $typenow,$pagenow;
$post_type = $typenow;
//if($post_type == \'post\') {
if($pagenow == \'upload.php\') {
// get the action
$wp_list_table = _get_list_table(\'WP_Media_List_Table\'); // depending on your resource type this could be WP_Users_List_Table, WP_Comments_List_Table, etc
$action = $wp_list_table->current_action();
$allowed_actions = array("appr_1","appr_2");
if(!in_array($action, $allowed_actions)) return;
// security check
check_admin_referer(\'bulk-posts\');
// 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\']);
}
if(empty($post_ids)) return;
// this is based on wp-admin/edit.php
$sendback = remove_query_arg( array(\'exported\', \'untrashed\', \'deleted\', \'ids\'), wp_get_referer() );
if ( ! $sendback )
//$sendback = admin_url( "edit.php?post_type=$post_type" );
$sendback = admin_url( "post.php" );
$pagenum = $wp_list_table->get_pagenum();
$sendback = add_query_arg( \'paged\', $pagenum, $sendback );
switch($action) {
case \'appr_1\':
// if we set up user permissions/capabilities, the code might look like:
//if ( !current_user_can($post_type_object->cap->export_post, $post_id) )
// wp_die( __(\'You are not allowed to export this post.\') );
$exported = 0;
foreach( $post_ids as $post_id ) {
if ( !$this->perform_export($post_id) )
wp_die( __(\'Error exporting post.\') );
$exported++;
}
$sendback = add_query_arg( array(\'exported\' => $exported, \'ids\' => join(\',\', $post_ids) ), $sendback );
break;
default: return;
}
$sendback = remove_query_arg( array(\'action\', \'action2\', \'tags_input\', \'post_author\', \'comment_status\', \'ping_status\', \'_status\', \'post\', \'bulk_edit\', \'post_view\'), $sendback );
wp_redirect($sendback);
exit();
}
}
/**
* Step 3: display an admin notice on the Posts page after exporting
*/
function custom_bulk_admin_notices() {
global $post_type, $pagenow;
if($pagenow == \'upload.php\' && isset($_REQUEST[\'exported\']) && (int) $_REQUEST[\'exported\']) {
$message = sprintf( _n( \'Post exported.\', \'%s posts exported.\', $_REQUEST[\'exported\'] ), number_format_i18n( $_REQUEST[\'exported\'] ) );
echo "<div class=\\"updated\\"><p>{$message}</p></div>";
}
}
function perform_export($post_id) {
// do something
return true;
}
代码不应执行任何操作(请参阅perform\\u export()函数)并返回“Post exported”消息,但不起作用。新的批量操作正确显示,但我收到一个安全错误(“您确定要这样做吗?”)。如果我禁用安全检查
check_admin_referer(\'bulk-posts\');
我没有看到确认消息。
我想我的问题可能就在我使用的这个钩子上(我在钩子处理方面很在行!!)
add_action(\'load-upload.php\', \'custom_bulk_action\');
在保存处理实际工作的页面的$sendback变量中:
$sendback = admin_url( "post.php" );
你能帮我弄清楚我应该使用哪些挂钩和发送页吗?谢谢:)