How can i limit a user in wordpress - 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 :-)

Related

(WordPress woocommerce)How to hide cart page checkout button for some user role

How to hide cart page checkout button for some user role in woocommerce hope there are some codes that I can use for function.php, thank you
Check this sample code. Here I remove the proceed to checkout button for the editor role users.
function remove_proceed_to_checkout_button() {
if( is_user_logged_in() ) {
$user_id = get_current_user_id();
$get_user_data = get_userdata( $user_id );
$role = $get_user_data->roles[0];
if('editor' == $role ) {
remove_action('woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
}
}
}
add_action('init', 'remove_proceed_to_checkout_button');
Hope this will help.

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

How to hide and disable Wordpress admin menu items

I have two Wordpress Administrator accounts but I need to hide certain admin menu items for one admin. I've out found how to do this but how can I disable directly accessing the page by url? Something like this Advanced Access Manger
So far I have..
function hide_menu() {
$user = wp_get_current_user();
if($user && isset($user->user_login) && 'admin2' == $user->user_login) {
remove_menu_page( 'plugins.php' ); //Plugins
remove_menu_page( 'tools.php' ); //Tools
remove_menu_page( 'users.php' ); //Users
}
}
add_action('admin_head', 'hide_menu');
Easy Solution:
You can use this plugin:
https://wordpress.org/plugins/prevent-direct-access/
Coding Solution:
add_action('template_redirect', function() {
// ID of the page you want to restrict from
if (!is_page(2072)) {
return;
}
// Provide the link of admin dashboard, so that page should only
// accessible
// through admin dashboard
if (wp_get_referer() === 'https://example.com/wp-admin/') {
return;
}
// we are on restricted page
// visitor is trying to access directly
// so redirect to home
wp_redirect(get_home_url());
exit;
} );

How to send user to wordpress dashboard?

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

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

Resources