Skip to content
Advertisement

WordPress show Edit and Delete buttons only if post’s author

My wordpress site uses post submissions (actually, CPTs) from site members. When displaying the CPT post, I’d like buttons to appear for Edit and Delete when the author is viewing the post, and not appear if any other member views the post. I’ve researched code and plugins, but all seem to only apply to the author role, not the specific author themselves. Is there a way to use PHP function/shortcode, Javascript, or some combination of the two, to add this functionality?

Advertisement

Answer

Here is the code for the disable edit and delete button. I’ve tested the code and it’s working fine from my side.

FOR THE EDIT BUTTON LINK ONLY

function prefix_remove_get_edit_post_link( $link ) {
    global $post;
    $current_user =  get_current_user_id();
    $author_id = get_post_field ('post_author', $post_id);

    if($author_id == $current_user) {
        return $link;
    }
    return null;
}
add_filter('get_edit_post_link', 'prefix_remove_get_edit_post_link');

FOR THE DELETE BUTTON LINK ONLY

function prefix_remove_get_delete_post_link( $link ) {
    global $post;
    $current_user =  get_current_user_id();
    $author_id = get_post_field ('post_author', $post_id);

    if($author_id == $current_user) {
        return $link;
    }
    return null;
}

add_filter('get_delete_post_link', 'prefix_remove_get_delete_post_link');

FOR THE HIDE BUTTONS EDIT AND DELETE

add_filter( 'post_row_actions', 'remove_row_actions', 10, 1 );
function remove_row_actions( $actions )
{
    global $post;
    $current_user =  get_current_user_id();
    $author_id = get_post_field ('post_author', $post_id);

    if($author_id != $current_user) {
        if( get_post_type() === 'post' ){
            unset( $actions['edit'] );
            unset( $actions['trash'] );
            unset( $actions['inline hide-if-no-js'] );
        }
    }
    return $actions;
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement