Give access to only two (/home, /inbox) page for a particular user with specific role in wordpress - wordpress

I want to give only two page (/home, /inbox) access to my user with role "Vendor", if user tries to access other pages than it will automatically redirect to "/inbox" page, I put the below code to achieve the functionality but after adding this to function.php, site again n again redirect and finally dies with message "Page isn't properly redirected". please suggest what is wrong with my tried code or any other solution.
function vendor_redirect() {
global $post;
if(current_user_can('Vendor') && !in_array($post->slug,array("home","inbox"))) {
wp_safe_redirect('/inbox');
}
}
add_action('template_redirect', 'vendor_redirect');

The main issue the way I tried to get the page slug, the correct way to get the slug is "$post->post_name". also I put exit after wp_safe_redirect as well because codex suggest that:
function vendor_redirect() {
global $post;
if(current_user_can('Vendor') && !in_array($post->post_name,array("home","inbox"))) {
wp_safe_redirect(get_permalink(get_page_by_path( 'inbox' )));
exit;
}
}
add_action('template_redirect', 'vendor_redirect');

Related

How to disable Woocommerce login redirect if on Checkout Page

I have some code in my WordPress functions.php file to do a redirect after a successful woocommerce login. It works great, but I'm wondering how I can disable that redirect from running on the checkout page?
If a user logs into their account while on the checkout page they are getting redirected off the checkout page just before they have the chance to fill in their credit card data which isn't a great experience.
According to https://docs.woocommerce.com/document/conditional-tags/ conditional query tags like is_checkout() won't work in the functions.php file because You can only use conditional query tags after the posts_selection action hook in WordPress. And, unfortunately, apparently the functions.php file gets run before that.
What's my best option to solve this?
Here's the code I currently have running (the one I want to disable on checkout page):
function woo_login_redirect( $redirect_to ) {
$redirect_to = get_permalink(70241);
return $redirect_to;
}
add_filter('woocommerce_login_redirect', 'woo_login_redirect');
This is me trying to avoid the redirect if on checkout page (it doesn't work. It just redirects to post id 70241):
function woo_login_redirect( $redirect_to ) {
if (is_checkout()){
$redirect_to = '#';
} else {
$redirect_to = get_permalink(70241);
}
return $redirect_to;
}
add_filter('woocommerce_login_redirect', 'woo_login_redirect');

Modify User Roles in WordPress?

I have been working on a client's WordPress Website and last day my client want to hide navigation menu and pages from author/contributor categories.
I have searched and tried some of the plugin but didn't get the exact thing. Please let me know what should i use to hide some pages from user and from navigation.
Only Admin can see all the pages and other members should see only 1 section that is allowed to visible for them.
Thank You
use this plugin to manage All roll:
http://wordpress.org/plugins/user-role-editor/
Here is the Complete function for removing each Menu and submenu from wp-admin for another user:
function remove_menus() {
global $menu, $submenu;
$restricted = array(__('Dashboard'), __('Profile'), __('Users'), __('Tools'), __('Comments'), __('Settings'), __('Plugins')); //Here you can also define the name like Pages
end($menu);
while (prev($menu)) {
$value = explode(' ', $menu[key($menu)][0]);
if (in_array($value[0] != NULL ? $value[0] : "", $restricted)) {
unset($menu[key($menu)]);
}
}
unset($menu[5]); // this is just for example
unset($submenu['edit.php'][16]); // this is just for example
}
Now You have to put a conditon for other user i.e:
$thisusername = $current_user->user_login; // this is to get the current user login
if (($thisusername == "user123")) {
add_action('admin_menu', 'remove_menus');
}
Note: You can find many plugins but all of them are not in depth like this code.Well you can try this plugin to manage your user's roles.Capability Manager Plugin

Session Login in wordpress

I have a site www.example.com and a wordpress blog at www.example.com/blog.
I want my user to login at www.example.com and $_SESSION['USERNAME'] is passed to www.example.com/blog.
I just want to know, how can i automatically login the end user to wordpress blog, once they login to my main site.
For now i am passing $_SESSION['USERNAME'] and using a plugin external database login, which I have linked to my main site database.
Any function that is used to login to wordpress using session username will be helpful.
Wordpress don't use $_SESSIONyou need to code a plugin or to add some code to your functions.php to do this - cf http://www.frank-verhoeven.com/using-session-in-wordpress/
You should add something like this :
function init_sessions() {
if (!session_id()) {
session_start();
}
}
add_action('init', 'init_sessions');
NB: there is also a plugin to manage Sessions with Wordpress, maybe you can use/hack this http://wordpress.org/plugins/wp-session-manager/
I have been reading on this. Here is what I have found as I am in the same issue. In your wp-config.php file at the top add:
add_action('init', 'myStartSession', 1);
function myStartSession() {
if(!session_id()) {
session_start();
}
}
HOWEVER, " The data stored in the session doesn’t go away when the user logs out or logs into a different account. For that you need to destroy the session. ..." (exerpt from this nice write up). It explains you also need to:
function myEndSession() {
session_destroy ();
}
if you can get wp user_id you could do something like this:
function init_sessions() {
if (user_logged_on_site) {
#session_start();
$user_id = $_SESSION['wp_user_id']; //this has to be set when the user logs on your website
wp_set_auth_cookie( $user_id); //log in the user on wordpress
}
}
add_action('init', 'init_sessions');
Documentation on wp_set_auth_cookie http://codex.wordpress.org/Function_Reference/wp_set_auth_cookie

wordpress login redirect function

I am trying to create my own plugin in wordpress. Everything works great but I want to make a loginredirect check for users who want to access to the plugins page.
Here is my function in my functions.php:
function wpuf_auth_redirect_login() {
$user = wp_get_current_user();
if ( $user->id == 0 ) {
nocache_headers();
wp_redirect(get_option('siteurl') . '/wp-login.php?redirect_to=' . urlencode($_SERVER['REQUEST_URI']));
exit();
}
}
And this is from my myplugin.php:
function wpuf_user_edit_profile() {
wpuf_auth_redirect_login(); // if not logged in, redirect to login page
nocache_headers();
wpuf_post_form_style();
wpuf_user_edit_profile_form();
}
add_shortcode('wpuf_editprofile', 'wpuf_user_edit_profile');
It is not working. If I enter the plugins page as a guest in browser it does not redirect. It only shows the template uncomplete. Any help?
Where you say that it 'only shows the template uncomplete', this could indicate that there is a syntax error in your PHP code, and the server has stopped rendering the page at the point of that error.
I would suggest that you check for any errors by turning error reporting on, and seeing what happens from there.

Wordpress Plug-ins: How-to add custom URL Handles

I'm trying to write a Wordpress Plug-in but can't seem to figure out how you would modify how a URL gets handled, so for example: any requests made for:
<url>/?myplugin=<pageID>
will get handled by a function in my plug-in. I'm sure this is a very simple to do, but I'm pretty new to working with Wordpress and couldn't find it in the documentation.
In order to handle just a specific URL use the code below:
add_action('parse_request', 'my_custom_url_handler');
function my_custom_url_handler() {
if(isset($_GET['myplugin']) && $_SERVER["REQUEST_URI"] == '/custom_url') {
echo "<h1>TEST</h1>";
exit();
}
}
add_action('parse_request', 'my_custom_url_handler');
function my_custom_url_handler() {
if( isset($_GET['myplugin']) ) {
// do something
exit();
}
}
That should set you on the right direction. parse_request happens before WordPress runs any of the complicated WordPress queries used to get the posts for the current URL.

Resources