Skip to content
  • Quick Ref
  • Contact
  • About
wpcanyon.com

wpcanyon.com

Tag: UX

Add custom dashboard widgets (useful shortcuts)

Posted on August 19, 2025 By Admin No Comments on Add custom dashboard widgets (useful shortcuts)

Add Custom Dashboard Widgets (Useful Shortcuts)

If you manage a WordPress site, adding custom dashboard widgets can greatly improve your workflow by providing quick access to important links, information, or tools right on your admin dashboard. This tutorial shows you how to create a custom dashboard widget in WordPress, making your admin area more functional and tailored to your needs.

Quick Fix: Add a Custom Dashboard Widget in WordPress

  1. Create a function to register your custom widget using wp_add_dashboard_widget().
  2. Hook this function into the wp_dashboard_setup action.
  3. Define the callback function that outputs the widget content (e.g., useful shortcuts).
  4. Place the code in your theme’s functions.php file or a custom plugin.

Why This Happens

By default, WordPress dashboard widgets display standard information like site health, quick drafts, or WordPress news. However, these widgets might not fit your specific workflow or provide the shortcuts you frequently need. WordPress provides a built-in API to add custom dashboard widgets, allowing you to personalize your admin area and improve efficiency.

Requirements

  • Access to your WordPress site’s functions.php file or ability to create a custom plugin.
  • Basic knowledge of PHP and WordPress hooks.
  • Administrator access to the WordPress dashboard.

Step-by-step: Add a Custom Dashboard Widget with Useful Shortcuts

  1. Create the widget registration function. This function will register your custom widget with WordPress.
  2. function my_custom_dashboard_widgets() {
        wp_add_dashboard_widget(
            'custom_shortcuts_widget',         // Widget slug.
            'Useful Shortcuts',                 // Widget title.
            'custom_shortcuts_widget_content'  // Display function.
        );
    }
    add_action('wp_dashboard_setup', 'my_custom_dashboard_widgets');
  3. Define the widget content callback. This function outputs the HTML content inside your widget.
  4. function custom_shortcuts_widget_content() {
        echo '<ul>';
        echo '<li><a href="' . admin_url('post-new.php') . '">Add New Post</a></li>';
        echo '<li><a href="' . admin_url('edit.php?post_type=page') . '">Manage Pages</a></li>';
        echo '<li><a href="' . admin_url('upload.php') . '">Media Library</a></li>';
        echo '<li><a href="' . admin_url('themes.php') . '">Themes</a></li>';
        echo '<li><a href="' . admin_url('plugins.php') . '">Plugins</a></li>';
        echo '<li><a href="' . admin_url('options-general.php') . '">Settings</a></li>';
        echo '</ul>';
    }
  5. Add the code to your site. You can add this code to your active theme’s functions.php file or create a simple plugin to keep it separate from your theme.

Common Pitfalls

  • Placing code in the wrong file: Adding the code to a plugin or functions.php file of a child theme is recommended to avoid losing changes during theme updates.
  • Syntax errors: Missing semicolons, brackets, or quotes can cause fatal errors. Always test on a staging site first.
  • Widget not showing: Ensure you have administrator privileges and the code is hooked correctly to wp_dashboard_setup.
  • Conflicts with other plugins: Rarely, other plugins may deregister dashboard widgets or interfere with hooks.

Works on

This method works on any standard WordPress installation regardless of the web server or control panel:

  • Web servers: Apache, Nginx, LiteSpeed
  • Control panels: cPanel, Plesk, DirectAdmin
  • WordPress versions: 4.0 and above (recommended to use latest version)

FAQ

Q: Can I add multiple custom dashboard widgets?
A: Yes, simply call wp_add_dashboard_widget() multiple times with different widget IDs and callback functions.
Q: How do I remove default dashboard widgets?
A: Use remove_meta_box() inside the wp_dashboard_setup hook to remove unwanted default widgets.
Q: Can I add widgets for specific user roles only?
A: Yes, check the current user’s role inside your registration function and conditionally add widgets.
Q: Is it possible to add dynamic content like recent posts or stats?
A: Absolutely. Your widget callback function can run any PHP code, including queries to display dynamic data.
Q: Will this affect front-end performance?
A: No, dashboard widgets only load in the WordPress admin area and do not impact the public-facing site.
…
Admin & Blocks

