1

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.

2
  • This is a strange request, can you provide more context? Why are you creating the pages?
    – Tom J Nowell
    May 1, 2019 at 20:51
  • Well, it is a business requirement, to control the pages from the network. This way, they admin controls the pages, but not the content. This way the company keeps track of the pages, and automatically re-create them via the API if a page is mistakenly deleted.
    – Greeso
    May 1, 2019 at 21:05

1 Answer 1

1

You can't find the option to specify the domain because there isn't one, that's not how multisite work. Functions always operate on the current site. If you're in WP Admin the site is the site that WP Admin is for.

Additionally, the plugin will run on all sites, so if your code worked the way you think it does, and you have 5 sites, you'd get 5 duplicate pages.

Finally, your code as is will create a page on every single request. AJAX requests, cron jobs, REST API endpoints, etc. Simply being in a post edit screen will create a page from the WP Heartbeat mechanism.

So there are a few things to note:

  • Don't create pages on init or admin_init
  • Create the pages on site creation or plugin activation
  • Only create the page when you're on the site you want to create the page on

You can change the current blog using switch_to_blog and passing a site ID, and then restore_current_blog to undo it, but keep in mind that this doesn't avoid the duplication bug I pointed out above.

So the gist is:

if ( on the site I want it on ) {
    create the page
}

So grab the current homepage and check if using standard string comparisons, e.g:

if ( site_url() == 'https://example.com' )

Otherwise if you already know the page content, and you already know which domain it's going on, why write it in code when you can write it in WP Admin? There doesn't seem to be a point to doing it the way you're doing it ( unless there's more to your question you've not shared with us )

2
  • Thanks for your input. Few things to note. First, I stated in the question that the init is only for testing purposes. My intention is not to use it in a live environment, and yes, I do create those pages in a different mechanism/call than what is presented here. Also, if you refresh each site, a page is created in that site, however, if you refresh the network section of the admin area, only the primary site will have those pages. That being said, this does not solve my problem. But with that, I was not aware of switch_to_blog. I think this will help. I will keep you posted. Thanks.
    – Greeso
    May 1, 2019 at 21:13
  • 1
    I love you man. And it is a brotherly love. You solved my conundrum!. switch_to_blog() did it.
    – Greeso
    May 1, 2019 at 22:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.