如何将POST_ROW_ACTIONS()与自定义操作函数链接

时间:2013-01-23 作者:emeraldjava

我在下面定义了一个自定义帖子类型,我想添加一个自定义行操作,以允许我通过管理面板“更新”帖子

class LeagueCpt
{
    function __construct()
    {
        add_action( \'init\', array(&$this,\'registerLeagueCPT\'));
        add_filter(\'post_row_actions\', array(&$this,\'post_row_actions\'), 0, 2);
    }

    function registerLeagueCPT()
    {
        $leagueLabels = array(
            \'name\' => _x( \'Leagues\', \'league\' ),
            \'singular_name\' => _x( \'League\', \'league\' ),
            \'add_new\' => _x( \'Add New\', \'league\' ),
            \'add_new_item\' => _x( \'Add New League\', \'league\' ),
            \'edit_item\' => _x( \'Edit League\', \'league\' ),
            \'new_item\' => _x( \'New League\', \'league\' ),
            \'view_item\' => _x( \'View League\', \'league\' ),
            \'search_items\' => _x( \'Search Leagues\', \'league\' ),
            \'not_found\' => _x( \'No league found\', \'league\' ),
            \'not_found_in_trash\' => _x( \'No leagues found in Trash\', \'league\' ),
            \'parent_item_colon\' => _x( \'Parent League:\', \'league\' ),
            \'menu_name\' => _x( \'Leagues\', \'league\' ),
            );

        $leagueArgs = array(
            \'labels\' => $leagueLabels,
            \'hierarchical\' => false,
            \'description\' => \'League Details\',
            \'supports\' => array( \'title\',\'editor\',\'excerpt\',\'thumbnail\',\'custom-fields\',\'page-attributes\',\'comments\'),
            \'public\' => true,
            \'show_ui\' => true,
            \'show_in_menu\' => true,
            \'show_in_nav_menus\' => true,
            \'publicly_queryable\' => true,
            \'exclude_from_search\' => false,
            \'has_archive\' => true,
            \'query_var\' => true,
            \'can_export\' => true,
            \'rewrite\' => true,
            \'capability_type\' => \'post\'
            );
        register_post_type(\'league\', $leagueArgs );
    }
这是注册行操作的代码

function post_row_actions($actions, $post) {
    $actions = array_merge($actions, array(
        \'update\' => sprintf(\'<a href="%s">Update</a>\',
            wp_nonce_url(sprintf(\'edit.php?post_type=league&action=update&post_id=%d\',$post->ID),
            \'abc\'))
    ));
    return $actions;
}
我在同一个php文件中定义了一个函数update(),我计划在其中进行实际工作

function update()
{
    // do some custom update stuff on the post content
}
我的问题是,如何确保具有更新操作的URL请求调用上述函数?

我使用了以下资源

Custom Post Row Actions

http://wordpress.org/support/topic/trying-to-add-custom-post_row_actions-for-custom-post-status

Row actions for custom post types?

http://wordpress.org/support/topic/replacement-for-post_row_actions

http://www.ilovecolors.com.ar/saving-custom-fields-quick-bulk-edit-wordpress/

2 个回复
SO网友:brasofilo

你必须使用$_GET 方法来,我要上钩了admin_head-{$pagenow}, 根据您的功能,您可能需要加入load-{$pagenow}.

在本例中,将var名称的前缀设置为不与任何WordPress内部代码合并my-update 操作名称:

edit.php?post_type=league&action=my-update&post_id
示例代码:

add_action( \'admin_head-edit.php\', \'custom_get_http_wpse_82761\' );

function custom_get_http_wpse_82761()
{
    global $typenow;
    if( \'league\' != $typenow )
        return;

    if( isset( $_GET[\'my-update\'] ) )
    {
        // do stuff
    }
}

SO网友:emeraldjava

我是在how-do-i-best-handle-custom-plugin-page-actions 我发现这非常有用,因为操作通过wordpress的“admin\\u action”挂钩直接映射到函数。唯一的问题是它只支持发送POST请求的html表单。我想保留“post\\u row\\u actions”允许的URL链接。

我的变通方法是使用jQuery将URL的GET请求详细信息转换为POST请求。我在href中添加了一个“id”属性,并添加了以下JQuery代码。它获取原始URL,然后发出POST请求。

jQuery(document).ready( function() {
    jQuery("a#bhaa_league_populate").bind("click", function(event) {
        event.preventDefault();
        var url = jQuery(this).attr("href");

        var p = url.split(\'?\');
        var action = p[0];
        var params = p[1];

        jQuery.post( action, params);
    });
});
它让我两全其美。

结束

相关推荐