WooCommerce: Force login to purchase (hide price for guests)

Posted on August 19, 2025 By Admin No Comments on WooCommerce: Force login to purchase (hide price for guests)

WooCommerce: Force Login to Purchase (Hide Price for Guests)

If you want to restrict your WooCommerce store so that only logged-in users can see product prices and make purchases, this tutorial will show you how to do it quickly and effectively. By forcing users to log in before viewing prices or adding products to the cart, you can create a members-only shopping experience or better control customer access.

Quick Fix: Require Login to Purchase in WooCommerce

  1. Add a code snippet to your theme’s functions.php file or a site-specific plugin.
  2. Hide product prices and add-to-cart buttons for guests (non-logged-in users).
  3. Redirect guests to the login page when they try to access the cart or checkout.

Why This Happens

By default, WooCommerce displays product prices and allows purchases to all visitors, regardless of login status. However, some stores want to restrict this functionality to registered users only. WooCommerce does not have a built-in setting for this, so you need to customize it by adding code that hides prices and disables purchasing for guests.

This approach is common for wholesale stores, membership sites, or private catalogs where pricing and purchasing should be exclusive.

Requirements

  • WooCommerce installed and active on your WordPress site.
  • Access to your theme’s functions.php file or the ability to add custom PHP snippets via a plugin like Code Snippets.
  • Basic understanding of WordPress and WooCommerce templates.

Step-by-Step: Force Login to Purchase in WooCommerce

  1. Backup your site before making any code changes.
  2. Open your theme’s functions.php file or a site-specific plugin editor.
  3. Add the following code snippet:
<?php
// Hide prices and add to cart button for guests
function wc_hide_price_add_to_cart_for_guests() {
    if ( ! is_user_logged_in() ) {
        remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
        add_filter( 'woocommerce_get_price_html', '__return_empty_string' );
        add_filter( 'woocommerce_cart_item_price', '__return_empty_string' );
        add_filter( 'woocommerce_cart_item_subtotal', '__return_empty_string' );
    }
}
add_action( 'wp', 'wc_hide_price_add_to_cart_for_guests' );

// Redirect guests to login page when trying to access cart or checkout
function wc_redirect_non_logged_users() {
    if ( ! is_user_logged_in() && ( is_cart() || is_checkout() ) ) {
        wp_safe_redirect( wp_login_url( wc_get_page_permalink( is_checkout() ? 'checkout' : 'cart' ) ) );
        exit;
    }
}
add_action( 'template_redirect', 'wc_redirect_non_logged_users' );
?>
  1. Save the file and test your site by logging out and visiting product pages, the cart, and checkout.

Common Pitfalls

  • Child Theme vs Parent Theme: Adding code to a parent theme’s functions.php can be overwritten on updates. Use a child theme or a site-specific plugin.
  • Cache Issues: If you use caching plugins or server caching, clear caches after adding the code to see changes immediately.
  • Login Redirect Loops: Ensure the login redirect URL is correct to avoid redirect loops.
  • Custom Themes or Plugins: Some themes or plugins might override WooCommerce hooks, so test thoroughly.

Works On

This solution works on all standard WooCommerce setups running on:

  • Apache and Nginx web servers
  • LiteSpeed servers
  • Hosting control panels like cPanel and Plesk
  • Any WordPress environment where you can add PHP code snippets

FAQ

Q: Can I customize the login redirect URL?
A: Yes, modify the wp_login_url() function parameter to any URL you want users to land on after login.
Q: Will this hide prices on all product types?
A: Yes, the snippet hides prices and add-to-cart buttons for all product types for guests.
Q: Can I show a custom message instead of hiding prices?
A: Yes, you can replace the price filters with a custom message by hooking into woocommerce_get_price_html and returning your message.
Q: Does this affect logged-in users?
No, logged-in users will see prices and can purchase products normally.
Q: Is there a plugin alternative?
Yes, some plugins offer membership or wholesale features that include login-to-purchase restrictions, but this code snippet is lightweight and free.
…
WooCommerce How‑tos

WooCommerce: Redirect after add to cart (to cart or checkout)

Posted on August 19, 2025 By Admin No Comments on WooCommerce: Redirect after add to cart (to cart or checkout)

WooCommerce: Redirect after Add to Cart (to Cart or Checkout)

