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. So I wrote this short snippet.
Refer to the screenshot below.
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 :-).
The post How to get the Widget ID in wordpress appeared first on SpiceMailer WP Blog.