• Resolved swedish boy

    (@swedish-boy)


    I have added “edit this sidebar” links after sidebars to aid my clients to locate the Widgets section in wp-admin.
    But it’s irritated me that all areas except the first always default to closed.
    In case anyone needs to auto-open a widget-area this little hack will help:

    The idea is to queue a small javascript and pass a simple anchor (#) to the URL after wp-admin/widgets.php.

    First queue a script to be loaded on all admin pages. (convenient if you need to add other hacks). You can also queue it only on widgets.php using $hook (see: https://codex.www.ads-software.com/Plugin_API/Action_Reference/admin_enqueue_scripts#Example:_Target_a_Specific_Admin_Page)

    Into functions.php

    function load_admin_js() {
            wp_register_script( 'admin-js', get_template_directory_uri() . '/myadmin.js', 'jquery' );
            wp_enqueue_script( 'admin-js' );
    }
    add_action( 'admin_enqueue_scripts', 'load_admin_js' );

    Then in my our queued myadmin.js:

    if(location.href.search(/widgets.php/) != -1) {
    
    	jQuery(document).ready(function() {
    		possible_id = location.hash;
    		// object exists?
    		if(jQuery(possible_id))
    			jQuery(possible_id).parent().removeClass('closed');
    	});
    }

    And voila. If I call “wp-admin/widgets.php?#sidebar-id” I open that sidebar’s widget-area.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter swedish boy

    (@swedish-boy)

    and to close all other widget areas at the same time one can add the following

    if(jQuery(possible_id)) {
    		jQuery('#widgets-right .widgets-holder-wrap').addClass('closed');
    		jQuery(possible_id).parent().removeClass('closed');
    	}
    Thread Starter swedish boy

    (@swedish-boy)

    My bad. The last post will hide all areas when going into widgets without a location.hash.

    This will work better:

    if(location.href.search(/widgets.php/) != -1) {
    	jQuery(document).ready(function() {
    		// object exists?
    		if(location.hash && jQuery(location.hash)) {
    			jQuery('#widgets-right .widgets-holder-wrap').addClass('closed');
    			jQuery(location.hash).parent().removeClass('closed');
    		}
    	});
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Auto-open widget area.’ is closed to new replies.