How to send user to wordpress dashboard? - wordpress

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' );

Related

Redirect User After creating a page

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';

Redirect to URL if the user isnt the author of the post

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() );
}
}
}

Restrict registered users from altering their own biographical info

I want to restrict all users from altering their own 'Biographical Info' (only I the admin should be able to edit/update it) in Dashboard->Users->Your Profile.
You can do that with following;
<?php
add_action( 'admin_init', 'disable_profile_edit' );
function disable_profile_edit() {
remove_menu_page( 'profile.php' );
remove_submenu_page( 'users.php', 'profile.php' );
if(IS_PROFILE_PAGE === true && ! current_user_can( 'manage_options' )) {
wp_redirect( home_url() );
exit;
}
}
?>
You can put only code part to functions.php. If anyone (except the users has role manage_options) tries to access profile page, it will be denied.
Note:
Put ;
define('IS_PROFILE_PAGE', true);
in profile.php

How can i limit a user in wordpress

Can any one please tell me how can i limit a user to edit only his post.I used the role editor plugin but it allow the user to edit all users post.I'm creating a classified site plugin where a user can post(custom post type)and he can edit his post.
You can user advanced access manager plugin for same.
You can limit a user to only edit their own posts using this bit of code.
function my_authored_content($query) {
//get current user info to see if they are allowed to access ANY posts and pages
$current_user = wp_get_current_user();
// set current user to $is_user
$is_user = $current_user->user_login;
//if is admin or 'is_user' does not equal #username
if (!current_user_can('manage_options')){
//if in the admin panel
if($query->is_admin) {
global $user_ID;
$query->set('author', $user_ID);
}
return $query;
}
return $query;
}
add_filter('pre_get_posts', 'my_authored_content');
function remove_menu_items() {
$current_user = wp_get_current_user();
if ( !current_user_can( 'manage_options' ) ) {
//hides comments menu
remove_menu_page( 'edit-comments.php' );
// hides posts menu
remove_menu_page( 'edit.php' );
hides pages menu
remove_menu_page( 'edit.php?post_type=page' );
}
}
add_action( 'admin_menu', 'remove_menu_items' );
Hope this helps you :-)

Autologin user after registration Wordpress

I am trying to autologin a user after registration.
I am trying this in functions.php file:
add_action( 'user_register', 'auto_login_user' );
function auto_login_user($user_id) {
$user = new WP_User($user_id);
$user_login_var = $user->user_login;
$user_email_var = stripslashes($user->user_email);
$user_pass_var = $user->user_pass;
$creds = array();
$creds['user_login'] = $user_login_var;
$creds['user_password'] = $user_pass_var;
$creds['remember'] = true;
$user = wp_signon( $creds, false );
if ( is_wp_error($user) )
echo $user->get_error_message();
}
I am getting ERROR:
The password you entered for the username "TheNewUserCreated" is incorrect. Lost your password?
How Can I take the password from the User object?
Also because this is a custom registration process in template registration.php I tried to take it with $_POST and run the function in that file but I didnt have any success that way too...
EDIT:
Ok I am getting the encrypted password, so what is the solution here, how can I autologin the user? Maybe I can do this in registration.php page?
Add below function to functions.php file
function auto_login_new_user( $user_id ) {
wp_set_current_user($user_id);
wp_set_auth_cookie($user_id);
// You can change home_url() to the specific URL,such as
//wp_redirect( 'http://www.wpcoke.com' );
wp_redirect( home_url() );
exit;
}
add_action( 'user_register', 'auto_login_new_user' );
If you are using wp_insert_user(); to register your users then to auto-login them it's simple.
This function returns the user ID if success, so use it to login that user.
$id = wp_insert_user($data);
//so if the return is not an wp error object then continue with login
if(!is_wp_error($id)){
wp_set_current_user($id); // set the current wp user
wp_set_auth_cookie($id); // start the cookie for the current registered user
}
but to follow what you have already it can be like this:
add_action( 'user_register', 'auto_login_user' );
function auto_login_user($user_id) {
wp_set_current_user($user_id); // set the current wp user
wp_set_auth_cookie($user_id); // start the cookie for the current registered user
}
//this code is a bit tricky, if you are admin and you want to create a user then your admin session will be replaced with the new user you created :)
function auto_login() {
$getuserdata=get_user_by('login',$_GET['login']);
$tuserid=$getuserdata->ID;
$user_id = $tuserid;
$user = get_user_by( 'id', $user_id );
if( $user ) {
wp_set_current_user( $user_id, $user->user_login );
wp_set_auth_cookie( $user_id );
do_action( 'wp_login', $user->user_login );
}
}
add_action('init', 'auto_login');
i had have the same problem as your and found this best solution. try this in your functions.php file. and one thing more use submit your reset form in a function in functions.php file

Resources