admin管理员组

文章数量:1434907

I'm working on a plugin where I have a Custom Post Type (CPT) registered and I need to add custom row actions to that particular post type. But could not make the following code hooked to post_row_actions work:

function ttest_copy_button( $actions, $post )
{
    // var_dump($actions);
    if( 'ttest' === $post->post_type ) {
        // pass nonce to check and verify false request
        $nonce = wp_create_nonce( 'copy_content' );

        // add our button
        $actions['copy_content'] = '<a data-nonce="'. $nonce .'" href="javascript:" role="button">'. esc_html__( 'Copy Content', 'ttest' ) .'</a>';
    }

    return $actions;
}

add_filter( 'post_row_actions', 'ttest_copy_button', 10, 2 );

My CPT code is:

function ttest_register_cpt() {
    register_post_type( 'ttest', array(
        'labels'              => array(
            'menu_name' => __('TTest', 'ttest')
        ),
        'hierarchical'        => true,
        'description'         => __( 'Get the test information', 'ttest' ),
        'supports'            => array( 'title', 'editor', 'excerpt', 'page-attributes' ),
        'taxonomies'          => array(),
        'menu_icon'           => 'dashicons-book-alt',
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'menu_position'       => 6,
        'show_in_nav_menus'   => false,
        'publicly_queryable'  => true,
        'exclude_from_search' => false,
        'has_archive'         => true,
        'query_var'           => true,
        'can_export'          => true,
        'rewrite'             => array( 'slug' => 'ttest' ),
        'capability_type'     => 'post'
    ) );
}

add_action( 'init', 'ttest_register_cpt', 5 );

When the var_dump($actions); line is active I see the code dump in all the other default post types, even on the WooCommerce product post type, but not in my custom post type.

What am I doing wrong?

I'm working on a plugin where I have a Custom Post Type (CPT) registered and I need to add custom row actions to that particular post type. But could not make the following code hooked to post_row_actions work:

function ttest_copy_button( $actions, $post )
{
    // var_dump($actions);
    if( 'ttest' === $post->post_type ) {
        // pass nonce to check and verify false request
        $nonce = wp_create_nonce( 'copy_content' );

        // add our button
        $actions['copy_content'] = '<a data-nonce="'. $nonce .'" href="javascript:" role="button">'. esc_html__( 'Copy Content', 'ttest' ) .'</a>';
    }

    return $actions;
}

add_filter( 'post_row_actions', 'ttest_copy_button', 10, 2 );

My CPT code is:

function ttest_register_cpt() {
    register_post_type( 'ttest', array(
        'labels'              => array(
            'menu_name' => __('TTest', 'ttest')
        ),
        'hierarchical'        => true,
        'description'         => __( 'Get the test information', 'ttest' ),
        'supports'            => array( 'title', 'editor', 'excerpt', 'page-attributes' ),
        'taxonomies'          => array(),
        'menu_icon'           => 'dashicons-book-alt',
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'menu_position'       => 6,
        'show_in_nav_menus'   => false,
        'publicly_queryable'  => true,
        'exclude_from_search' => false,
        'has_archive'         => true,
        'query_var'           => true,
        'can_export'          => true,
        'rewrite'             => array( 'slug' => 'ttest' ),
        'capability_type'     => 'post'
    ) );
}

add_action( 'init', 'ttest_register_cpt', 5 );

When the var_dump($actions); line is active I see the code dump in all the other default post types, even on the WooCommerce product post type, but not in my custom post type.

What am I doing wrong?

Share Improve this question asked Mar 30, 2019 at 8:14 Mayeenul IslamMayeenul Islam 12.9k21 gold badges85 silver badges169 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You might be wrong while saying:

I see the code dump in all the other default post types

Because your dump won't show code dump on the default post type page, and here is where the clue lies - page is hierarchical.

If you see the documentation of post_row_actions, you can see the description saying:

The filter is evaluated only for non-hierarchical post types.

Meet with the page_row_actions

As your post type is hierarchical you have to use page_row_actions the counterpart for the hierarchical post types.

本文标签: postrowactions not working for hierarchical post type