I want to redirect the users of my site to a custom thank you page after they create a page and send it for review.
I found this snippet, but this one is for posts, and it's for publishing, not for pending review. The role that I want to use for this snippet is Tutor Instructor.
Can somebody help me to edit this snippet? It's a WordPress site.
add_action( 'save_post', 'redirect_user_page_list', 10, 3 );
function redirect_user_page_list( $post_ID, $post, $update ) {
if( is_user_logged_in() ) {
$user = wp_get_current_user();
$role = ( array ) $user->roles;
if ( 'user_role' == $role[0] ) {
$url = '';
wp_redirect($url);
exit;
}
}
}
//replace user_role with your actual role the one for which you need to implement this functionality. Also place the proper url in $url.
You can execute this code after your code
window.location.href = 'https://yoursite.com/thank-you';
Related
I am trying to redirect users from one page to my account page of woocommerce while not effecting after registration behavior of other pages, I am trying to use this code snippet in functions.php of my child theme but it does not seems to work.
add_filter( 'woocommerce_registration_redirect', 'custom_redirection_after_registration', 10, 1 );
function custom_redirection_after_registration( $reg_url, $user ){
if( str_contains( $reg_url, 'login-or-register' ) ) {
$reg_url = get_permalink( wc_get_page_id( 'myaccount' ) );
}
return $reg_url;
}
I will be glad get any help.
Thanks in advance.
You have access to $_SERVER variable. you can check the request referral link and set your conditions.
I hope this helps,
add_filter( 'woocommerce_registration_redirect', 'custom_redirection_after_registration', 10, 1 );
function custom_redirection_after_registration( $reg_url ) {
if ( str_contains( $_SERVER['REQUEST_URI'], 'login-or-register' ) ) {
$reg_url = get_permalink( wc_get_page_id( 'myaccount' ) );
}
return $reg_url;
}
I want to give access Woo product single page only for Logged in users.
I have a product listing on the Homepage and Shop page, What I want is: when a logged out user click on a Product, the user will get a Login form and after Logged in user will be redirected to the Product page that he wanted to View.
So the flow will be something like:
Home/Shop-> click to Product X -> Login page -> Redirect to Product X single page
Currently, I am using the regular Woo Login form created by this function
woocommerce_login_form()
I am trying bellow code snippets:
add_filter('login_redirect', 'my_login_redirect', 10, 3);
function my_login_redirect() {
$location = $_SERVER['HTTP_REFERER'];
var_dump($location);
wp_safe_redirect($location);
exit();
}
}
add_action('init','my_login_redirect');
function my_login_redirect() {
$location = $_SERVER['HTTP_REFERER'];
var_dump($location);
//wp_safe_redirect($location);
}
-----------------------
AND ALSO THIS ONE
-----------------------
function redirect_after_login(){
global $wp;
$protocol='http';
if (isset($_SERVER['HTTPS']))
if (strtoupper($_SERVER['HTTPS'])=='ON')
$protocol='https';
if (!is_user_logged_in() && is_product() ){
$redirect = site_url() . "/my-account.php?redirect_to= $protocol://" .
$_SERVER["HTTP_HOST"] . urlencode($_SERVER["REQUEST_URI"]);
wp_redirect( $redirect );
exit;
}
}
add_action( 'wp', 'redirect_after_login', 3 );
In both of cases, the problem is, it always find the Login page as HTTP_REFERER / REQUEST_URI
Because currently, I am using below code to redirect Non-logged-in user who is trying to see the product page to the Login page:
add_action('template_redirect', 'ethis_redirect_for_loggedin_users');
function ethis_redirect_for_loggedin_users() {
if ( !is_user_logged_in() && is_product() ) {
wp_redirect(site_url().'/default-login');
exit;
}
}
You can use the template_redirect filter hook to prevent guest users to access a single product page.
add_action( 'template_redirect', 'wc_redirect_non_logged_to_login_access');
function wc_redirect_non_logged_to_login_access() {
if ( !is_user_logged_in() && is_singular( 'product' ) ) {
global $post;
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id')).'?redirect='.get_the_permalink( $post->ID ) );
exit();
}
}
Then after you have to use the woocommerce_login_redirect filter hook for login redirect.
add_filter( 'woocommerce_login_redirect', 'my_login_redirect', 10, 2 );
function my_login_redirect( $redirect, $user ) {
if( isset( $_GET['redirect'] ) && $_GET['redirect'] != '' ){
return $_GET['redirect'];
}
return $redirect;
}
Code will go in your active theme functions.php Tested and Works.
I created a new user and i gave him “author” role. I want him to only can write posts in wordpress. But when he logs in, he enters the user profile but not wordpress dashboard which is where i want. How can i fix this? I want him to go to wordpress dashboard so he can write posts only.
Try this,
<?php
// check if current user is the post author
global $current_user;
get_currentuserinfo();
if (is_user_logged_in() && $current_user->ID == $post->post_author) {
wp_redirect( admin_url( 'the url you need to redirect' ) );
exit;
}
?>
Add this code
function check_custom_authentication () {
$user_id = get_current_user_id();
$user_meta = get_userdata($user_id);
$user_roles = $user_meta->roles;
if ( in_array('author', $user_roles, true ) ) {
wp_redirect(admin_url());
exit;
}
}
add_action( 'wp_login' , 'check_custom_authentication' );
I am looking for a way to redirect when viewing any publication, except the author of the publication.
There are two roles in the "author" and "custom_role" site. This last role is allowed to see all.
the role of author can only see his own, the rest redirects.
I've tried for a while, in this last code I'm working but it does not work and I do not know why
Thanks very much!
add_action( 'pre_get_posts', function() {
if( is_author() )
{
if( ! is_user_logged_in() )
{
wp_redirect( 'https://aaa.com/custom' );
exit;
}
$author = get_queried_object();
if( $author->ID != get_current_user_id() )
{
wp_redirect( get_author_posts_url(
get_current_user_id() ) );
exit;
}
}
} );
First of all, is_author() is used to check if current page is author archive page. Please check following example. This may not be the exact anster but it may help. In the single post, post author and current user ID is compared. If they are not same then it is redirected to home page. If those IDs are same then, current user is also the post author, so current user will be allowed to view page.
add_action( 'get_header', 'wpso_author_redirection' );
function wpso_author_redirection() {
if ( is_singular() ) {
$current_post_details = get_post( get_the_ID(), ARRAY_A );
$user_id = get_current_user_id();
if ( $user_id !== absint( $current_post_details['post_author'] ) ) {
wp_redirect( home_url() );
}
}
}
I have an existing php portal - www.oureducation.in. now I was integrating a wordpress answers theme to it on www.oureducation.in/answers .
Now, I need to do following things
- Register a user on wordpress when a user registers on mysite. this I have done using triggers.
- login only from mysite. If a user logs in on mysite, he should be logged in on wordpress as well.(I tried doing this but with a lot of errors)
- logout from mywebsite and user should be logged out from wordpress as well.(user session_destroy but not working properly.)
-logout from wordpress and user shall be logged out from my site as well.(not at all working)
I thing i have made a whole mess of it. So i want to start from start. please guide me how to carry out these integration.
I have been through this link -http://codex.wordpress.org/Integrating_WordPress_with_Your_Website
but not of much help to me.
Added following code to wp theme`s functions.php
add_action( 'setup_theme', 'wp_session_oureducation' );
function wp_session_oureducation() {
global $wpdb;
if( !$current_user->data->ID ) {
if( !empty ($_SESSION['User_id'] ) ) {
$fetchidquery = "Select ID from $wpdb->users where user_email = '".$_SESSION['E-mail']."'";
$thisuserid = $wpdb->get_row($fetchidquery);
wp_set_current_user( $thisuserid->ID,'');
$user_id = $thisuserid->ID;
}
}
}
add_action( 'wp_head', 'wp_session_oureducation_head' );
function wp_session_oureducation_head() {
global $wpdb;
if( !$current_user->data->ID ) {
if( !empty ( $_SESSION['User_id'] ) ) {
$fetchidquery = "Select ID from $wpdb->users where user_email = '".$_SESSION['E-mail']."'";
$thisuserid = $wpdb->get_row( $fetchidquery );
wp_set_current_user( $thisuserid->ID,'');
$user_id = $thisuserid->ID;
}
}
}
Thanks