3

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="%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

4

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

I came across this post on how-do-i-best-handle-custom-plugin-page-actions which i found very useful since the action is mapped directly to a function via the wordpress 'admin_action' hook. The only problem it only supports html forms which send a POST request. I want to keep the URL's link that 'post_row_actions' allow me.

My wordaround is to use jQuery to convert the GET request details of the URL into a POST request. I added an 'id' attribute to the href and added the following JQuery code. It takes the original URL and then makes a POST request.

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

It allows me the best of both worlds.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.