In this Post I will share a short snippet which will display the widget id inside the widget form.
One can also get the widget id by inspecting the html but I find that rather tedious and user unfriendly. So I wrote this short snippet. All you need to do is include it on your site and every widget form will display the widget-id.
Refer to the screenshot below.
Image may be NSFW.
Clik here to view.
Snippet
Here is the snippet. Simply paste it in your functions.php file and you are good to go. You may also use the code snippet plugin.
add_action('in_widget_form', 'spice_get_widget_id');
function spice_get_widget_id($widget_instance)
{
// Check if the widget is already saved or not.
if ($widget_instance->number=="__i__"){
echo "<p><strong>Widget ID is</strong>: Pls save the widget first!</p>" ;
} else {
echo "<p><strong>Widget ID is: </strong>" .$widget_instance->id. "</p>";
}
}
Basically we simple hook into the in_widget_form filter and then echo the widget id. To be doubly sure we first check if the widget instance actually exists.
Conclusion
As you can see its really simple to get the widget id. Just use this small snippet and save yourself some time Image may be NSFW.
Clik here to view.
The post How to get the Widget ID in wordpress appeared first on SpiceWP.