最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

post_row_actions not working for hierarchical post type

matteradmin5PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far