Reputation: 876
I am working on an API for wordpress and I am looking for a way to uniquely identify a wordpress installation that is requesting the API. Does anyone know of a install id or unique wordpress blog id? Is that even a thing?
Upvotes: 1
Views: 79
Reputation: 5099
I have written 2 APIs and have worked with several, I stumbled upon this question long time back...
I like to keep the stuff simple... so I use site_url()
as identity of the blog, since I'm sure no other installation would run on the same site url so whenever there is an issue to debug, I know site ID is the url.
Saved one extra field in my DB ( If I had ID different, I'd need another field for site url ).
You can also go for complex solutions like using md5 of site_url along with some random generated strings to name a few.
This will generate an ID on activation and save it in the database.
function generate_password($length = 12) {
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$password = '';
for ($i = 0; $i < $length; $i++) {
$password. = substr($chars, rand(0, strlen($chars) - 1), 1);
}
return $password;
}
function my_plugin_activated() {
$id = generate_password();
add_option('my_plugin_installation_id', $id);
}
register_activation_hook( __FILE__, 'my_plugin_activated' );
Code in my_plugin_activated()
is executed only once on activation of plugin ( you can do the same for theme 😉 ), you can, in activation hook itself, make a call to your api and get the site registered in your db.
Hope that helps.
Upvotes: 1