dtkwt62022 2013-10-31 14:46
浏览 19
已采纳

预先填充wp-login.php用户名

I have a WP site whereby i send customers an email with a link to /my-quotes/ this then, has the following code:

<?
if (!is_user_logged_in()): ?>
<meta http-equiv="refresh" content="0;URL=<? echo wp_login_url(get_permalink()); ?>">
<? endif; ?>

What i want to do is, give the link in the email like: /my-quotes/?login=myusername

and that 'myusername' is the login fields value to save the user re-typing it.

I've had a look in wp-login.php and cant work out where to populate the login fields using $_GET, can anyone advise on a solution?

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • douyan4958 2013-10-31 14:51
    关注

    You should do it by hooking in to the login_form action, rather than modifying wp-login.php, which will get overwritten at next update.

    You can add this code to your theme's functions.php file:

    // As part of WP login form construction, call our function
    add_filter ( 'login_form', 'login_form_prepop' );
    
    function login_form_prepop(){
        // Output jQuery to pre-fill the box
    
        if ( isset( $_REQUEST['myusername'] ) ) { // Make sure a username was passed
    ?>
    <!-- Small jQuery script to set the value of the input field to your variable -->
    
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready( function(){
        $("#user_login").val( '<?php echo( $_REQUEST['myusername'] ); ?>' );
    });
    </script>
    <?php
        }
    }
    

    Then update your code to include the variable:

    <?
    if (!is_user_logged_in()): ?>
    <meta http-equiv="refresh" content="0;URL=<? echo wp_login_url(get_permalink()); ?>?myusername=username">
    <? endif; ?>
    

    If the jQuery footprint is too much, you can achieve the same with JavaScript:

    <script type="text/javascript">
    var el = document.getElementById("user_login");
    el.value = "<?php echo( $_REQUEST['myusername'] ); ?>";
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?