Wordpress - display shortcode for user certain user role - php

I am trying to display a shortcode of a specific user role. The user role is customer.
global $current_user;
get_currentuserinfo();
if ( user_can( $current_user, "customer" ) ){
echo do_shortcode('[gravityform id="2"]');
}

You can use the following code:
$user = wp_get_current_user();
if ( in_array( 'shop_manager', (array) $user->roles ) ) { // user role condition
echo do_shortcode('[gravityform id="2"]');
}

Related

php redirect with custom user roles set and working but redirect no

I have created code to add custom user rules. It works. However when I try to tie in the code for redirecting an already logged in user based on one test role, then it doesn't work. What am I missing in this in order to have it redirect, based on a user's role. Here is the code:
add_action('init', 'cloneRoleCompany');
function cloneRoleCompany(){
global $wp_roles;
if ( ! isset( $wp_roles ) )
$wp_roles = new WP_Roles();
$adm = $wp_roles->get_role('subscriber');
//Adding a 'new_role' with all admin caps
$wp_roles->add_role('company-admin', 'Company Admin', $adm->capabilities);
$wp_roles->add_role('company', 'Company', $adm->capabilities);
$wp_roles->add_role('user', 'User', $adm->capabilities);
}
function redirect_from_front_page() {
$redirect_url = '/user-dashboard';
$expected_role = 'user';
if( is_front_page() ) {
return;
}
if( is_user_logged_in() ) {
return;
}
$user = wp_get_current_user();
$roles = $user->roles;
if ( in_array( $expected_role, $roles ) ) {
wp_safe_redirect( $redirect_url );
exit;
}
}
You have condition is_user_logged_in() which prevents to get into the redirect part of code, only logged in user have a role.
if( is_user_logged_in() ) {
return; // when user is logged in this is last instruction
}
// code below will not get executed
$user = wp_get_current_user();
$roles = $user->roles;

WordPress how to give access to a page only for subscribers

I am trying to give access of the specific page just for my subscriber user role, if someone else with other then subscriber role or non role user want to access to this page redirect them to a custom login page, I tried with below snippet but I think something is wrong with it, can you please help me to fix this
Example : Restrict access to specific pages
add_action( 'template_redirect', function() {
// Get global post
global $post;
// Prevent access to page with ID 1052
$page_id = 1052;
if ( is_page() && ( $post->post_parent == $page_id || is_page( $page_id ) ) ) {
// Set redirect to true by default
$redirect = true;
// If logged in do not redirect
// You can/should place additional checks here based on user roles or user meta
if ( current_user_can( 'subscriber' ) || !is_user_logged_in()) {
$redirect = false;
}
// Redirect people without access to login page
if ( $redirect ) {
wp_redirect( get_site_url().'/custom-url-login' ); exit;
}
}
} );
You could refactor your code like this:
add_action( 'template_redirect', 'checking_current_user' );
function checking_current_user()
{
global $post;
global $current_user;
$page_id = 1052;
if (
( $post->post_parent == $page_id || is_page( $page_id ) )
&&
( !in_array('subscriber', $current_user->roles) )
)
{
wp_safe_redirect( site_url('/custom-url-login') );
exit;
}
}
This answer has been tested and works fine!
Wouldn't this do what you asked for?
// If user role 'subscriber' & logged in do not redirect
// You can/should place additional checks here based on user roles or user meta
if ( current_user_can( 'subscriber' ) && is_user_logged_in()) {
$redirect = false;
}
WordPress Admin Dashboard > Appearance > Theme File Editor > Theme Functions functions.php
Edit code by adding to the bottom:
// Allow subscribers to see Private posts and pages
$subRole = get_role( 'subscriber' );
$subRole->add_cap( 'read_private_pages' );
$subRole->add_cap( 'read_private_posts' );
Then change your chosen Pages / Posts from Public to Private visibility:
Pages > All Pages > Page > Quick Edit > Private (check box)
You can do a redirection, replace siteurl at the bottom of functions.php using:
// Redirect to page on login
function loginRedirect( $redirect_to, $request_redirect_to, $user ) {
if ( is_a( $user, 'WP_User' ) && $user->has_cap( 'edit_posts' ) === false ) {
return get_bloginfo( 'siteurl' );
}
return $redirect_to; }
add_filter( 'login_redirect', 'loginRedirect', 10, 3 );
or URL redirection to the "page only for subscribers" e.g:
https://WordPress.com/wp-login.php?user_id=3&redirect_to=https%3A%2F%2Fwordpress.com%2Fabout%2F#top
First of all you must need to check user is logged-in or not, If user is not logged-in then you need to redirect user to login page, After to login you need to apply code code then it will work fine.
add_action( 'template_redirect', 'check_current_user' );
function check_current_user(){
global $post, $current_user;
$page_id = 1052;
if ( is_page() && ( $post->post_parent == $page_id || is_page( $page_id ) ) ) {
if ( is_user_logged_in() ) {
/*==Now check user is subscriber==*/
$current_user = wp_get_current_user();
$role = $current_user->roles;
$currentRole = $role[0];
if($currentRole == "subscriber"){
$redirect = false;
}else{
$redirect = true;
}
}else{
wp_safe_redirect( site_url('/custom-url-login') );
}
}
}

Contact Form 7 (CF7) radio button to change User Role on WordPress

