multisite - Network Plugin Creating Pages for Different Subdomains

I have a network multisite setup with subdomains. It looks like thisexample<--- (this is the main site)sub1.

I have a network multisite setup with subdomains. It looks like this

example        <--- (this is the main site)
sub1.example   <--- (subdomain 1 of the main site)
sub2.example   <--- (subdomain 2 of the main site)
example        <--- (note that the  instead of , 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. 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). How can I specify which site to add the page to? For example, how can I add a page to example 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.

I have a network multisite setup with subdomains. It looks like this

example        <--- (this is the main site)
sub1.example   <--- (subdomain 1 of the main site)
sub2.example   <--- (subdomain 2 of the main site)
example        <--- (note that the  instead of , 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. 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). How can I specify which site to add the page to? For example, how can I add a page to example 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.

Share Improve this question asked May 1, 2019 at 20:24 GreesoGreeso 2,2347 gold badges33 silver badges57 bronze badges 2
  • This is a strange request, can you provide more context? Why are you creating the pages? – Tom J Nowell Commented 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 Commented May 1, 2019 at 21:05
Add a comment  | 

1 Answer 1

Reset to default 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' )

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 )

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745533716a4631810.html

相关推荐

  • multisite - Network Plugin Creating Pages for Different Subdomains

    I have a network multisite setup with subdomains. It looks like thisexample<--- (this is the main site)sub1.

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信