Wordpress: get_delete_post_link not working for cu

2019-08-24 02:28发布

问题:

I have a custom role in functions.php:

add_role('test_pilot', 'Test Pilot', array(
    'read' => true,
    'edit_posts' => true,
    'delete_posts' => true,
));
// Give the custom role a new level
$test_pilot = get_role('test_pilot');
$test_pilot->add_cap('level_3');

...and on the front-end I'm trying to echo the delete post link:

<?php echo get_delete_post_link( get_the_ID() ); ?>

The problem is the link isn't actually being displayed when logged in as a user with the test pilot role.

If I am logged in as an administrator the link does display.

What am I doing wrong?

回答1:

Try to replacing with below code:

function init_roles() {
global $wp_roles;

if (class_exists('WP_Roles'))   
    if ( ! isset( $wp_roles ) )
        $wp_roles = new WP_Roles(); 

if (is_object($wp_roles)) :
    $wp_roles->add_cap( 'editor');      
endif;

$wp_roles->add_role( 'test_pilot', 'Test Pilot', array(
    'read' => true,
    'edit_posts' => true,
    'delete_posts' => true
)); 
}
add_action('init', 'init_roles');