媒体集项的自定义批量操作

时间:2013-03-05 作者:Torakiki

我正在尝试将自定义“批量操作”添加到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" );
你能帮我弄清楚我应该使用哪些挂钩和发送页吗?谢谢:)

1 个回复
SO网友:JohnK

我有一个非常相似的问题(“How to make custom bulk actions work on the media/upload page?)关于Justin Stern的Custom Bulk Action Demo, 这是刚刚回答的。

我给你的最大建议是,一步一步地进行调整:确保export 先为媒体工作。不要试图同时完成两个步骤。(这就是所有这些电脑的秘密:把东西分成可管理的小块。)

对于最后的具体问题:

更换check_admin_referer(\'bulk-posts\') 具有check_admin_referer(\'bulk-media\') 将摆脱“您确定要这样做吗?”add_action(\'load-upload.php\', \'custom_bulk_action\') 听起来可能很傻,但很好

  • $sendback = admin_url( "upload.php" ) 很好custom_bulk_admin_footer(), 和第3部分,custom_bulk_admin_notices(), 您要替换

    • \'post\' --> \'attachment\'.
    需要更换的其他地方

    • post.php --> upload.php,
    • edit --> upload, 和post --> media.custom_bulk_action(), 需要更复杂的适应。我建议您使用下面的代码(来自上面链接的问题)。

       add_action(\'load-upload.php\', array(&$this, \'custom_bulk_action\'));
      
      
      function custom_bulk_action() {
      
      //  ***if($post_type == \'attachment\') {  REPLACE WITH:
          if ( !isset( $_REQUEST[\'detached\'] ) ) {
      
          // get the action
          $wp_list_table = _get_list_table(\'WP_Media_List_Table\');  
          $action = $wp_list_table->current_action();
      
          echo "\\naction = $action\\n</pre>";
      
          $allowed_actions = array("export");
          if(!in_array($action, $allowed_actions)) return;
      
          // security check
      //  ***check_admin_referer(\'bulk-posts\'); REPLACE WITH:
          check_admin_referer(\'bulk-media\'); 
      
          // make sure ids are submitted.  depending on the resource type, this may be \'media\' or \'ids\'
          if(isset($_REQUEST[\'media\'])) {
            $post_ids = array_map(\'intval\', $_REQUEST[\'media\']);
          }
      
          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( "upload.php?post_type=$post_type" );
      
          $pagenum = $wp_list_table->get_pagenum();
          $sendback = add_query_arg( \'paged\', $pagenum, $sendback );
      
          switch($action) {
            case \'export\':
      
              // 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();
        }
      }
      

  • 结束

    相关推荐

    WP3.5 Media Uploader-如何让它接受多幅图像?

    我想知道如何制作WP3。5媒体上载程序返回2个或多个项目attachement? 即使选择了2个项目,它也始终运行一次。代码:$(\'.button\').on(\'click\', function(){ var send_attachment_backup = wp.media.editor.send.attachment; wp.media.editor.send.attachment = function(props, attachment){