Category: code

  • Redirect after logging in based on the user role

    Redirect after logging in based on the user role

    Not every user should be able to log in to the Dashboard, and WooCommerce already prevents this. But you may want to change it for somebody or just use a different page.

    By default WooCommerce redirects the user to the My Account page after a successful login.

    You can redirect after logging in and use a custom URL based on the user role, like the Dashboard for admins and My Account page for customers.

    Add this code at the end of the file functions.php in wp-content/themes/your-child-theme-name/:

    <?php
    /**
     * Redirect users to custom URL based on their role after login
     *
     * @param string $redirect
     * @param object $user
     * @return string
     */
    function wc_custom_user_redirect( $redirect, $user ) {
    	// Get the first of all the roles assigned to the user
    	$role = $user->roles[0];
    	$dashboard = admin_url();
    	$myaccount = get_permalink( wc_get_page_id( 'myaccount' ) );
    	if( $role == 'administrator' ) {
    		//Redirect administrators to the dashboard
    		$redirect = $dashboard;
    	} elseif ( $role == 'shop-manager' ) {
    		//Redirect shop managers to the dashboard
    		$redirect = $dashboard;
    	} elseif ( $role == 'editor' ) {
    		//Redirect editors to the dashboard
    		$redirect = $dashboard;
    	} elseif ( $role == 'author' ) {
    		//Redirect authors to the dashboard
    		$redirect = $dashboard;
    	} elseif ( $role == 'customer' || $role == 'subscriber' ) {
    		//Redirect customers and subscribers to the "My Account" page
    		$redirect = $myaccount;
    	} else {
    		//Redirect any other role to the previous visited page or, if not available, to the home
    		$redirect = wp_get_referer() ? wp_get_referer() : home_url();
    	}
    	return $redirect;
    }
    add_filter( 'woocommerce_login_redirect', 'wc_custom_user_redirect', 10, 2 );
    

    Delete the sign <?php on first line if you are having errors come up after saving the file.

    If you need to change the URL for a specific role you can use the function get_permalink() passing the page ID to return its URL:

    get_permalink( 150 );
    

    This code will return the URL to the page with ID 150.

  • Debugging WordPress

    Debugging WordPress

    If you accidentally introduce a syntax or fatal error in one of your template files, you’ll probably see the dreaded white screen of death when you visit your home page. In some cases, even the admin screens will go blank. The best way to figure out what happened is to check your PHP error log. Look for the last error shown and try to correct it. However, if you can’t find your error log (or you don’t have one), you can turn on debugging by adding define('WP_DEBUG', true); to your wp-config.php file.

    The problem with this method, though, is that everyone who visits the site will see your errors. Fortunately there is an alternative, which lets you enter debug mode by adding a query string to any URL.


    if ( isset($_GET['debug']) && $_GET['debug'] == 'debug')
    define('WP_DEBUG', true);

    Then you can just add ?debug=debug to your URL (e.g. http://mysite.com/category/news/?debug=debug) to see the errors.

    The white screen of death usually comes up when you update a theme or plugin. If your theme has gone horribly wrong and you just want to switch back to another one, but you can’t access the admin screens to change it, don’t panic. Simply delete or rename your active theme’s directory. When WordPress can’t find it, it will revert to the default theme. You can also deactivate an error-causing plugin by deleting or renaming its directory. WordPress will automatically deactivate it.

  • Call a navigation menu using a shortcode

    Today I came across a weird situation: I needed to place a navigation menu in the content of a page. A shortcode was the obvious solution, but there doesn’t appear to be one built in for menus. I created this one very quickly:

    function print_menu_shortcode($atts, $content = null) {
    extract(shortcode_atts(array( 'name' =&gt; null, ), $atts));
    return wp_nav_menu( array( 'menu' =&gt; $name, 'echo' =&gt; false ) );
    }
    add_shortcode('menu', 'print_menu_shortcode');
    

    Place this in functions.php, then use [menu name="main-menu"] to call the menu in your content (replacing "main-menu" with your menu’s slug, of course).

    You could adapt this to accept any of the other arguments available for wp_nav_menu(), but this served my purposes.

  • The semicolon;

    The semicolon;

    A nerd’s tiny tale,

    Staring at the pimple in the mirror,

    He wondered if it was a biological error

    Looking back at the things which were left out and stolen,

    He finally realized his worst nightmare was that semicolon*

    Well this line refers to the time when I spent 1/10th of the time timespan of a project debugging an exception. I later realized it was just a missing semicolon 😑🙈

    Reference : https://www.npmjs.com/package/semicolon-troll