简体   繁体   English

WP 子域多站点:如何更改新站点的默认永久链接结构?

[英]WP sub-domains multisite: How to change default permalink structure for new sites?

I have a WP multisite set up with sub-domains for all sites.我有一个为所有站点设置子域的 WP 多站点。

I would like the URL structure for each new site to be:我希望每个新站点的URL 结构为:

subdomain.maindomain.com/ post-name subdomain.maindomain.com/ 后缀

For instance: subdomain.maindomain.com/hi-world例如:subdomain.maindomain.com/hi-world

I've been struggling with this for a day and a half, and I hope somebody here can help me.我已经为此苦苦挣扎了一天半,希望这里有人可以帮助我。 I have read a LOT of info, and tried with editing the default theme's functions.php and also adding a custom plugin in mu-plugins .我已经阅读了很多信息,并尝试编辑默认主题的functions.php并在mu-plugins添加自定义插件。 But I have not succeeded yet.但我还没有成功。

So the regular setting for permalinks is now: /%year%/%monthnum%/%day%/%postname%/所以现在固定链接的常规设置是:/%year%/%monthnum%/%day%/%postname%/

But I'd like to have: /%postname%/但我想要:/%postname%/

Ok, I have tried many different things.好的,我尝试了很多不同的东西。 This is probably the closest to success (?): 可能是最接近成功的(?):

// set permalink
function set_permalink(){
    global $wp_rewrite;
    $wp_rewrite->set_permalink_structure('/%year%/%monthnum%/%postname%/');
}
add_action('init', 'set_permalink');

In the functions.php I have this for adding standard pages to the new blogs/ sites automatically:在 functions.php 中,我有这个用于自动将标准页面添加到新博客/站点:

add_action('wpmu_new_blog', 'wpb_create_my_pages', 10, 2);

// create new page
  $page_id = wp_insert_post(array(
    'post_title'     => 'My title',
    'post_name'      => 'The name',
    'post_content'   => 'The content here',
    'post_status'    => 'publish',
    'post_author'    => $user_id, // or "1" (super-admin?)
    'post_type'      => 'page',
    'menu_order'     => 10,
    'comment_status' => 'closed',
    'ping_status'    => 'closed',
 ));  

So in this I have tried the 'set_permalink' function, but it doesn't work.所以在这个我尝试了'set_permalink' function,但它不起作用。

I have also tried making my own mu-plugins plugin, but haven't got that working neither.我也尝试过制作自己的 mu-plugins 插件,但也没有成功。

When I Google I keep finding solutions that require the new blog's owner to log in and save the permalink structure, but I simply want the permalink structure to be the same for all new blogs/ sites.当我使用 Google 搜索时,我一直在寻找需要新博客所有者登录并保存永久链接结构的解决方案,但我只是希望所有新博客/网站的永久链接结构都相同。

How can I set the default permalink structure for new sites?如何为新网站设置默认的永久链接结构?

Thanks for any pointers or code that can help me with this!感谢任何可以帮助我的指针或代码!

Just for future proofing this question with an answer, and for reference for others:只是为了将来用答案来证明这个问题,并供其他人参考:

  1. I created a new sub directory in /wp-content/ named "mu-plugins", so I had /wp-content/mu-plugins/我在 /wp-content/ 中创建了一个名为“mu-plugins”的新子目录,所以我有 /wp-content/mu-plugins/

  2. In "mu-plugins" I created a file named "rewritepermalinks.php"在“mu-plugins”中,我创建了一个名为“rewritepermalinks.php”的文件

In it I put:在里面我放了:

<?php

/**
 * Plugin Name: Permalin Structure Edit
 * Description: Edit the permalinks structure on new multisite sites
 * Author:      
 * License:     GNU General Public License v3 or later
 * License URI: http://www.gnu.org/licenses/gpl-3.0.html
 */

add_action( 'wpmu_new_blog', function( $blog_id ){

switch_to_blog( $blog_id );
global $wp_rewrite;
$wp_rewrite->set_permalink_structure('/%postname%/');
$wp_rewrite->flush_rules();
restore_current_blog();

}, 10 );
?>

This then worked for me on Wordpress 5.8.3 and with multisite enabled.然后,这在 Wordpress 5.8.3 上对我有用,并启用了多站点。 The solution was found here and also mentioned in the comments for the question.这里找到了解决方案,并且在问题的评论中也提到了。 I had tried it once before but must have had a typo because it didn't work the first time.我之前尝试过一次,但肯定有错字,因为它第一次不起作用。

I have tried to use 'wp_insert_site' in stead, but haven't got that to work.我曾尝试使用 'wp_insert_site' 来代替,但还没有成功。 There is a hint in the commentshere about wp_insert_site that might be of value to get this working. 此处的评论中有关于 wp_insert_site的提示,这可能对使其正常工作有价值。

For now the above code works for me as a MU plugin.现在,上面的代码对我来说是一个 MU 插件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM