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?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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.
Update
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