By default, WooCommerce keeps customers on the product page after they add an item to their cart. Sometimes, you want to improve the shopping experience by redirecting users immediately to the cart or checkout page after adding a product. This tutorial explains how to implement a redirect after add to cart in WooCommerce quickly and reliably.

Quick Fix: Redirect After Add to Cart

  1. Choose whether to redirect to the cart page or the checkout page.
  2. Add a snippet of PHP code to your theme’s functions.php file or a site-specific plugin.
  3. Test the add-to-cart button to confirm the redirect works as expected.

Why This Happens

WooCommerce’s default behavior is designed to keep customers browsing products after adding an item to their cart. This can be good for discovery but sometimes disrupts the flow for customers ready to purchase. Redirecting after add to cart streamlines the checkout process by taking users directly to the cart or checkout page, reducing friction and potentially increasing conversions.

Requirements

  • WooCommerce plugin installed and active.
  • Access to your WordPress theme’s functions.php file or a site-specific plugin to add custom code.
  • Basic knowledge of editing PHP files in WordPress.

Step-by-step: Redirect After Add to Cart

  1. Backup your site. Always back up your site before editing theme files.
  2. Access your theme’s functions.php file. You can do this via Appearance > Theme Editor in WordPress admin or via FTP.
  3. Add the redirect code snippet. Copy and paste one of the following snippets depending on where you want to redirect users.

Code Snippet: Redirect to Cart Page

add_filter( 'woocommerce_add_to_cart_redirect', 'custom_redirect_to_cart' );
function custom_redirect_to_cart() {
    return wc_get_cart_url();
}

Code Snippet: Redirect to Checkout Page

add_filter( 'woocommerce_add_to_cart_redirect', 'custom_redirect_to_checkout' );
function custom_redirect_to_checkout() {
    return wc_get_checkout_url();
}
  1. Save the file. After adding the code, save your changes.
  2. Test the functionality. Visit a product page, add a product to the cart, and verify you are redirected to the cart or checkout page as intended.

Common Pitfalls

  • Theme or plugin conflicts: Some themes or plugins may override WooCommerce’s add to cart behavior, preventing the redirect from working.
  • AJAX add to cart enabled: If your theme uses AJAX for add to cart, the redirect filter may not trigger because the page does not reload.
  • Editing the wrong file: Ensure you add the code to your active theme’s functions.php or a site-specific plugin, not a parent theme if using a child theme.
  • Cache issues: Browser or server caching can interfere with testing changes. Clear caches after updating code.

Works on

Server/Environment Compatibility
Apache Fully compatible
Nginx Fully compatible
LiteSpeed Fully compatible
cPanel Fully compatible
Plesk Fully compatible

FAQ

Q: Why doesn’t the redirect work on my site?
A: If your theme uses AJAX add to cart, the redirect filter won’t trigger because the page doesn’t reload. You may need to disable AJAX add to cart or use JavaScript-based redirects.
Q: Can I redirect to a custom page after add to cart?
A: Yes. Replace wc_get_cart_url() or wc_get_checkout_url() with the URL of your custom page in the redirect function.
Q: Is it safe to add this code to my theme’s functions.php file?
A: Yes, but changes will be lost if you update the theme. Use a child theme or a site-specific plugin to keep changes persistent.
Q: How do I disable the redirect and go back to default behavior?
A: Remove or comment out the redirect filter code from your functions.php file or plugin.
Q: Will this affect variable or grouped products?
A: The redirect applies globally to all products added to the cart. However, AJAX add to cart on variable or grouped products may behave differently depending on your theme.
…
WooCommerce How‑tos

WooCommerce: Change “Add to cart” text per product type

Posted on August 19, 2025 By Admin No Comments on WooCommerce: Change “Add to cart” text per product type

WooCommerce: Change “Add to cart” Text Per Product Type

If you want to customize the “Add to cart” button text in WooCommerce based on the product type, this guide will show you how to do it quickly and efficiently. Changing the button text per product type can improve user experience by providing context-specific calls to action, such as “Select options” for variable products or “Read more” for external products.

Quick Fix

  1. Identify the product types you want to customize (simple, variable, grouped, external).
  2. Add a custom function to your theme’s functions.php or a site-specific plugin.
  3. Use WooCommerce filters to change the button text based on product type.
  4. Test the changes on the frontend to confirm the new button text appears correctly.

Why This Happens

