下面是最好的“WordPress”方式,我已经对它进行了充分的评论,请您解释它的作用。
add_action(\'wp\',\'_my_custom_ssl_redirect\'); // the \'wp\' hook is the first place the post id is set.
function _my_custom_ssl_redirect(){
global $post,$wp; // get some global values.
$page_ids = array(2,123,321,456); // array of page ids we want to force to ssl.
if( is_page() && isset($post->ID) && in_array($post->ID,$page_ids) ){ // check we are on a page and its a page we want to redirect.
wp_safe_redirect( // make sure we only redirect to "internal" urls.
add_query_arg( // add any url query arguments back to the url.
$_SERVER[\'QUERY_STRING\'], // The current query args.
\'\',
trailingslashit( // add a trailing slash to the home url as sometimes it is not added.
home_url( $wp->request, "https" ), // get the home url HTTPS link.
301 // set the redirect to be 301 "permanent", you can use 302 "temporary" here instead.
)
)
);
exit; // exit ASAP, no point in loading anything more.
}
}
整洁的非注释版本:)(完全相同的代码)
add_action(\'wp\',\'_my_custom_ssl_redirect\');
function _my_custom_ssl_redirect(){
global $post,$wp;
$page_ids = array(2,123,321,456); // array of page ids we want to force to ssl.
if( is_page() && isset($post->ID) && in_array($post->ID,$page_ids) ){
wp_safe_redirect( add_query_arg( $_SERVER[\'QUERY_STRING\'], \'\',trailingslashit(home_url( $wp->request, "https" ), 301 )) );
exit;
}
}