I have a custom post type define below and I want to add a custom row action to allow me to ‘update’ the post via the admin panel

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

This is the code that registers the row action

function post_row_actions($actions, $post) {
    $actions = array_merge($actions, array(
        'update' => sprintf('<a href="https://wordpress.stackexchange.com/questions/82761/%s">Update</a>',
            wp_nonce_url(sprintf('edit.php?post_type=league&action=update&post_id=%d',$post->ID),
            'abc'))
    ));
    return $actions;
}

I’ve defined a function update() in the same php file, in which i plan to do real work

function update()
{
    // do some custom update stuff on the post content
}

My problem is how can I ensure that the URL request with the update action calls the function above?

I’ve used the following resources

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 Answers
2

You’d have to use the $_GET method. Here, I’m hooking in admin_head-{$pagenow}, depending on your functionality maybe you’ll need to hook in load-{$pagenow}.

Prefix your var names to not colide with any WordPress internals, in this case my-update for the action name:

edit.php?post_type=league&action=my-update&post_id

Sample code:

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
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *