Support Forum
Before today, I had the SP Global Settings set to redirect users below a certain level to the front page of our forum ("/community") and it worked great. It only allow back-end admin access to Admins, anyone else logging in or out went to the forum.
Today we have opened up the back-end access to Editors, Authors and Contributors as well so that we can start adding more content from non-staff writers, and I have things working well EXCEPT that when users now logout, they STILL get redirected to the forum page instead of staying on the page they were on, which is what we want (I created a logout link instead of using wp_loginout() because we needed to add extra links if they were logged in or if they were logged out).
I've double-checked that I don't have any URL set in the Global Settings for Simple:Press where it says "URL to redirect to if blocking admin access:" - that box is blank.
I regenerated the SP permalinks and the WP permalinks but it still redirects anyone on logout to the forum.
Here's what I added in my functions to show the logout link if they are logged in:
`if ( is_user_logged_in() ) {
$userprofilelink = get_edit_user_link();
$pageURL = 'http://';
$pageURL .= $_SERVER['HTTP_HOST'];
$pageURL .= $_SERVER['REQUEST_URI'];
echo '<li><a href="';
echo wp_logout_url($pageURL);
echo '">Logout</a>';
echo '<li><a href="';
echo $userprofilelink;
echo '">Edit Profile</a></li>';
} `
So when you hover over the logout link it looks correct - it calls the current page - but when you click on it, it changes the url from the current page to the community.
Also, I don't know if this is relevant, but when we first started using SP I didn't know it would redirect certain users so I had a function in my functions.php to do the same thing, once I learned that SP would do this, I removed that function entirely.....is it possible that my WP installation is somehow storing an old wp_redirect command?
This is what I had, and my understanding is that the default type of redirect is 302, not 301 so it should have only been a temporary change anyway.....
`function restrict_admin()
{
if ( ! current_user_can( 'manage_users' ) && '/wp-admin/admin-ajax.php' != $_SERVER['PHP_SELF'] ) {
wp_redirect( site_url() . '/community' );
exit;
}
}
add_action( 'admin_init', 'restrict_admin', 1 );`
Any suggestions would be greatly appreciated - I feel like I must be overlooking something simple.
do you have a logout redirect specified on forum - components - login and registration??
Visit Cruise Talk Central and Mr Papa's World
Ah ha - I knew it had to be something simple.
BUT this presents me with a quandary - how can I have my writers (authors and contributors) as well as editors/admins NOT have to redirect to the forum on login/out.......we really only want forum users (who have no WP role in the backend) to be redirected to the forum if they login or out.
Is that possible?
Or can I change those settings in such a way as to tell it to use the current page? Sort of like I’m doing above the the $pageURL variable?
we dont make a difference between wp roles and logout redirects.. you would need some custom code to hook into that process...
we do offer sp admins and mods the option to bypass the logout redirect... so that would be option if you could make them mods..
Visit Cruise Talk Central and Mr Papa's World
Ok thanks, I appreciate the direction.
I plan to tackle this even though I'm not a coder, although I can usually understand other people's code and figure out how to modify it (granted it generally takes me hours of research and a few more hours of testing to get things correct, LOL).
SO I looked through the documentation here on the API actions and filters, and I found a filter (hook?) named sph_logout_redirect, which I assume I need to write the code for and put it in my forum child theme's functions file, yes?
And I'm guessing it would be something very similar to what I have above, except instead of using
add_action( ‘admin_init’, ‘restrict_admin’, 1 );
I would do something like add_action( 'sph_logout_redirect', 'restrict_admin', 1 );
Is that on the right track?
yes, that is the right track... you will need to see what kind of user you have and adjust the redirect how you like based on that specific user... you can get at user info with the globals $spThisuser or $current_user
Visit Cruise Talk Central and Mr Papa's World
Well sorry this has taken so long.......I've been trying a lot of things and I get close, BUT no cigar.
What happens is that on logout, both WP and SP take the user to the login page, and while that's an improvement over before (they were getting redirected to the forum home page), it's still not what I want - I want them to stay on whatever page they are on when they click 'log out'.....so if they're in the forum they stay on that page, or if they're on the site they stay where they are.
Here's my code:
`
// Restrict access to back-end to Admins, Editors, & Authors only, redirects other users to whatever page they were on
function restrict_admin()
{
$pageURL = 'http://';
$pageURL .= $_SERVER['HTTP_HOST'];
$pageURL .= $_SERVER['REQUEST_URI'];
if ( ! current_user_can( 'publish_posts' ) && '/wp-admin/admin-ajax.php' != $_SERVER['PHP_SELF'] ) {
wp_redirect( $pageURL );
exit;
}
}
add_action( 'admin_init', 'restrict_admin', 1 );
add_action( ‘sph_logout_redirect’, ‘restrict_admin’, 1 );
`
Any ideas on what I'm doing wrong?