我在阅读管理页面中为我的WordPress CMS添加了一些自定义选项。我正在使用这些选项设置一些附加页面,然后将这些页面绑定到自定义操作。此方法允许我不使用一组硬编码的页面ID维护常量文件,并允许最终用户在哪些页面执行哪些功能方面具有更大的灵活性。
当这些选项更改时,我需要能够刷新永久链接,以便更新对这些页面的任何引用,并使整个站点的永久链接正常工作。
为了做到这一点,我写了以下内容:
function qd_flush_rules() {
flush_rewrite_rules();
}
add_action(\'update_option_qd_news_page\', \'qd_flush_rules\');
qd_news_page
是我创建的选项的名称,我可以确认此设置已正确创建并正常工作。我也很确定这个函数是在数据保存后启动的,因为我在
qd_flush_rules
函数正确输出设置值。
无论如何,重写规则没有刷新,我的永久链接返回404。如果我刷新WordPress permalinks管理页面中的permalinks,一切都会立即正常工作。
完整代码:
function qd_settings_api_init() {
add_settings_section(
\'qd_theme_setting_section\',
\'Theme Reading Settings\',
\'qd_theme_setting_section_callback_function\',
\'reading\'
);
add_settings_field(
\'qd_news_page\',
\'News Page\',
\'qd_news_page_dropdown_cbf\',
\'reading\',
\'qd_theme_setting_section\'
);
register_setting( \'reading\', \'qd_news_page\' );
}
add_action( \'admin_init\', \'qd_settings_api_init\' );
//FLUSH THE PERMALINKS WHEN THIS OPTION IS SAVED
function qd_flush_rules() {
flush_rewrite_rules();
echo "News Page updated - " . get_option(\'qd_news_page\');
}
add_action(\'update_option_qd_news_page\', \'qd_flush_rules\');
function qd_theme_setting_section_callback_function() {
echo "<p>Setting Instructions</p>";
}
function qd_news_page_dropdown_cbf() {
$args = array(
\'depth\' => 0,
\'child_of\' => 0,
\'selected\' => get_option( \'qd_news_page\' ),
\'echo\' => 1,
\'name\' => \'qd_news_page\',
\'id\' => \'qd_news_page\', // string
\'show_option_none\' => \'— Select —\', // string
\'show_option_no_change\' => null, // string
\'option_none_value\' => \'0\', // string
);
wp_dropdown_pages( $args );
}
update 12/8/15 11:24am est
想要澄清的是,自定义设置并不是直接创建永久链接结构,而是被自定义帖子类型用来分配其重写规则。
该代码摘录如下:
function qd_custom_post_types() {
$post_type_slug = "/" . get_page_uri( get_option(\'qd_news_page\') );
register_post_type( \'qd_news\',
array( \'labels\' => array(
...
\'rewrite\' => array( \'slug\' => $post_type_slug . \'/article\', \'with_front\' => false ),
...
)
);
}
add_action( \'init\', \'qd_custom_post_types\');`
最合适的回答,由SO网友:Megan Rose 整理而成
这对你有用吗?当我尝试它时,它似乎起作用了。
function qd_settings_api_init() {
add_settings_section(
\'qd_theme_setting_section\',
\'Theme Reading Settings\',
\'qd_theme_setting_section_callback_function\',
\'reading\'
);
add_settings_field(
\'qd_news_page\',
\'News Page\',
\'qd_news_page_dropdown_cbf\',
\'reading\',
\'qd_theme_setting_section\'
);
if (delete_transient(\'qd_flush_rules\')) flush_rewrite_rules(); // * Added
register_setting( \'reading\', \'qd_news_page\', \'qd_sanitize\' ); // * Changed
}
/* Added this function. */
function qd_sanitize($input) {
set_transient(\'qd_flush_rules\');
return $input;
}
add_action( \'admin_init\', \'qd_settings_api_init\' );
//FLUSH THE PERMALINKS WHEN THIS OPTION IS SAVED
function qd_flush_rules() {
flush_rewrite_rules();
echo "News Page updated - " . get_option(\'qd_news_page\');
}
add_action(\'update_option_qd_news_page\', \'qd_flush_rules\');
function qd_theme_setting_section_callback_function() {
echo "<p>Setting Instructions</p>";
}
function qd_news_page_dropdown_cbf() {
$args = array(
\'depth\' => 0,
\'child_of\' => 0,
\'selected\' => get_option( \'qd_news_page\' ),
\'echo\' => 1,
\'name\' => \'qd_news_page\',
\'id\' => \'qd_news_page\', // string
\'show_option_none\' => \'— Select —\', // string
\'show_option_no_change\' => null, // string
\'option_none_value\' => \'0\', // string
);
wp_dropdown_pages( $args );
}
//
function qd_custom_post_types() {
$post_type_slug = "/" . get_page_uri( get_option(\'qd_news_page\') );
register_post_type( \'qd_news\',
array(
\'labels\' => array(
\'name\' => __( \'Articles\' ),
\'singular_name\' => __( \'Article\' )
),
\'public\' => true,
\'has_archive\' => true,
\'rewrite\' => array( \'slug\' => $post_type_slug . \'/article\', \'with_front\' => false ),
)
);
}
add_action( \'init\', \'qd_custom_post_types\');