正在使用下面的代碼段為 WooCommerce 創建單獨的登錄和注冊頁面,但是此代碼段覆寫了我的默認 wp-dmin 頁面,因此當我想通過example.com/wp-admin登錄時,它會重定向到我創建的登錄頁面示例.com/login然后在登錄后重定向到 example.com/my-account。
我需要幫助修改登錄簡碼,以便在登錄后將具有管理員角色的用戶重定向到管理儀表板,并將具有客戶角色的用戶重定向到我的帳戶儀表板。
注冊頁面短代碼
/**
* @snippet WooCommerce User Login Shortcode
*/
add_shortcode( 'wc_reg_form_bbloomer', 'bbloomer_separate_registration_form' );
function bbloomer_separate_registration_form() {
if ( is_admin() ) return;
if ( is_user_logged_in() ) return;
ob_start();
// NOTE: THE FOLLOWING <FORM></FORM> IS COPIED FROM woocommerce\templates\myaccount\form-login.php
// IF WOOCOMMERCE RELEASES AN UPDATE TO THAT TEMPLATE, YOU MUST CHANGE THIS ACCORDINGLY
do_action( 'woocommerce_before_customer_login_form' );
?>
<form method="post" class="woocommerce-form woocommerce-form-register register" <?php do_action( 'woocommerce_register_form_tag' ); ?> >
<?php do_action( 'woocommerce_register_form_start' ); ?>
<?php if ( 'no' === get_option( 'woocommerce_registration_generate_username' ) ) : ?>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="reg_username"><?php esc_html_e( 'Username', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="username" id="reg_username" autocomplete="username" value="<?php echo ( ! empty( $_POST['username'] ) ) ? esc_attr( wp_unslash( $_POST['username'] ) ) : ''; ?>" /><?php // @codingStandardsIgnoreLine ?>
</p>
<?php endif; ?>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="reg_email"><?php esc_html_e( 'Email address', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="email" class="woocommerce-Input woocommerce-Input--text input-text" name="email" id="reg_email" autocomplete="email" value="<?php echo ( ! empty( $_POST['email'] ) ) ? esc_attr( wp_unslash( $_POST['email'] ) ) : ''; ?>" /><?php // @codingStandardsIgnoreLine ?>
</p>
<?php if ( 'no' === get_option( 'woocommerce_registration_generate_password' ) ) : ?>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="reg_password"><?php esc_html_e( 'Password', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="password" class="woocommerce-Input woocommerce-Input--text input-text" name="password" id="reg_password" autocomplete="new-password" />
</p>
<?php else : ?>
<p><?php esc_html_e( 'A password will be sent to your email address.', 'woocommerce' ); ?></p>
<?php endif; ?>
<?php do_action( 'woocommerce_register_form' ); ?>
<p class="woocommerce-FormRow form-row">
<?php wp_nonce_field( 'woocommerce-register', 'woocommerce-register-nonce' ); ?>
<button type="submit" class="woocommerce-Button woocommerce-button button woocommerce-form-register__submit" name="register" value="<?php esc_attr_e( 'Register', 'woocommerce' ); ?>"><?php esc_html_e( 'Register', 'woocommerce' ); ?></button>
</p>
<?php do_action( 'woocommerce_register_form_end' ); ?>
</form>
<?php
return ob_get_clean();
}
登錄頁面短代碼
/**
* @snippet WooCommerce User Login Shortcode
*/
add_shortcode( 'wc_login_form', 'bbloomer_separate' );
function bbloomer_separate() {
if ( is_admin() ) return;
ob_start();
woocommerce_login_form( array( 'redirect' => 'https://example.com/my-account/' ) );
return ob_get_clean();
}
uj5u.com熱心網友回復:
當滿足給定條件時,您可以使用“條件邏輯”來執行某個功能。
首先,我建議您在此處洗掉重定向位:
add_shortcode( 'wc_login_form', 'bbloomer_separate' );
function bbloomer_separate() {
if ( is_admin() ) return;
ob_start();
woocommerce_login_form();
return ob_get_clean();
}
然后,看看其他教程WooCommerce: Login Redirect by User Role @ My Account并將其添加到您的functions.php:
add_filter( 'woocommerce_login_redirect', 'bbloomer_customer_login_redirect', 9999, 2 );
function bbloomer_customer_login_redirect( $redirect, $user ) {
if ( wc_user_has_role( $user, 'administrator' ) ) {
$redirect = '/wp-admin';
}
return $redirect;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/355639.html
標籤:php WordPress的 求购
