簡體   English   中英

WordPress自定義登錄模板

[英]Wordpress custom login template

我在wordpress中創建了一個名為custom-login.php的自定義頁面,然后將該模板設置為從wordpress管理面板登錄到登錄頁面,當我訪問登錄頁面時,我得到了登錄表單,我輸入了登錄詳細信息,然后將其重定向我聊天頁面。 但問題是我無需登錄即可訪問聊天頁面。

custom-login.php代碼

<?php
$args = array(
'echo'           => true,
'redirect' => site_url( '/chat/ ' ),
'form_id'        => 'loginform',
'label_username' => __( 'Username' ),
'label_password' => __( 'Password' ),
'label_remember' => __( 'Remember Me' ),
'label_log_in'   => __( 'Log In' ),
'id_username'    => 'user_login',
'id_password'    => 'user_pass',
'id_remember'    => 'rememberme',
'id_submit'      => 'wp-submit',
'remember'       => true,
'value_username' => '',
'value_remember' => false
); 

wp_login_form($args); 

?>

有誰知道,如何防止聊天頁面未經登錄而無法訪問?

在您的聊天頁面模板的頂部,插入以下內容:

<?php if ( !is_user_logged_in() ) { die(); } ?> 

如果當前用戶未登錄,則此函數返回false: is_user_logged_in

或者,如果您不想修改聊天模板,則可以在頁面加載時觸發登錄檢查。 將其添加到主題的functions.php中:

// 'template_redirect' is fired before the template is rendered
add_action('template_redirect', 'check_access');

function check_access(){
   // only block the access for the chat page with the slug 'chat'
   if(is_page('chat')){
       // if the user is not logged in: abort
       if ( !is_user_logged_in() ) { die(); }
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM