Home > OS >  Show icon on frontend if current user is the author of post
Show icon on frontend if current user is the author of post

Time:10-21

When a user registers on my website, a single post is automatically created (under a custom post type) with the user assigned as the author to that post. This post is essentially the user's "profile page".

Any user is able to see any other users profile page. However, I would like a button to appear on the profile page if it belongs to the logged in user.

In other words: if the logged-in user is the author of the post that is currently being viewed (on the frontend), then display the button.

The below is the closest I have come to achieving what I need to do - I have created a button with css id #profile_edit_button and hidden it by default using display:none. The code below "unhides" the button, but on all profile pages. It also throws out the following errors:

Warning: Undefined variable $user_session_id Warning: Undefined variable $author_id

The code I've used is as follows. Any guidance would be greatly appreciated.

 if($user_session_id == $author_id) {
    ?>
    <style type="text/css">
        #profile_edit_button {
            display: flex !important;
        }
        </style>;
    <?php } ?>

CodePudding user response:

Try the below code. code will go in your active theme functions.php file.

function current_user_is_post_author(){
    if( is_single() ){
        global $post;
        if( get_current_user_id() == $post->post_author ) { ?>
            <style type="text/css">
                #profile_edit_button   {
                    display: none !important;
                }
            </style>;
    <?php } }
}
add_action( 'wp_head', 'current_user_is_post_author', 10, 1 );
  • Related