Remove menu for the user which has ‘editor’ role in WordPress

We need to use ‘admin_menu’ hook to remove/hide menu from specific role type. Following code show example of this. First get role type then remove menu.

Open functions.php and write following code.


function remove_menus(){
// get current login user's role
$roles = wp_get_current_user()->roles;

// test role
if( !in_array('editor',$roles)){
return;
}

//remove menu from site backend.
remove_menu_page( 'index.php' ); //Dashboard
remove_menu_page( 'edit.php' ); //Posts
remove_menu_page( 'upload.php' ); //Media
remove_menu_page( 'edit-comments.php' ); //Comments
remove_menu_page( 'themes.php' ); //Appearance
remove_menu_page( 'plugins.php' ); //Plugins
remove_menu_page( 'users.php' ); //Users
remove_menu_page( 'tools.php' ); //Tools
remove_menu_page( 'options-general.php' ); //Settings
remove_menu_page( 'edit.php?post_type=page' ); //Pages
remove_menu_page('edit.php?post_type=testimonial'); // Custom post type 1
remove_menu_page('edit.php?post_type=homeslider'); // Custom post type 2
}
add_action( 'admin_menu', 'remove_menus' , 100 );

Please keep in mind that this code doesn’t remove user’s permission. That means user still can access menu directly from browser.

Get list of all WooCommerce categories.

$orderby = 'name';
$order = 'asc';
$hide_empty = false ;
$cat_args = array(
    'orderby'    => $orderby,
    'order'      => $order,
    'hide_empty' => $hide_empty,
);

$product_categories = get_terms( 'product_cat', $cat_args );

if( !empty($product_categories) ){
	echo '<ul>';
	foreach ($product_categories as $key => $category) {
		echo '<li>';
		echo '<a href="'.get_term_link($category).'" >';
		echo $category->name;
		echo '</a>';
		echo '<li>';
	}
	echo '</ul>';
}

Remove version parameters from WordPress javascript and css urls

In WordPress version number are append to javascript and css url. you can see these in browser by press ‘Ctrl+U’ or right click on web page then click on ‘View page source’. Continue reading “Remove version parameters from WordPress javascript and css urls”

Applying the Category to Attachments in WordPress

You can assign category to media in wordpress using following php code.

function ptt_add_categories_to_attachments() {
	register_taxonomy_for_object_type( 'category', 'attachment' );
}
add_action( 'init' , 'ptt_add_categories_to_attachments' );

Continue reading “Applying the Category to Attachments in WordPress”