WP 3.7.1中的自定义批量操作

时间:2014-02-28 作者:Marcel de Hoog

因此,我正在尝试在Wordpress 3.7.1中使用自定义批量操作,但迄今为止没有成功。我使用的基础是http://www.skyverge.com/blog/add-custom-bulk-action, 这里出了问题:

$wp_list_table = _get_list_table(\'WP_Posts_List_Table\');
$action = $wp_list_table->current_action();
该操作本身是用jQuery添加的,并显示。

当我使用提示显示$action 值它始终为空,这意味着脚本的其余部分不会激发。有什么想法吗?整个代码:

<?php
/*
Plugin Name: FoxRunSoftware 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 (!class_exists(\'FRS_Custom_Bulk_Action\')) {

    class FRS_Custom_Bulk_Action {

        public function __construct() {

            if(is_admin()) {
                // admin actions/filters
                add_action(\'admin_footer-edit.php\', array(&$this, \'custom_bulk_admin_footer\'));
                add_action(\'load-edit.php\',         array(&$this,     \'custom_bulk_action\'));
            add_action(\'admin_notices\',         array(&$this, \'custom_bulk_admin_notices\'));
        }
    }


    /**
     * Step 1: add the custom Bulk Action to the select menus
     */
    function custom_bulk_admin_footer() {
        global $post_type;

        if($post_type == \'post\') {
            ?>
                <script type="text/javascript">
                    jQuery(document).ready(function() {
                        jQuery(\'<option>\').val(\'export\').text(\'<?php _e(\'Export\')?>\').appendTo("select[name=\'action\']");
                        jQuery(\'<option>\').val(\'export\').text(\'<?php _e(\'Export\')?>\').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;
        $post_type = $typenow;

        if($post_type == \'post\') {

            // get the action
            $wp_list_table = _get_list_table(\'WP_Posts_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("export");
            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" );

            $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();
        }
    }


    /**
     * Step 3: display an admin notice on the Posts page after exporting
     */
    function custom_bulk_admin_notices() {
        global $post_type, $pagenow;

        if($pagenow == \'edit.php\' && $post_type == \'post\' && 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 whatever work needs to be done
        return true;
    }
}
}

new FRS_Custom_Bulk_Action();
还有更多信息,在我看来,WP\\u List\\u Table只返回空值。以下是$wp\\U list\\U表格的内容,有什么想法吗?:

object(WP_Posts_List_Table)#8848 (10) { 
    ["hierarchical_display"]=> NULL 
    ["comment_pending_count"]=> NULL 
    ["user_posts_count"]=> NULL 
    ["sticky_posts_count"]=> int(0) 
    ["items"]=> NULL 
    ["_args"]=> array(4) { 
        ["plural"]=> string(5) "posts" 
        ["singular"]=> string(0) "" 
        ["ajax"]=> bool(false) 
        ["screen"]=> object(WP_Screen)#8846 (16) { 
            ["action"]=> string(0) "" 
            ["base"]=> string(4) "edit" 
            ["columns":"WP_Screen":private]=> int(0) 
            ["id"]=> string(15) "edit-shop_order" 
            ["in_admin":protected]=> string(4) "site" 
            ["is_network"]=> bool(false) 
            ["is_user"]=> bool(false) 
            ["parent_base"]=> NULL 
            ["parent_file"]=> NULL 
            ["post_type"]=> string(10) "shop_order" 
            ["taxonomy"]=> string(0) "" 
            ["_help_tabs":"WP_Screen":private]=> array(0) { } 
            ["_help_sidebar":"WP_Screen":private]=> string(0) "" 
            ["_options":"WP_Screen":private]=> array(0) { } 
            ["_show_screen_options":"WP_Screen":private]=> NULL 
            ["_screen_settings":"WP_Screen":private]=> NULL 
        } 
    } 
    ["_pagination_args"]=> array(0) { } 
    ["screen"]=> object(WP_Screen)#8846 (16) { 
        ["action"]=> string(0) "" 
        ["base"]=> string(4) "edit" 
        ["columns":"WP_Screen":private]=> int(0) 
        ["id"]=> string(15) "edit-shop_order" 
        ["in_admin":protected]=> string(4) "site" 
        ["is_network"]=> bool(false) 
        ["is_user"]=> bool(false) 
        ["parent_base"]=> NULL 
        ["parent_file"]=> NULL 
        ["post_type"]=> string(10) "shop_order" 
        ["taxonomy"]=> string(0) "" 
        ["_help_tabs":"WP_Screen":private]=> array(0) { } 
        ["_help_sidebar":"WP_Screen":private]=> string(0) "" 
        ["_options":"WP_Screen":private]=> array(0) { } 
        ["_show_screen_options":"WP_Screen":private]=> NULL 
        ["_screen_settings":"WP_Screen":private]=> NULL 
    } 
    ["_actions"]=> NULL ["_pagination"]=> NULL 
}

2 个回复
最合适的回答,由SO网友:sakibmoon 整理而成

你看到了$action 空,因为存在来自的重定向wp-admin/edit.php.

因此,您看到的输出是在重定向之后。

使用die() 之后echo $action 在重定向之前查看输出。

SO网友:Pat J

查看您提到的页面,我注意到有一个代码段缺少一些代码。在之后的第一个代码段中The Solution, 有以下代码段:

add_action(\'admin_footer-edit.php\', \'custom_bulk_admin_footer\');

function custom_bulk_admin_footer() {

  global $post_type;

  if($post_type == \'post\') {

    ?>
然后作者声明,正如您所见,这里我们使用javascript将新的批量操作项注入到帖子编辑页面上的两个批量操作选择下拉列表中。只有该代码段中没有javascript。

但是,查看页面的来源,我发现:

add_action(\'admin_footer-edit.php\', \'custom_bulk_admin_footer\');



function custom_bulk_admin_footer() {

  global $post_type;

  if($post_type == \'post\') {

    ?>

    <script type="text/javascript">

      jQuery(document).ready(function() {

        jQuery(\'<option>\').val(\'export\').text(\'<?php _e(\'Export\')?>\').appendTo("select[name=\'action\']");

        jQuery(\'<option>\').val(\'export\').text(\'<?php _e(\'Export\')?>\').appendTo("select[name=\'action2\']");

      });

    </script>

    <?php

  }

}
。。。实际上,其中包含JS代码(虽然不是WordPress中应该使用的方式,请参见wp_enqueue_script() 了解更多信息。

结束

相关推荐

bulk post_content update

我需要将特定标签/类别中的所有post\\u内容替换为我所需的内容。是他们的插件来完成它,还是他们的查询来完成它。我厌倦了手工操作,这是他们实现它的一种方式。