I have a challenge creating a title for this tutorial, so to describe what it covers, I recently had a question regarding how to dynamically put a redirect back to the original content into the link to the login page. In this instance, the user had customized how they were using the plugin and rather than having the login in place of blocked content as is the default, they had a link to the login page.
The WP-Member login page (via the shortcode) does accept the WP redirect_to parameter, so that if this parameter is given, it will redirect back to that link upon successful login. But how to make that work dynamically? Here is a quick example of how you can filter the content of a post, look for a link to the login page, and add the redirect_to parameter to the end.
This example will use the_content to call the filter function, get_permalink to get the link we want to come back to, and str_replace to do the search and replace.
The login page is now a setting in the plugin’s main options, so you need to make sure that is set.
add_filter( 'the_content', 'login_link_back' ); function login_link_back( $content ) { global $wpmem; $old = $wpmem->user_pages['login']; $new = $wpmem->user_pages['login'] . '?redirect_to=' . get_permalink(); return str_replace( $old, $new, $content ); }