WooCommerce uses default button texts for different product types to guide customers appropriately. For example, variable products show “Select options” because customers need to choose variations before adding to cart. However, these defaults might not fit your store’s branding or messaging strategy. WooCommerce provides hooks and filters that allow developers to modify these texts without changing core files, ensuring updates won’t overwrite your customizations.

Requirements

  • WooCommerce installed and active on your WordPress site.
  • Access to your theme’s functions.php file or a custom plugin for adding PHP code.
  • Basic understanding of PHP and WordPress hooks.
  • Optional: Child theme to avoid losing changes on theme updates.

Step-by-step

  1. Backup your site: Always create a backup before editing theme files.
  2. Open your theme’s functions.php file: Use FTP, cPanel file manager, or the WordPress theme editor.
  3. Add the following code snippet:
add_filter( 'woocommerce_product_add_to_cart_text', 'custom_add_to_cart_text', 10, 2 );
add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_add_to_cart_text', 10, 2 );

function custom_add_to_cart_text( $text, $product ) {
    if ( ! is_a( $product, 'WC_Product' ) ) {
        return $text;
    }

    switch ( $product->get_type() ) {
        case 'simple':
            $text = __( 'Buy Now', 'your-text-domain' );
            break;
        case 'variable':
            $text = __( 'Choose Options', 'your-text-domain' );
            break;
        case 'grouped':
            $text = __( 'View Products', 'your-text-domain' );
            break;
        case 'external':
            $text = __( 'Visit Website', 'your-text-domain' );
            break;
        default:
            $text = __( 'Add to cart', 'your-text-domain' );
            break;
    }

    return $text;
}
  1. Save the file and refresh your WooCommerce shop and product pages. You should see the new button texts based on product types.

Common Pitfalls

  • Editing the wrong file: Always use a child theme or a custom plugin to avoid losing changes after updates.
  • Missing product type check: Ensure the product object is valid before calling methods to avoid PHP errors.
  • Not using translation functions: Use __() or _e() for strings to support localization.
  • Cache issues: Clear your site and browser cache if changes don’t appear immediately.
  • Conflicts with other plugins: Some plugins may override button texts; test with plugins disabled if needed.

Works on

This method works on any WooCommerce installation regardless of your web server or control panel:

  • Web servers: Apache, Nginx, LiteSpeed
  • Control panels: cPanel, Plesk, DirectAdmin
  • Hosting environments: Shared hosting, VPS, Dedicated servers
  • Compatible with most themes and WooCommerce versions (tested on WooCommerce 7.x and later)

FAQ

Q: Can I change the button text only on the shop/archive pages?
A: Yes. Use the woocommerce_product_add_to_cart_text filter for archive/shop pages and woocommerce_product_single_add_to_cart_text for single product pages. The example code above uses both.
Q: How do I translate the new button texts?
A: Wrap your strings with translation functions like __() and load your text domain properly in your theme or plugin. Use tools like Loco Translate to manage translations.
Q: What if I want different texts for specific products, not just product types?
A: You can extend the function to check product IDs or categories and return custom texts accordingly.
Q: Will this affect the functionality of the add to cart button?
No. This only changes the button text, not the underlying functionality.
Q: Can I use this code in a plugin instead of functions.php?
Yes. Creating a site-specific plugin is a best practice to keep your customizations independent of theme updates.
…
WooCommerce How‑tos

Recent Posts

  • Top WordPress Themes for Blogs in 2025
  • WordPress Admin Panel Trick: Adding ID Field to the Posts Listing
  • Solution previous_posts_link and next_posts_link Not Working
  • Show Top Commentators in WordPress Without a Plugin
  • How to Style Admin Comments in WordPress

Recent Comments

    Archives

    • August 2025

    Categories

    • Admin & Blocks
    • Admin & UI
    • Automation
    • Automation & Plugins
    • Comments
    • Comparisons
    • Database & Revisions
    • Developer Snippets
    • Fixes & Errors
    • Media & Thumbnails
    • Queries & Pagination
    • Security
    • Speed & Security
    • Tips & Tricks
    • WooCommerce How‑tos
    • WordPress Snippets
    • WordPress Themes
    • Terms & Conditions
    • Affiliate Disclosure

    Copyright © 2025 wpcanyon.com.

    Powered by PressBook WordPress theme

    Also by the maker of MySurveyReviews.com