Hide all pages except one from specific user in WordPress backend - php

I'm trying to hide all pages except one specific from an user (ID=14). This is what I got so far with the help of this post »https://www.johnparris.com/how-to-hide-pages-in-the-wordpress-admin/«:
function jp_exclude_pages_from_user($query) {
if ( ! is_admin() )
return $query;
global $pagenow, $post_type;
if ( !$current_user->14 && $pagenow == 'edit.php' && $post_type == 'page' )
$query->query_vars['post__in'] = array( '10' ); // Enter your page IDs here
}
add_filter( 'parse_query', 'jp_exclude_pages_from_user' );
Result is that my site stops working.

There's an issue with your code. You are fetching user id outside the function. In that case the code should be as below:
function jp_exclude_pages_from_admin($query) {
if ( ! is_admin() )
return $query;
global $pagenow, $post_type;
$current_user_id = get_current_user_id();
if ( $current_user_id == 'youruserid' && $pagenow == 'edit.php' && $post_type == 'page' )
$query->query_vars['post__not_in'] = array( 'yourpageid' ); // Enter your page IDs here
}
add_filter( 'parse_query', 'jp_exclude_pages_from_admin' );
In the above section you forgot to put $current_user_id that's why it got suspended. Hopefully this code will work. Let me know if it works or not. I've tested it out & it works perfectly on my end

Related

Change default sorting in Woocommerce order list

How can the default sorting of the Woocommerce order list be changed. For now each time I enter the order page, orders are displayed as DESC. I would like it to be ASC by default each time I enter the order page. Can anyone help?
As I found another custom order solution in the forum:
sort-orders-by-paid-date-in-woocommerce-admin-order-list
I edited the code mentioned in there to the following, which worked out:
function action_parse_query( $query ) {
global $pagenow;
// Initialize
$query_vars = &$query->query_vars;
// Only on WooCommerce admin order list
if ( is_admin() && $query->is_main_query() && $pagenow == 'edit.php' && $query_vars['post_type'] == 'shop_order' ) {
// Set
$query->set( 'orderby', 'date' );
$query->set( 'order', 'ASC' );
}
}
add_action( 'parse_query', 'action_parse_query', 10, 1 );
These code will do the work.
add_filter( 'woocommerce_order_query_args', function ( $args ) {
$args['order'] = 'ASC';
return $args;
} );

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

Disable WooCommerce payment gateway for guests and specific user roles

I have disabled the invoice payment mehtod for one of the user roles in my site ('customer') but now I need to add another user role ('business') to this rule and I can't figure out how to make it work. When I add the second role, the code stops working altogether and it ends up showing the gateway to all users.
Here's the code I'm using to disable the gateway:
I'm not very experienced with PHP so any help will be tremendously appreciated.
If there's any chance you can correct my code to fit the use case, I would be very grateful.
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_private' );
function payment_gateway_disable_private( $available_gateways ) {
$user = wp_get_current_user();
if ( isset( $available_gateways['igfw_invoice_gateway'] ) && !is_user_logged_in() || isset( $available_gateways['igfw_invoice_gateway'] ) && in_array('customer', $user->roles) ) {
unset( $available_gateways['igfw_invoice_gateway'] );
}
return $available_gateways;
}
Thoughts?
There is a mistake in your if statement (also you can use current_user_can() function for user roles) like:
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_private' );
function payment_gateway_disable_private( $available_gateways ) {
if ( ( ! is_user_logged_in() || current_user_can('customer') || current_user_can('business') )
&& isset( $available_gateways['igfw_invoice_gateway'] ) ) {
unset( $available_gateways['igfw_invoice_gateway'] );
}
return $available_gateways;
}
or with the global $current_user; and array_intersect() function:
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_private' );
function payment_gateway_disable_private( $available_gateways ) {
global $current_user;
// Here define your user roles
$user_roles = array( 'customer', 'business' );
if ( ( ! is_user_logged_in() || array_intersect( $current_user->roles, $user_roles ) )
&& isset( $available_gateways['igfw_invoice_gateway'] ) ) {
unset( $available_gateways['igfw_invoice_gateway'] );
}
return $available_gateways;
}
It should better work now.

