2016-12-04 69 views
0

歡迎光臨! 我想在Wordpress中創建一個函數,通過按下用戶登錄的檢查按鈕。如果用戶登錄,它將移動到「a」,但是如果用戶離線,則移動到「b」。如果按鈕功能 - wordpress

我無法實現該功能,如果在按鈕中。在cart.php文件

標尺碼按鈕:

<a href="#"> class="button" CHECK OUT </a> 

,檢查在用戶是否登錄代碼:

if (is_user_logged_in() && isset ($ _ GET [ 'page_id']) && $ _ GET [ 'page_id']> 0 && get_option ('permalink_structure') == "" && $ _ GET [ 'page_id'] == woocommerce_get_page_id (' shop ')) { 
    wp_redirect (get_post_type_archive_link ('product')); 
    die(); 
} 

我嘗試了各種方案,但都失敗了。

樣品: 1.單擊按鈕 - > a。如果用戶登錄 - 重定向到「http://a」, b。如果用戶註銷 - 重定向到「hhtp:// b」。

感謝我們的時間!

回答

0

在模板開始加載之前,您需要與操作掛鉤聯繫。 所以,避免您已經應用其他條件,對於一般的代碼將

function redirect_on_login_status(){ 

    if(is_user_logged_in()){ // you might want to put other conditions here 
    wp_redirect(site_url('a')); // change the site_url() part to you desired url, with this case it will be redirected to example.com/a 
    }else{ 
    wp_redirect(site_url('b')); 
    } 
    exit(); 
} 
add_action('wp','redirect_on_login_status'); 

將這個功能主題functions.php文件。應該工作得很好:)

+0

你太棒了!唯一的問題是我想通過按下按鈕來使它工作。 如果我在籃子裏,我點擊「CHECK OUT」,然後: - 如果我登錄移動到a,但是如果你這樣做到b。 此時,此功能可在整個頁面上工作。即使我刷新頁面。我想通過點擊按鈕來使它工作。 如何在「檢查輸出」 中實現此功能? –