Uniquely Identy Wordpress Installation

2019-07-25 11:21发布

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?

标签: wordpress api
1条回答
迷人小祖宗
2楼-- · 2019-07-25 11:42

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

查看更多
登录 后发表回答