How to hide specific page (from wp-admin) for certain users in wp?

My Image
I just wanted to hide specific page for certain users.
function remove_menus(){
// get current login user's role
$roles = wp_get_current_user()->roles;
// test role
if( in_array('administrator',$roles)){
remove_menu_page( 'edit-comments.php' ); //Posts
remove_menu_page( 'tools.php' );
remove_menu_page('edit.php');
remove_menu_page('wpcf7');
}
}
add_action( 'admin_menu', 'remove_menus' , 100 );
This what I am tried upto now and its working fine for all the page.
My question is I dont want to show home - Front page (Please see my image) If logged in user is not admin. and also I want to hide add new
You can use user's role capabilities and allow on the basis of role for add new items.
function manage_user_action() {
// get current login user's role
$roles = wp_get_current_user()->roles;
if( !in_array('administrator',$roles)){
//remove capabilities
$roles->remove_cap( 'edit_pages');
}
}
add_action( 'admin_init', 'manage_user_action');
To Remove Page from list
function jp_exclude_pages_from_admin($query) {
global $pagenow, $post_type;
if ( !current_user_can( 'administrator' ) && $pagenow == 'edit.php' && $post_type == 'page' )
$query->query_vars['post__not_in'] = array( '10'); // Enter your page IDs here
//don't forget to the query
return $query;
}
add_filter( 'parse_query', 'jp_exclude_pages_from_admin' );
For more help see this link : Click Here
I have extended #dineshkashera answer to target a specific user by ID and the code should be as follows :
function jp_exclude_pages_from_admin($query) {
global $pagenow, $post_type;
$current_user_id = get_current_user_id();
if ( $current_user_id == 2 && $pagenow == 'edit.php' && $post_type == 'page' )
$query->query_vars['post__not_in'] = array( '11', '12','13' ); // Enter your page IDs here
}
add_filter( 'parse_query', 'jp_exclude_pages_from_admin' );
This code will get the user with ID of 2 and remove the pages IDs 11,12 and 13 from their WP page edit screen.

Change default order of pages in WordPress Admin / Backend

I am trying to change the default sorting order of the pages in my WordPress backend. I know this can easily be done by clicking on the tab "Title", "Date" or "ID" but those are merely one-time settings and I need a global = default solution.
I went ahead and tried using this function which to me makes perfect sense but it just doesn't work with WordPress 4.2.3 :-(
function set_post_order_in_admin( $wp_query ) {
global $pagenow;
if ( is_admin() && 'edit.php' == $pagenow && !isset($_GET['orderby'])) {
$wp_query->set( 'orderby', 'title' );
$wp_query->set( 'order', 'asc' );
}
}
add_filter('pre_get_posts', 'set_post_order_in_admin', 5 );
Any idea why this is not working any more? How can I achieve that?
Thanks + regards,
Henning
Just change order "ASC" to "DESC" in your own code, it will work perfectly. Or copy and paste below mentioned code into your functions.php :
function set_post_order_in_admin( $wp_query ) {
global $pagenow;
if ( is_admin() && 'edit.php' == $pagenow && !isset($_GET['orderby'])) {
$wp_query->set( 'orderby', 'title' );
$wp_query->set( 'order', 'DESC' );
}
}
add_filter('pre_get_posts', 'set_post_order_in_admin', 5 );
Use this snippet of code :
function set_post_order_in_admin( $wp_query ) {
global $pagenow;
if ( is_admin() && 'edit.php' == $pagenow && !isset($_GET['orderby'])) {
$wp_query->set( 'orderby', 'title' );
$wp_query->set( 'order', 'DSC' );
}
}
add_filter('pre_get_posts', 'set_post_order_in_admin' );

Categories