Auto-open widget area.
-
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.
- The topic ‘Auto-open widget area.’ is closed to new replies.