Skip to content

Instantly share code, notes, and snippets.

@dianjuar
Last active April 18, 2018 16:43
Show Gist options
  • Save dianjuar/f95c8006b0b25c6441b34c13fef27027 to your computer and use it in GitHub Desktop.
Save dianjuar/f95c8006b0b25c6441b34c13fef27027 to your computer and use it in GitHub Desktop.
With the Woocommerse plugin installed, allow some roles to access to the WordPress Dashboard using a hook.

WooCommerce admin bar and dashboard access

Do you wonder why some users at WooCommerce enabled site have access to the top admin bar and WordPress admin dashboard, but some users don’t?

It is by design: WooCommerce plugin developers decided for us for whom they allow access to WordPress admin dashboard and for whom they prohibit it. But thanks them for the professionalism – it is possible to change that default WooCommerce logic via special filters.

Garagulya, V., (2015) Woocommerce Admin Bar Access

Code used.

/**
 * Allow the "CUSTOM_ROLE" role access to the WordPress Admin
 * 
 * @param  boolean $prevent_access
 *         Woocommerce parameter. 
 *         -true:  doesn't have access
 *         -false: have access
 *         
 * @return boolean
 *         If woocommerce prevent the access to the admin bar.
 */
function allow_CUSTOM_ROLE_wp_admin_access($prevent_access)
{
	# If the current is is not the patrocinador, do nothing
	if( !is_current_user_CUSTOM_ROLE() )
		return $prevent_access;

	# Woocommerce ask itself, "prevent this user to access to the WordPress Admin?"
	return false;
}
add_filter( 'woocommerce_prevent_admin_access', 'allow_CUSTOM_ROLE_wp_admin_access');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment