I have a network multisite setup with subdomains. It looks like this
example.com <--- (this is the main site)
sub1.example.com <--- (subdomain 1 of the main site)
sub2.example.com <--- (subdomain 2 of the main site)
example.net <--- (note that the .net instead of .com, however this is still one of the sites in the network)
I am creating a network plugin. This plugin cannot be activated in the sites' admin area, but can only be activated in the network admin area. This is achieved via Network: true as described here
Now, in that plugin, I am creating pages via API calls, via wp_insert_post()
. However, I want those pages to be specific to a certain subdomain. For example, I like to create a page that can ONLY be seen in sub2.example.com
. How can I achieve that? I could not find anything in the documentation wp_insert_post()
that helps specify the subdomain in which the page is created at.
Here is the code:
/*
Plugin Name: Awesome Plugin
Version: 1.0
Network: true
*/
function create_pages_for_my_network () {
// Add the page using the data from the array above
wp_insert_post(
array(
'post_name' => 'page-1',
'post_title' => 'Page 1',
'post_content' => 'Blah blah',
'post_status' => 'publish',
'post_type' => 'page',
'ping_status' => 'closed',
'comment_status' => 'closed',
)
);
}
add_action('init', 'create_pages_for_my_network');
Important Note I am using the action init
just for testing purposes. In this case, I am refreshing the network admin section to achieve the call to the create_pages_for_my_network()
function
When the wp_insert_post()
call is made, the pages are only created in the main site (in this case, example.com
). How can I specify which site to add the page to? For example, how can I add a page to example.net
from the network admin section?
On a side note, here is a related question. How can I add tables to the database that are specific to that subdomain from the network level? In other words, those tables will have the prefix of that specific subdomain.
Thanks.