0

I created a plugin that if the post is in a certain category, it will change the content to display a login message if the user is not logged in. How can I change the post title's link from my plugin?

Note: My code is at https://github.com/NerdOfLinux/MemberOnly/

If you know the plugin Ultimate Member, I want something similar to their custom post settings to only allow logged in users to access the post, and redirect users who are not logged in to the login page.

  • I don't understand... "How can I change the URL displayed on the posts page(the title with a link to the post) from the plugin?" Can you reformulate please? – ClemC Aug 25 '17 at 21:44
  • The title of the post that when you click it, it takes you to the permalink – NerdOfLinux Aug 25 '17 at 21:45
  • Ok, you want to replace the title's link, but then, by what do you want to replace it? Also, I'm not sure to understand when do you want to change it, under which conditions, etc... From all this could depend the solution. – ClemC Aug 25 '17 at 21:55
  • How can I change it from a plugin. I'll put it in the right place in my code. – NerdOfLinux Aug 25 '17 at 22:17
0

Use the post_link filter hook:

add_filter( 'post_link', 'wpse_change_post_link', 99, 3 );

function wpse_change_post_link( $url, $post, $leavename = false ) {
    // Can't fill these conditions since you didn't specify them...
    if ( 'my_post_type' === $post->post_type ) {
        $url = ''; // Your new URL...
    }

    return $url;
}
  • Doesn't work. My plugin is github.com/NerdOfLinux/MemberOnly – NerdOfLinux Aug 25 '17 at 22:54
  • @NerdOfLinux I just checked a little of your code on GitHub... You put my code which include a function declaration in another function declaration... Of course it doesn't work... Put my whole code in global scope. – ClemC Aug 25 '17 at 23:00
  • How can I apply that to certain posts and not all? – NerdOfLinux Aug 30 '17 at 23:40
  • @NerdOfLinux see the updated answer. – ClemC Aug 31 '17 at 1:00

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.