I have a form setup for users to fill in after they register on my WordPress site. Ive setup the form using Contact Form 7, and have a radio button called radio-766 that has two options: subscriber and customer.
I want the user to pick one of these two options, then when they submit the form, it will change their user role.
Below is what I have so far... I've grabbed snippets from online and tried to create my own, but it isn't working. Any ideas on how I can get this to work?
function tm_cf7_roles_posted_data( $posted_data ) {
// Stop if user is not logged in.
if ( ! is_user_logged_in() )
return;
ob_start();
$role = sanitize_key( $posted_data['radio-766'] );
if ( ! in_array( $role, array( 'subscriber', 'customer' ) ) )
return;
$user = new WP_User( get_current_user_id() );
$index = key( $user->roles );
$user_role = $user->roles[ $index ];
$output = ob_get_contents();
ob_end_clean();
return $output;
}
add_action( 'wpcf7_posted_data', 'tm_cf7_roles_posted_data' );
Should I be including the form name or ID anywhere? Can't find info on this
Any help is so appreciated!
EDIT
I feel like there is nothing connecting this function to the CF7 form named "After LinkedIn", so I found this snippet of code, just not sure how to integrate and get working
if (!isset($cfdata->posted_data) && class_exists('WPCF7_Submission')) {
// Contact Form 7 version 3.9 removed $cfdata->posted_data and now
// we have to retrieve it from an API
$submission = WPCF7_Submission::get_instance();
if ($submission) {
$formdata = $submission->get_posted_data();
}
} elseif (isset($cfdata->posted_data)) {
// For pre-3.9 versions of Contact Form 7
$formdata = $cfdata->posted_data;
} else {
// We can't retrieve the form data
return $cfdata;
}
// Check this is the user registration form
if ( $cfdata->title() == 'After LinkedIn') {
As per the Contact form 7 plugin author is_user_logged_in() will work on below to cases in form submission:
subscribers_only: true set in the additional setting in form. OR
Set WPCF7_VERIFY_NONCE to true using add_filter( 'wpcf7_verify_nonce', '__return_true' );
For more information click here.
Also, to change the user role you can do the following:
add_action( 'wpcf7_posted_data', 'tm_cf7_roles_posted_data' );
function tm_cf7_roles_posted_data( $posted_data ) {
$submission = WPCF7_Submission::get_instance();
$wpcf7 = WPCF7_ContactForm::get_current();
$formID = $wpcf7->id();
if ( $submission ) {
if( $formID == "YOUR-FORM-ID" ) {
if ( is_user_logged_in() ) {
$role = sanitize_key( $posted_data['radio-766'][0] );
if ( ! in_array( $role, array( 'subscriber', 'customer' ) ) )
return;
// Getting the WP_User object
$user = wp_get_current_user();
// The user exists
if( $user && $user->exists() ) {
// Check if user already has that role or not
if ( !in_array( $role, (array) $user->roles ) ) {
// Remove all the previous roles from the user and add this one
// This will also reset all the caps and set them for the new role
$user->set_role( $role );
}
}
}
}
}
}
If you only want to add a new role to the user and retain the existing role then instead of set_role use below:
// Add a new role to the user while retaining the previous ones
$user->add_role( $role );
As for programatically setting a user role you can just take the user object and use the set_role() function to change their role to whatever you want as long as that role has been defined.Try this way
function tm_cf7_roles_posted_data( $posted_data ) {
// Stop if user is not logged in.
if ( ! is_user_logged_in() )
return;
ob_start();
$role = sanitize_key( $_POST['radio-766'] );
if ( ! in_array( $role, array( 'subscriber', 'customer' ) ) )
return;
$user = new WP_User( get_current_user_id() );
$index = key( $user->roles );
$user_role = $user->set_role($role);
$output = ob_get_contents();
ob_end_clean();
return $output;
}
add_action( 'wpcf7_posted_data', 'tm_cf7_roles_posted_data' );

How to redirect a user off a page by user role?

A lot of these guides are 'redirect after login' I have mulitple user types with different levels of access, I'd like to write some code which if a 'Subscriber' role goes onto the page (Which they do not have access it'll redirect them.
Here's one of the many code snippets I've tried, but can't get it to work! Help :)
function consultant_page_redirect() {
$current_user = wp_get_current_user();
$role_name = $current_user->roles[0];
if (('subscriber' === $role_name ) &&( is_single( array(2864, 2630)) ) ) {
wp_redirect('/trial-user-page/');
}
}
function consultant_page_redirect( $user_login, $user ) {
$role_name = $user->roles[0];
if (('subscriber' === $role_name ) &&( is_single( array(2864, 2630)) ) ) {
wp_redirect('/trial-user-page/');
}
}
add_action('wp_login', 'consultant_page_redirect', 10, 2);
Add this snippet code on child theme functions.php

Wordpress: check for custom user role in frontend

I simply want to display some message when a user has a custom user role. It works with native user roles, but not with custom ones. Here is my code:
<?php
$user_ID = get_current_user_ID();
$user = new WP_User( $user_ID );
$current_user = $user->roles[0];
if( $current_user == 'client' ){
echo 'hello client';
} else {
// do nothing
}
?>
Any help is welcome, thanks!
You can try this :
if ( is_user_logged_in() )
{
global $current_user;
$user_role = $current_user->roles[0];
if($user_role == 'client')
{
// do something
}
}
is_user_logged_in() is used to check whether user is logged in or not
The examples above are problematic if the User has more than one Role set, as the code only checks the first Role the user has set.
Ideally, you should be checking if any of the User's Roles match the custom role:
if ( is_user_logged_in() ) {
global $current_user;
if( in_array('YOUR_CUSTOM_ROLE', $current_user->roles) ) {
echo 'User has Custom Role';
} else {
echo 'User does not have Custom Role';
}
}

Categories