WordPress用钩子将html添加到登录屏幕



我在这里遇到了一个问题。我需要将添加到html登录/忘记密码页面。我不想更改默认登录页面。另外,在WP中创建页面本身对我来说是没有选择的,因为它是一个有很多网站的WPMU。

所以我的问题是:有没有一种方法可以改变这些屏幕html(不使用前端语言(。像钩子什么的?

首先,我建议不要编辑核心文件,因为下次更新WordPress时会覆盖它。

要在登录页面上创建一个额外的字段,您可以使用login_form操作挂钩:

add_action('login_form','my_added_login_field');
function my_added_login_field(){
//Output your HTML
?>
<p>
<label for="my_extra_field">My extra field<br>
<input type="text" tabindex="20" size="20" value="" class="input" id="my_extra_field" name="my_extra_field_name"></label>
</p>
<?php
}

接下来,我们需要验证他们输入字段的内容是否与您存储的内容相匹配。在下面的代码中,我假设您已经将标识代码存储为具有元密钥my_ident_code的用户元值您应该这样做,而不是创建自己的列

若要验证用户,可以使用身份验证筛选器。这会传递输入的用户名和密码。如果识别码正确,则返回null以允许WordPress验证密码和用户名。如果不正确,请删除WordPress的身份验证并返回错误。这将迫使用户返回到登录页面,在那里他们将看到显示的错误。

add_filter( 'authenticate', 'my_custom_authenticate', 10, 3 );
function my_custom_authenticate( $user, $username, $password ){
//Get POSTED value
$my_value = $_POST['my_extra_field_name'];
//Get user object
$user = get_user_by('login', $username );
//Get stored value
$stored_value = get_user_meta($user->ID, 'my_ident_code', true);
if(!$user || empty($my_value) || $my_value !=$stored_value){
//User note found, or no value entered or doesn't match stored value - don't proceed.
remove_action('authenticate', 'wp_authenticate_username_password', 20);
remove_action('authenticate', 'wp_authenticate_email_password', 20); 
//Create an error to return to user
return new WP_Error( 'denied', __("<strong>ERROR</strong>: You're unique identifier was invalid.") );
}
//Make sure you return null 
return null;
}

对于自定义登录页面

https://codex.wordpress.org/Customizing_the_Login_Form#Login_Hooks

所以我的问题是:有没有一种方法可以更改这些屏幕html(不使用前端语言(。

没有JS和CSS,很难

Maartjie以下是wordpress操作和过滤器的完整列表,其中包含管理操作的链接:https://codex.wordpress.org/Plugin_API/Action_Reference#Administrative_Actions.login_form是您应该查看的。不需要js或css,但需要php。

最新更新