• Can someone tell me how to display a post edit link for post author when logged in?

    I tried this:

    <?php
    	global $current_user;
    	get_currentuserinfo();
    
    	if (is_user_logged_in() && $current_user->ID == $post->post_author)  {
    		echo ' <a href="<?php echo $edit_post; ?>">Edit</a>';
    	}
    ?>

    Works perfectly displaying the link only for authors and logged in users but the link is dead. when you hover over the link it shows <?php echo $edit_post; ?> When you remove the conditional the Link works just fine.

    I’ve also tried this function I found on the net:

    function limit_edit_Link_wpse_85214($link) {
      global $post,$current_user;
      get_currentuserinfo();
      if ($post->post_author == $current_user->ID) {
        return $link;
      }
      return false;
    }
    add_filter('get_edit_post_link','limit_edit_Link_wpse_85214');

    Had no affect what so ever…

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter Dono12

    (@dono12)

    Figured a solution for anyone having this issue. Simply remove the echo”

    and add some extra php, like so:

    <?php
    	global $current_user;
    	get_currentuserinfo();
    
    	if ($post->post_author == $current_user->ID || current_user_can( 'manage_options' ))  { ?>
    	<a href="<?php echo $edit_post; ?>">Edit</a>
    
    <?php } ?>

    Can someone please take a look at this? I’ve tried this code…and two others that are quite similar, but they don’t seem to work as they should.

    The “edit” link I am trying to display appears across all posts and is not being limited to the post author. Just to clarify, I am trying to add an “edit” link, but it should only appear when the current user is the post author.

    Thank you.

    Thread Starter Dono12

    (@dono12)

    Try this tutorial. Follow the steps to the pot of gold at the end of the rainbow ??
    Side Note, it only gives front-end editing capabilities to your users.
    Also you should look into adjusting user capabilities locating in admin settings.

    https://codex.www.ads-software.com/Roles_and_Capabilities

    https://code.tutsplus.com/tutorials/posting-via-the-front-end-editing-and-deleting–wp-27172

    USE THIS TO REPLACE THE TUT’s EDIT LINK:

    <?php
    	global $current_user;
    	get_currentuserinfo();
    	if ($post->post_author == $current_user->ID || current_user_can( 'manage_options' ))  { ?>
    		<div class="edit-post">  
    <a href="<?php echo $edit_post; ?>"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> Edit Post</a></div>
    
    <?php } ?>
    • This reply was modified 7 years, 7 months ago by Dono12.
    • This reply was modified 7 years, 7 months ago by Dono12.

    Thank you. I figured out the problem. It was a plugin issue. I use a php in widgets plugin and it just won’t work. I just ending up putting the code in my single.php file and now it works as expected.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Display Edit Link Only For Logged In and Author’ is closed to new replies.