我合并了this nice solution 用于向永久链接页面添加其他选项,以及this snippet 用于向页面永久链接添加自定义基名称。
用法将下面的代码段另存为wpse238124-page-base.php
, 将其放在插件目录中,并在插件页面上激活Page Base。然后,访问permalinks页面,输入所需的页面基本名称,例如。example
, 并保存更改。
<?php
/*
Plugin Name: Page Base
Plugin URI:
Description: Enables adding a base to page permalinks.
Version: 0.0.1
Author:
Author URI:
License: GPL2/Creative Commons
*/
function wpse238124_page_base_load_permalinks() {
if ( isset( $_POST[\'wpse238124_page_base\'] ) ) {
update_option( \'wpse238124_page_base\', sanitize_title_with_dashes( $_POST[\'wpse238124_page_base\'] ) );
}
// Add a settings field to the permalink page
add_settings_field( \'wpse238124_page_base\', __( \'Page Base\' ), \'wpse238124_page_base_field_callback\', \'permalink\', \'optional\' );
}
add_action( \'load-options-permalink.php\', \'wpse238124_page_base_load_permalinks\' );
function wpse238124_page_base_field_callback() {
$value = get_option( \'wpse238124_page_base\' );
echo \'<input type="text" value="\' . esc_attr( $value ) . \'" name="wpse238124_page_base" id="wpse238124_page_base" class="regular-text" />\';
}
function wpse238124_page_base_rules() {
$page_base = get_option( \'wpse238124_page_base\' );
if ( ! $page_base ) {
return;
}
global $wp_rewrite;
$wp_rewrite->page_structure = $wp_rewrite->root . $page_base . \'/%pagename%/\';
}
add_action( \'init\', \'wpse238124_page_base_rules\' );