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



我有一个WP多站点设置,所有站点都有子域。

我希望每个新网站的URL结构为:

subdomain.maindomain.com/帖子名称

例如:subdomain.maindowmain.com/hi-world

我已经为此挣扎了一天半,我希望这里有人能帮助我。我读了很多信息,并尝试编辑默认主题的functions.php,还在mu-plugins中添加了一个自定义插件。但是我还没有成功。

因此,永久链接的常规设置现在是:/%year%/%monthnum%/%day%/%postname%/

但是我想要:/%postname%/

好的,我试过很多不同的东西。这可能是最接近成功的(?(:

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

在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',
));  

因此,在本文中,我尝试了"set_permink"函数,但它不起作用。

我也尝试过制作我自己的mu-plugins插件,但都没有成功。

当我在谷歌上搜索时,我一直在寻找需要新博客所有者登录并保存永久链接结构的解决方案,但我只是希望所有新博客/网站的永久链接结构都相同。

如何为新网站设置默认的永久链接结构

感谢任何可以帮助我的指针或代码!

只是为了将来证明这个问题的答案,并供其他人参考:

  1. 我在/wp-content/中创建了一个新的子目录,名为";μ插件";,所以我有/wp内容/mu插件/

  2. 在";μ插件";我创建了一个名为";repwritepermalinks.php";

我在里面放了:

<?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 );
?>

这对我来说适用于Wordpress 5.8.3,并且启用了多站点。在这里找到了解决方案,在对该问题的评论中也提到了解决方案。我以前试过一次,但肯定是打字错误,因为第一次不起作用。

我试过用"wp_insert_site"来代替它,但没有成功。这里的评论中有一个关于wp_insert_site的提示,这可能对实现这一点很有价值。

目前,上面的代码对我来说是一个MU插件。

最新更新