如何在创建站点的同一函数中获取站点 ID?
How do I get the site id in the same function that created the site?
我正在编写一些 php 函数来处理端点,特别是接受表单输入以在 wp 多站点中创建站点的函数。就进度而言,下面的代码将接受输入并创建一个具有正确路径和域的站点,但正在添加 none 的元信息。
public function create_endpoint($request) {
$key = $request['key'];
//Capturing user-input from form
if ($this->validate_key($key)) {
$title = $request['name'];
$slug = $request['slug'];
$admin_user = $request['admin_user'];
$site_owner = $request['site_owner'];
$site_ouc = $request['site_ouc'];
$registered = $request['registered'];
$service_tier = $request['service_tier'];
$service_offering = $request['service_offering'];
//Site creation process
$domain = 'localhost';
$path = 'wordpress/'.$request['slug'];
$user_id = get_user_by( 'login', $this->admin_user );
$network_id = get_network()->id;
if ( !empty($title) and !empty($domain) and !empty($path) ) {
wpmu_create_blog($domain, $path, $title, $user_id);
$site_id = get_blog_id_from_url( $domain, $path);
//Adding in meta-data
update_site_meta( $site_id, 'site_owner' , $site_owner);
update_site_meta( $site_id, 'site_ouc' , $site_ouc);
update_site_meta( $site_id, 'registered' , $registered);
update_site_meta( $site_id, 'service_tier' , $service_tier);
update_site_meta( $site_id, 'service_offering' , $service_offering);
return $site_id;
}
else {
return "Not enough information";
}
}
else {
return $this->invalid_key_message;
}
}
根本问题是当我 return $site_id
我总是得到 0
,所以 update_site_meta
函数没有任何作用。
if ( !empty($title) and !empty($domain) and !empty($path) ) {
wpmu_create_blog($domain, $path, $title, $user_id);
return $site_id;
欢迎任何见解或想法,我一直在靠墙工作。
看着他 docs 似乎 wpmu_create_blog
应该 return 博客 ID...所以您可以将那部分代码更新为以下内容:
// save id returned from creating the new site
$site_id = wpmu_create_blog($domain, $path, $title, $user_id);
// delete this line
// $site_id = get_blog_id_from_url( $domain, $path);
//Adding in meta-data
根据 WordPress 文档,如果创建成功,wpmu_create_blog returns 站点 ID:
https://developer.wordpress.org/reference/functions/wpmu_create_blog/#return
我还没有测试过,但你的代码片段中的这个更改应该有效:
public function create_endpoint($request)
{
$key = $request['key'];
//Capturing user-input from form
if ($this->validate_key($key)) {
$title = $request['name'];
$slug = $request['slug'];
$admin_user = $request['admin_user'];
$site_owner = $request['site_owner'];
$site_ouc = $request['site_ouc'];
$registered = $request['registered'];
$service_tier = $request['service_tier'];
$service_offering = $request['service_offering'];
//Site creation process
$domain = 'localhost';
$path = 'wordpress/' . $request['slug'];
$user_id = get_user_by('login', $this->admin_user);
$network_id = get_network()->id;
if (!empty($title) and !empty($domain) and !empty($path)) {
$site_id = wpmu_create_blog($domain, $path, $title, $user_id);
//Adding in meta-data
update_site_meta($site_id, 'site_owner', $site_owner);
update_site_meta($site_id, 'site_ouc', $site_ouc);
update_site_meta($site_id, 'registered', $registered);
update_site_meta($site_id, 'service_tier', $service_tier);
update_site_meta($site_id, 'service_offering', $service_offering);
return $site_id;
} else {
return "Not enough information";
}
} else {
return $this->invalid_key_message;
}
}
我正在编写一些 php 函数来处理端点,特别是接受表单输入以在 wp 多站点中创建站点的函数。就进度而言,下面的代码将接受输入并创建一个具有正确路径和域的站点,但正在添加 none 的元信息。
public function create_endpoint($request) {
$key = $request['key'];
//Capturing user-input from form
if ($this->validate_key($key)) {
$title = $request['name'];
$slug = $request['slug'];
$admin_user = $request['admin_user'];
$site_owner = $request['site_owner'];
$site_ouc = $request['site_ouc'];
$registered = $request['registered'];
$service_tier = $request['service_tier'];
$service_offering = $request['service_offering'];
//Site creation process
$domain = 'localhost';
$path = 'wordpress/'.$request['slug'];
$user_id = get_user_by( 'login', $this->admin_user );
$network_id = get_network()->id;
if ( !empty($title) and !empty($domain) and !empty($path) ) {
wpmu_create_blog($domain, $path, $title, $user_id);
$site_id = get_blog_id_from_url( $domain, $path);
//Adding in meta-data
update_site_meta( $site_id, 'site_owner' , $site_owner);
update_site_meta( $site_id, 'site_ouc' , $site_ouc);
update_site_meta( $site_id, 'registered' , $registered);
update_site_meta( $site_id, 'service_tier' , $service_tier);
update_site_meta( $site_id, 'service_offering' , $service_offering);
return $site_id;
}
else {
return "Not enough information";
}
}
else {
return $this->invalid_key_message;
}
}
根本问题是当我 return $site_id
我总是得到 0
,所以 update_site_meta
函数没有任何作用。
if ( !empty($title) and !empty($domain) and !empty($path) ) {
wpmu_create_blog($domain, $path, $title, $user_id);
return $site_id;
欢迎任何见解或想法,我一直在靠墙工作。
看着他 docs 似乎 wpmu_create_blog
应该 return 博客 ID...所以您可以将那部分代码更新为以下内容:
// save id returned from creating the new site
$site_id = wpmu_create_blog($domain, $path, $title, $user_id);
// delete this line
// $site_id = get_blog_id_from_url( $domain, $path);
//Adding in meta-data
根据 WordPress 文档,如果创建成功,wpmu_create_blog returns 站点 ID:
https://developer.wordpress.org/reference/functions/wpmu_create_blog/#return
我还没有测试过,但你的代码片段中的这个更改应该有效:
public function create_endpoint($request)
{
$key = $request['key'];
//Capturing user-input from form
if ($this->validate_key($key)) {
$title = $request['name'];
$slug = $request['slug'];
$admin_user = $request['admin_user'];
$site_owner = $request['site_owner'];
$site_ouc = $request['site_ouc'];
$registered = $request['registered'];
$service_tier = $request['service_tier'];
$service_offering = $request['service_offering'];
//Site creation process
$domain = 'localhost';
$path = 'wordpress/' . $request['slug'];
$user_id = get_user_by('login', $this->admin_user);
$network_id = get_network()->id;
if (!empty($title) and !empty($domain) and !empty($path)) {
$site_id = wpmu_create_blog($domain, $path, $title, $user_id);
//Adding in meta-data
update_site_meta($site_id, 'site_owner', $site_owner);
update_site_meta($site_id, 'site_ouc', $site_ouc);
update_site_meta($site_id, 'registered', $registered);
update_site_meta($site_id, 'service_tier', $service_tier);
update_site_meta($site_id, 'service_offering', $service_offering);
return $site_id;
} else {
return "Not enough information";
}
} else {
return $this->invalid_key_message;
}
}