您应该通过挂钩操作来完成此login_form
操作,而不是修改 wp-login.php,后者将在下次更新时被覆盖。
您可以将此代码添加到主题functions.php
文件中:
// 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
}
}
然后更新您的代码以包含该变量:
<?
if (!is_user_logged_in()): ?>
<meta http-equiv="refresh" content="0;URL=<? echo wp_login_url(get_permalink()); ?>?myusername=username">
<? endif; ?>
如果 jQuery 占用太多,您可以使用 JavaScript 实现相同的效果:
<script type="text/javascript">
var el = document.getElementById("user_login");
el.value = "<?php echo( $_REQUEST['myusername'] ); ?>";
</script>