简体   繁体   English

Wordpress-以简码显示登录表单

[英]Wordpress - Displaying login form in shortcode

I tried the wordpress stack but no luck so thought I'd try here... 我尝试了wordpress堆栈,但是没有运气,以为我会在这里尝试...

So I am trying to display a working version of the default wordpress login form on one of my pages. 因此,我试图在我的页面之一上显示默认wordpress登录表单的工作版本。 There is code in the functions.php file that creates the shortcode and as far as I know it works successfully, however after submitting the form, it simply refreshes the page and doesn't redirect to the url I am specifying...can anyone see why it might not work as it should? 据我所知,functions.php文件中包含创建短代码的代码,该代码可以成功运行,但是提交表单后,它只是刷新页面,而不会重定向到我指定的网址...任何人都可以看看为什么它可能无法正常工作?

This is the function I put in the functions.php file: 这是我放入functions.php文件中的函数:

//Login form Shortcode
add_shortcode( 'login-form', 'my_login_form_shortcode' );
/**
 * Displays a login form.
 *
 * @since 0.1.0
 * @uses wp_login_form() Displays the login form.
 */
function my_login_form_shortcode( $atts, $content = null ) {

    $defaults = array(      "redirect"              =>  site_url( $_SERVER['REQUEST_URI'] )
                        );

        extract(shortcode_atts($defaults, $atts));
        if (!is_user_logged_in()) {
        $content = wp_login_form( array( 'echo' => false, 'redirect' => $redirect ) );
        }
    return $content;
}

I am then placing it in the desired page like so: 然后,将其放置在所需的页面中,如下所示:

[login-form redirect="https://myurl.com"]

but instead of redirecting me to the right page, it is instead refreshing the page even though I am now successfully logged in...any ideas? 但是,即使我现在已成功登录,也没有刷新我到正确的页面,而是刷新了页面...任何想法?

You can simply add this code below 您只需在下面添加此代码

add_action( 'login_form_middle', 'add_lost_password_link' );
function add_lost_password_link() {
    return '<a href="/wp-login.php?action=lostpassword">Forgot Your Password?</a>';
}

if(!function_exists('vivid_login_page'))
{
  function vivid_login_page()
  {
      $args = array(
        'echo'           => true,
        'remember'       => true,
        'redirect'       => ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'],
        'form_id'        => 'loginform',
        'id_username'    => 'user_login',
        'id_password'    => 'user_pass',
        'id_remember'    => 'rememberme',
        'id_submit'      => 'wp-submit',
        'label_username' => __( 'Username or Email Address' ),
        'label_password' => __( 'Password' ),
        'label_remember' => __( 'Remember Me' ),
        'label_log_in'   => __( 'Log In' ),
        'value_username' => '',
        'value_remember' => false
    );
    wp_login_form($args);
    add_lost_password_link();
  }
  add_shortcode('vivid-login-page', 'vivid_login_page');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM