Accueil » Coding » Divi » Show some layouts only to defined users

Perhaps do you remember my post about the exclusion of some pages and posts from the WordPress back office well this time, we will add to the functions.php file of our child theme, the code needed to hide some elements of the Divi library.

So maybe you’ll ask me, “Why?” and quite simply because as a web developer, it happens very regularly to me to create elements (modules, sections, rows or full layouts) that I wish to “inject” in certain places of the site on which I work. This can be in the header, before or after the content, in the footer, in short: pretty much everywhere :).

The problem is that when I deliver the site to my client, he’s not always aware of the importance of these layouts and their uses. Or with time, he forgets and one day wishing to do some “cleaning” it removes items from this library… The result, you can imagine, is a site that will not work quite as before :)…

So, to avoid this, I wrote a small function that limits access to these models to only “competent people” (Not a rookie 🙂 ).

The code to display hidden layouts only to admins:

function exclude_some_layouts_from_admin($query) {
global $pagenow, $post_type;
	if ( ! is_admin() )
		return $query;

	$user = current_user_can('administrator');

	if ( $user!=1 && $pagenow == 'edit.php' && $post_type == 'et_pb_layout' ) {
		$query->query_vars['post__not_in'] = array( 600, 608 ); /* id of the layouts to hide */
	}
}
add_filter( 'parse_query', 'exclude_some_layouts_from_admin' );

The code to display hidden layouts only to users whose email address is allowed:

function exclude_some_layouts_from_admin_by_mail($query) {
global $pagenow, $post_type;

	if ( ! is_admin() )
		return $query;

	$emails = array('adresse1@domain.tld', 'adresse2@domain.tld');
	$user = wp_get_current_user();
	$user = (in_array($user->user_email, $emails)) ? 1 : 0;

	if ( $user!=1 && $pagenow == 'edit.php' && $post_type == 'et_pb_layout' ) {
		$query->query_vars['post__not_in'] = array( 600, 608 ); /* id of the layouts to hide */
	}
}
add_filter( 'parse_query', 'exclude_some_layouts_from_admin_by_mail' );

Here you have the choice between limiting access to layouts to the “Administrator” role or limit their access to a list of email addresses :).

Have fun 😀 !!

 

If you wish to help me to make this blog alive, you can make a donation, below :).
Processing ...

 

Information: Truly conquered by this theme, I registered on the site of ElegantThemes as an affiliate, which means that if you click on the link below and buy the theme I will receive a commission :).

ElegantThemes

 

Après avoir visité les coulisses, découvrez le site :) !

Submit a Comment

Your email address will not be published. Required fields are marked *

Accueil » Coding » Divi » Show some layouts only to defined users
Share This