給Wordpress指定登錄后轉向頁面代碼
來源:程序員人生 發布時間:2013-12-27 23:30:09 閱讀次數:3530次
給Wordpress指定登錄后轉向頁面代碼,本文來源:wordpress啦
這是一段很有趣的代碼,在用戶登錄的時候,可以選擇登錄成功之后轉向到哪個頁面。同樣是把這段代碼放在functions.php中。
<?php
// Fields for redirect
function custom_login_fields() {
?>
<p>
<label>
<strong>Choose your location: </strong>
<select name="login_location">
<option value="">Select …</option>
<option value="<?php bloginfo('url'); ?>#banking">Banking</option>
<option value="<?php bloginfo('url'); ?>#insurance">Insurance</option>
<option value="<?php echo get_permalink(2); ?>">Securities</option>
</select>
</label>
</p><br/>
<?php
}
// Redirect function
function location_redirect() {
$location = $_POST['login_location'];
wp_safe_redirect($location);
exit();
}
// Add fields to the login form
add_action('login_form','custom_login_fields');
// Make sure the redirect happens only if your fields are submitted
if ( (isset($_GET['action']) && $_GET['action'] != 'logout') || (isset($_POST['login_location']) && !empty($_POST['login_location'])) )
add_filter('login_redirect', 'location_redirect', 10, 3);
?>