How to Hide Selected Reports in WooCommerce Analytics
Explanation
If you're looking to tidy up your WooCommerce Analytics section by hiding certain reports or even the entire Analytics menu, this code snippet is just what you need.
Here's what it does:
- Hides Specific Reports: The code removes specific reports like Revenue, Orders, Products, and more from the WooCommerce Analytics section. This is done by listing the report slugs you want to hide and then removing them from the menu.
- Removes the Analytics Menu: If you want to hide the entire Analytics menu from the WordPress admin dashboard, this snippet will do that too.
- Restricts Access: It ensures that only administrators can access the Analytics section. Non-admin users will be redirected away if they try to access it.
By using this code, you can customize the WooCommerce Analytics view to show only what you need, keeping your dashboard clean and focused.
Code
<?php
// Hook into the WooCommerce Admin initialization
add_action('woocommerce_admin_init', 'wp_dudecom_customize_woocommerce_analytics');
function wp_dudecom_customize_woocommerce_analytics() {
// Check if the current user has the capability to manage WooCommerce
if (!current_user_can('manage_woocommerce')) {
return;
}
// Remove specific reports from WooCommerce Analytics
add_filter('woocommerce_analytics_report_menu_items', 'wp_dudecom_remove_analytics_reports', 10, 1);
}
function wp_dudecom_remove_analytics_reports($reports) {
// List of report slugs to remove
$reports_to_remove = array(
'revenue', // Remove Revenue report
'orders', // Remove Orders report
'products', // Remove Products report
'categories', // Remove Categories report
'coupons', // Remove Coupons report
'taxes', // Remove Taxes report
'downloads', // Remove Downloads report
'stock', // Remove Stock report
'customers', // Remove Customers report
);
// Loop through the reports and unset the ones we want to remove
foreach ($reports_to_remove as $report_slug) {
if (isset($reports[$report_slug])) {
unset($reports[$report_slug]);
}
}
return $reports;
}
// Hook into the WooCommerce Admin menu
add_action('admin_menu', 'wp_dudecom_remove_analytics_menu_items', 99);
function wp_dudecom_remove_analytics_menu_items() {
// Remove the Analytics menu item completely
remove_menu_page('wc-admin&path=/analytics');
}
// Restrict access to WooCommerce Analytics for non-admin users
add_action('admin_init', 'wp_dudecom_restrict_analytics_access');
function wp_dudecom_restrict_analytics_access() {
// Check if the current user is not an administrator
if (!current_user_can('administrator')) {
// Redirect non-admin users away from the Analytics page
if (is_admin() && isset($_GET['page']) && $_GET['page'] === 'wc-admin&path=/analytics') {
wp_redirect(admin_url());
exit;
}
}
}
?>
Instructions
File Location: Add the following code to your theme's functions.php
file or a custom plugin file.
Prerequisites:
- Ensure WooCommerce is installed and activated on your WordPress site.
- You must have administrator access to implement these changes.
Implementation Steps:
- Log in to your WordPress admin dashboard.
- Navigate to Appearance > Theme Editor if you are adding the code to
functions.php
, or go to Plugins > Editor if you are adding it to a custom plugin. - Locate and open the
functions.php
file of your active theme or the custom plugin file where you want to add the code. - Copy the provided code snippet and paste it at the end of the file.
- Save the changes to the file.
- Clear your browser cache and refresh your WordPress admin dashboard to see the changes take effect.
By following these steps, you will successfully hide selected reports in the WooCommerce Analytics section and restrict access to non-admin users.
If you need assistance with this implementation or require more advanced functionality, consider reaching out to wp-dude.com for expert WordPress support.