如何在主题切换时重定向到“我的主题选项”页面,重定向wp\\u redirect或wp\\u safe\\u redirect的最佳方式是什么:
这里是我的代码:
<?php
function sunset_add_admin_page() {
//Generate Sunset Admin Page
add_menu_page( \'Sunset Theme Options\', \'Sunset\', \'manage_options\', \'alecaddd_sunset\', \'sunset_theme_create_page\', get_template_directory_uri() . \'/img/sunset-icon.png\', 110 );
//Generate Sunset Admin Sub Pages
add_submenu_page( \'alecaddd_sunset\', \'Sunset Theme Options\', \'General\', \'manage_options\', \'alecaddd_sunset\', \'sunset_theme_create_page\' );
add_submenu_page( \'alecaddd_sunset\', \'Sunset CSS Options\', \'Custom CSS\', \'manage_options\', \'alecaddd_sunset_css\', \'sunset_theme_settings_page\');
}
add_action( \'admin_menu\', \'sunset_add_admin_page\' );
function sunset_sidebar_options( )
{
echo "Customize your Sidebar Information";
}
function sunset_theme_create_page() {
}
function sunset_theme_settings_page( )
{
echo "<h1> Hello Submenu </h1>";
}
function my_on_switch_theme($new_theme) {
wp_safe_redirect( $location, $status );
exit;
}
add_action(\'switch_theme\', \'my_on_switch_theme\');
最合适的回答,由SO网友:Ben HartLenn 整理而成
您可以使用after_theme_switch
钩子来做这个,并且wp_safe_redirect()
只重定向到本地URL效果很好。在回调函数中,检查$_GET[\'activated\']
标志,否则可能会出现“太多重定向”类型的错误。
add_action( \'after_switch_theme\', \'my_theme_redirect\' );
function my_theme_redirect() {
if( isset( $_GET[\'activated\'] ) ) {
wp_safe_redirect( admin_url(\'admin.php?page=alecaddd_sunset\') );
exit;
}
}