0

i am coding a plugin, where user can subscribe to a indvidual author of post (like we do on YouTube). I want to display the subscribe link to all logged in users, but not the author of post.

my plugin gets the author id for each post and display's the link. Problem is author would not subscribe to his own self,

So in short i want a way to echo a link, if current user is NOT the author of the post.

1

Try something like this in your template tags (or wherever in your theme):


function should_display_link() {
    $post = get_post();

    // Something went wrong, bail. (You should always have a post, in theory)
    if ( ! $post ) {
        return false;
    }

    $current_user = get_current_user_id();

    // Assuming only logged-in users can subscribe.
    if ( ! $current_user ) {
        return false;
    }

    return absint( $post->post_author ) !== absint( $current_user );
}

In your template, you can use it like this:

<?php if ( should_display_link() ) : ?>
    <!-- Your link code here -->
<?php endif; ?>
  • That worked out, i applied in a custom plugin, and worked perfectly – Aditya Agarwal Feb 4 at 2:01
  • Thanks @AdityaAgarwal! If my response answered your question, could you please accept it to mark the question as Answered? – phatskat Feb 4 at 2:15
  • <?php echo '<a class="btn" style="background-color:#1f1fff; border-radius:100px;" href="<'.site_url('chat/').'?new-message&to='.get_the_author_meta( 'user_login' ).'">Chat</a>' ; ?> I tried to echo this code in place of your link code. But it didn't work out. My this code when echoed outside the 'should display link' works perfectky. Could u please help me figure it out. – Aditya Agarwal Feb 4 at 8:38
  • 1
    oh i got it. It worked. there was a typo. Thanks for answering the question – Aditya Agarwal Feb 4 at 11:13
  • No problem, thanks for accepting my answer. For reference, I'd have a look at the methods esc_url and add_query_var for that URL from your other comment. Happy coding! – phatskat Feb 4 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.