首页 > 解决方案 > 从 Wordpress 批量操作功能执行/调用 jquery

问题描述

最初,我没有在我的自定义 WP 插件中使用 WP_List_Table 并执行批量操作,我使用带有 onClick 事件的按钮来调用具有以下代码的 jQuery 函数;

<button type="button" onclick="selected_payout()" class="button">Pay Selected</button>
<button type="button" onclick="multi_withdaw()" class="button">Pay All</button>

现在,我决定在我的插件中使用 WP_List_Table 并且除了批量操作之外一切正常。下面是我的 prepare_items() 函数的代码;

    public function prepare_items()
   {
    $columns = $this->get_columns();
    $hidden = $this->get_hidden_columns();
    $sortable = $this->get_sortable_columns();

    $data = $this->table_data();
    usort( $data, array( &$this, 'sort_data' ) );

    $perPage = get_option('dragonpay_payout_settings')['entry_per_page'];
    $currentPage = $this->get_pagenum();
    $totalItems = count($data);

    $this->set_pagination_args( array(
        'total_items' => $totalItems,
        'per_page'    => $perPage
    ) );

    $data = array_slice($data,(($currentPage-1)*$perPage),$perPage);

    $this->_column_headers = array($columns, $hidden, $sortable);
    $this->items = $data;
    
    $this->process_bulk_action();
   }

以下是我的批量操作功能,下拉菜单出现在页面中。但是,单击应用按钮时没有任何反应。我不知道如何有效地调用 jQuery。

    public function get_bulk_actions() {

        return array(
                'pay_selected' => __( 'Pay Selected' ),
                'pay_all'   => __( 'Pay All' ),
        );

     }

public function process_bulk_action() {
        
        // security check!
        if ( isset( $_POST['_wpnonce'] ) && ! empty( $_POST['_wpnonce'] ) ) {

            $nonce  = filter_input( INPUT_POST, '_wpnonce', FILTER_SANITIZE_STRING );
            $action = 'bulk-' . $this->_args['plural'];

            if ( ! wp_verify_nonce( $nonce, $action ) )
                wp_die( 'Nope! Security check failed!' );

        }

        $action = $this->current_action();

        switch ( $action ) {

            case 'pay_selected':
                //call the jquery selected_payout()
                break;

            case 'pay_all':
                //call the jquery multi_withdaw()            
                break;

            default:
                // do nothing or something else
                return;
                break;
        }

        return;
     }

这个问题对其他人来说似乎很愚蠢,但我非常感谢您的建议和建议,以了解如何解决这个问题。

标签: jquerywordpress-plugin-creation

解决方案


推荐阅读