I'm fairly new to Wordpress so not sure what I'm doing wrong. On my Wordpress site, users are able to create custom post types (products). I'm looking to disallow access to a page like this: example.com/post-a-listing by redirecting users to another page if they didn't create at least one product. Here's what I've tried but this isn't working..
function yoursite_user_has_posts($user_id) {
$result = new WP_Query(array(
'author'=>$user_id,
'post_type'=>'product',
'post_status'=>'publish',
'posts_per_page'=>1,
));
return (count($result->posts)!=0);
}
add_action( 'template_redirect', 'redirect_to_specific_page' );
function redirect_to_specific_page() {
if ( is_page('post-a-listing') && ! yoursite_user_has_posts($user_id) ) {
wp_redirect( 'https://example.com/', 301 );
exit;
}
The issue for the function is $user_id actually , from where you can get the $user_id with out referencing it?
Use get_current_user_id() inside the function to get the current User ID.
function redirect_to_specific_page() {
$user_id = get_current_user_id();
if ( is_page('post-a-listing') && ! yoursite_user_has_posts($user_id) ) {
wp_redirect( 'https://example.com/', 301 );
exit;
}
}
Related
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') );
}
}
}
This is a query based around WooCommerce Membership and Subscriptions.
I must add that I'm also trying to decide if it's good UX to do what I'm doing.
There are many solutions to redirecting users after they log in but I have a situation where I want to redirect a user with the role of 'subscriber' when they click on specific links to pages that describe and allow you to become a member. So although I don't want to hide 'join now' etc I just want those to redirect to the my-account page.
Again there are various roles and redirect plugins but none seem to help in this specific scenario. So the source of the code I've used is here: SOURCE and I'm looking to do something like this:
function eks_redirect_users_by_role() {
global $post;
$current_user = wp_get_current_user();
$role_name = $current_user->roles[0];
if ( 'subscriber' === $role_name && $post->ID == 47145) {
wp_redirect( '/my-account' );
}
}
add_action( 'admin_init', 'eks_redirect_users_by_role' );
So if the user role is a subscriber and they try and visit the page idea it's redirected.
At the current time, it does fall back to a product page which says 'you already have a membership' but it's multiple steps to arrive.
This might be more useful and proper way to achieve your request.
function redirection_based_user_role(){
$current_user = wp_get_current_user();
$role_name = $current_user->roles[0];
$postid = get_the_ID();
if ( 'subscriber' === $role_name && $postid == 47145 ) {
wp_redirect( home_url( '/my-account' ), 301 );
exit;
}
}
add_action( 'template_redirect', 'redirection_based_user_role' );
Hope this works.
I was able to achieve want I wanted to the following way:
function eks_redirect_user() {
$current_user = wp_get_current_user();
$role_name = $current_user->roles[0];
$postid = get_the_ID();
if ( 'subscriber' === $role_name && $postid == 47145 ) { ?>
<script>
function redirectPage() {
window.location = "/my-account/";
}
redirectPage();
</script>
<?php
exit;
}
}
add_action('wp_footer', 'eks_redirect_user' );
The issue is it's a fairly untidy solution as the redirect feels odd. I'm not sure if using wp_redirect would work better. I decided to do was just disable the button on the page with the main membership information rather than redirecting every call to action to the account page which seems like a more elegant solution.
I would like no redirect not logged in user who is trying to access a specific product, to My Account page to help them register (in WooCommerce).
I'm using the code below, but at present all the products redirect to My account:
function reg_redirect(){
if( is_product(1735) && !is_user_logged_in() ) {
wp_redirect( 'https://www.la-chaine-maconnique.fr/my-account/' );
exit();
}
}
add_action('template_redirect', 'reg_redirect');
An idea what's wrong ?
That's because is_product() doesn't accept a post ID as an argument like is_single() and similar functions do.
You could simply use get_the_ID(), though:
function reg_redirect(){
if( 1735 == get_the_ID() && !is_user_logged_in() ) {
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );
exit();
}
}
add_action('template_redirect', 'reg_redirect');
Note that you should also use get_permalink() and get_option() together to get the URL of your account page, rather than 'hard-coding' it (passing it as a string).
I am unfamiliar with woocommerce, but according to https://docs.woocommerce.com/wc-apidocs/function-is_product.html, I don't think is_product is the function you want. Try this.
global $product;
function reg_redirect(){
$id = $product->get_id();
if( $id == 1735 && !is_user_logged_in() ) {
wp_redirect( 'https://www.la-chaine-maconnique.fr/my-account/' );
exit();
}
}
add_action('template_redirect', 'reg_redirect');
i have a page where there is a permanent slug and another page where slug is keep on changing. i want to redirect user if he is not logged in.
i am using this:-
add_action( 'template_redirect', 'redirect_to_specific_page' );
function redirect_to_specific_page() {
if ( (is_page( array( 'administration', 'documents', 'images' )) && ! is_user_logged_in()) ) {
wp_redirect( get_site_url().'/log-in/', 301 );
exit;
}
}
This is working if it has permanent slug as in if condition but how can i redirect user if page URL is like http://siteurl/portfolio/this-is-image-5/ where this-is-image-5 is keep on changing based on portfolio posts. so it can be this-is-image-6, this-is-image-7 and so on. So please tell me how to redirect based on this dynamic slug for non-logged in user.
Any help will be highly appreciated
You can check current post type and then redirect. Check this code:
add_action( 'template_redirect', 'redirect_to_specific_page' );
function redirect_to_specific_page() {
global $post;
if ( ($post->post_type == 'portfolio') && ! is_user_logged_in()) ) {
wp_redirect( get_site_url().'/log-in/', 301 );
exit;
}
}
Here is the code in my functions.php that redirects users (to their new "Sample Page") when they log in:
function admin_default_page() {
return '/wp-admin/post.php?post=2&action=edit';
}
add_filter('login_redirect', 'admin_default_page');
The problem is that the url to which we redirect is missing the users site url.
For example, if a user creates a site "Bobs Trucks", it redirects them to
example.com/wp-admin/post.php?post=2&action=edit (does not work)
instead of
example.com/bobstrucks/wp-admin/post.php?post=2&action=edit
I've tried all the WordPress functions to get the full url needed (example.com/bobstrucs/url-to-redirect-to) but have not succeeded.
I'm trying a rather hackish solution where I get the user ID form the logged in user, and get all the blogs the user has (= just one, "Bobs Trucks"):
function admin_default_page() {
global $current_user;
get_currentuserinfo();
$blogs = get_blogs_of_user( $current_user->ID);
foreach($blogs as $blog) { $userblog = $blog->siteurl; }
return $userblog .'/wp-admin/post.php?post=2&action=edit';
}
add_filter('login_redirect', 'admin_default_page');
But this one has the problem of not working the first time user logs in.
Thank you very much!
Try something like this:
function admin_default_page() {
if ( is_user_logged_in() && is_admin() ) {
$blog_id = get_current_blog_id();
$blog = get_blog_details( array( 'blog_id' => $blog_id ) );
$location = '/' . $blog->blogname . '/wp-admin/post.php?post=2&action=edit';
wp_redirect( $location );
exit;
}
}
add_filter( 'login_redirect', 'admin_default_page' );
Refs:
http://codex.wordpress.org/Function_Reference/get_blog_details
http://codex.wordpress.org/Function_Reference/wp_redirect