How to get the current widget id in WordPress admin

A very short post today, I just thought this would come in handy to someone. If you are in the form() function you should be able to do this:

$id = explode("-", $this->get_field_id("widget_id"));
echo $id[1] . "-" . $id[2];

However, if for some reason this won’t work for you and you have already managed to get an object ($this) within the widget form within jQuery then this should also work for you:

var id_attr = $(this).closest('form').find('input[name="widget-id"]').val();

About Stephen Pickett

Stephen Pickett is a programmer, IT strategist and architect, project manager and business analyst, Oracle Service Cloud and telephony expert, information security specialist, all-round geek. He is currently Technical Director at Connect Assist, a social business that helps charities and public services improve quality, efficiency and customer engagement through the provision of helpline services and CRM systems.

Stephen is based in south Wales and attended Cardiff University to study Computer Science, in which he achieved a 2:1 grading. He has previously worked for Think Consulting Solutions, a leading voice on not-for-profit fundraising, Fujitsu Services and Sony Manufacturing UK as a software developer.

Stephen is the developer of ThinkTwit, a WordPress plugin that allows you to display multiple Twitter feeds within a blog.

3 thoughts on “How to get the current widget id in WordPress admin

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.