如何在wordpress中对用户进行身份验证?
How to authenticate the user in the wordpress?
我正在创建一个插件。我需要从 wordpress 的 rest api 获取一些信息,在这个 link: http://localhost/wordpress/wp-json/wp/v2/posts
不幸的是,当我试图从那里获取一些信息时,我的 ajax 根本不接受,因为只有经过身份验证的用户才能从那里获取信息。我尝试使用此代码:
<?php
//I am trying to authenticate the user here
add_filter( 'rest_authentication_errors', 'only_authorised_rest_access');
function only_authorised_rest_access( $result )
{
if( ! is_user_logged_in() ) {
return new WP_Error( 'rest_unauthorised', __( 'Only authenticated users can access the REST API.', 'rest_unauthorised' ), array( 'status' => rest_authorization_required_code() ) );
}
return $result;
}
?>
我正在使用这个功能
<?php
function wp_favoritar_posts_init() {
$post_id = 6;
echo "<div class='redimensionar'>";
echo "<a id='teste' href='?wpfpaction=add&postid=". $post_id ."' title='teste' rel='nofollow'>Favorito</a>";
echo "</div>";
echo "<script>calculate()</script>"; //I am calling the function for take the rest api here
}
add_shortcode('favorito', 'wp_favoritar_posts_init');
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
function calculate(){
var URL = "http://localhost/wordpress/wp-json/wp/v2/posts";
$.ajax({
type: "GET",
dataType: "JSON",
url: URL,
success: function(data){
alert('oi');
},
error: function (request, status, error) {
alert(request.responseText);
}
});
}
</script>
但是当我在浏览器中执行该页面时,出现了这个消息:
有人建议从浏览器获取 cookie 以验证用户身份?
我认为您安装了限制 API 访问的插件。
根据您发布的消息。
“Only authenticated users can access the REST API” is not an error
message that appears anywhere in WordPress core. Are you using a
plugin to restrict access to the API?
https://wordpress.org/support/topic/rest-api-not-allowing-unauthenticated-requests/
我正在创建一个插件。我需要从 wordpress 的 rest api 获取一些信息,在这个 link: http://localhost/wordpress/wp-json/wp/v2/posts 不幸的是,当我试图从那里获取一些信息时,我的 ajax 根本不接受,因为只有经过身份验证的用户才能从那里获取信息。我尝试使用此代码:
<?php
//I am trying to authenticate the user here
add_filter( 'rest_authentication_errors', 'only_authorised_rest_access');
function only_authorised_rest_access( $result )
{
if( ! is_user_logged_in() ) {
return new WP_Error( 'rest_unauthorised', __( 'Only authenticated users can access the REST API.', 'rest_unauthorised' ), array( 'status' => rest_authorization_required_code() ) );
}
return $result;
}
?>
我正在使用这个功能
<?php
function wp_favoritar_posts_init() {
$post_id = 6;
echo "<div class='redimensionar'>";
echo "<a id='teste' href='?wpfpaction=add&postid=". $post_id ."' title='teste' rel='nofollow'>Favorito</a>";
echo "</div>";
echo "<script>calculate()</script>"; //I am calling the function for take the rest api here
}
add_shortcode('favorito', 'wp_favoritar_posts_init');
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
function calculate(){
var URL = "http://localhost/wordpress/wp-json/wp/v2/posts";
$.ajax({
type: "GET",
dataType: "JSON",
url: URL,
success: function(data){
alert('oi');
},
error: function (request, status, error) {
alert(request.responseText);
}
});
}
</script>
但是当我在浏览器中执行该页面时,出现了这个消息:
有人建议从浏览器获取 cookie 以验证用户身份?
我认为您安装了限制 API 访问的插件。 根据您发布的消息。
“Only authenticated users can access the REST API” is not an error message that appears anywhere in WordPress core. Are you using a plugin to restrict access to the API?
https://wordpress.org/support/topic/rest-api-not-allowing-unauthenticated-requests/