post_row_actions not working for hierarchical post type

I'm working on a plugin where I have a Custom Post Type (CPT) registered and I need to add custom row actions to th

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745644416a4637883.html

相关推荐

  • post_row_actions not working for hierarchical post type

    I'm working on a plugin where I have a Custom Post Type (CPT) registered and I need to add custom row actions to th

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信