admin管理员组文章数量:1278985
I am creating a plugin that allow the users logged to favorite the posts. At first, I create a shortcode for put in the posts.
function add_favorite_shortcode() {
global $post_id;
$post_id = get_post();
$post_id = !empty( $post_id ) ? $post_id->ID : false;
$output = '<div class="redimensionar"><a id="teste" href="?faction=add&postid='. $post_id .'" title="teste" rel="nofollow">♥ Favorito</a></div>';
return $output;
}
add_shortcode( 'favorito', 'add_favorite_shortcode' );
Now, I would like the enable this function only for users logged, how to do it?
I found this function,
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;
}
add_filter( 'rest_authentication_errors', 'only_authorised_rest_access');
but is not working to me
I am creating a plugin that allow the users logged to favorite the posts. At first, I create a shortcode for put in the posts.
function add_favorite_shortcode() {
global $post_id;
$post_id = get_post();
$post_id = !empty( $post_id ) ? $post_id->ID : false;
$output = '<div class="redimensionar"><a id="teste" href="?faction=add&postid='. $post_id .'" title="teste" rel="nofollow">♥ Favorito</a></div>';
return $output;
}
add_shortcode( 'favorito', 'add_favorite_shortcode' );
Now, I would like the enable this function only for users logged, how to do it?
I found this function,
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;
}
add_filter( 'rest_authentication_errors', 'only_authorised_rest_access');
but is not working to me
Share Improve this question edited Oct 24, 2021 at 0:43 Rodrigo Franco asked Oct 23, 2021 at 22:08 Rodrigo FrancoRodrigo Franco 52 bronze badges 4 |1 Answer
Reset to default 0If you want to hide the output of this shortcode from users who aren't logged in, I'd make use of the is_user_logged_in() function built right into Wordpress.
The issue you'll run into, however, as Tom pointed out in the comments, is that you will need to check if users are logged in as part of the action that runs to favorite the post. Hiding the button doesn't actually prevent users from using the action unless you protect the action itself as well.
本文标签: pluginsHow to authorize viewing and clicking a function only logged users
版权声明:本文标题:plugins - How to authorize viewing and clicking a function only logged users? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741254469a2366402.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
?faction=add
etc ) is missing from your code. Even if you hide it anybody could directly hit that URL, and they could pass any post ID they want to favourite any post they want and ignore your shortcode and shortcode function – Tom J Nowell ♦ Commented Oct 23, 2021 at 22:14rest_authentication_errors
filter you found and edited into your question is completely unrelated to your problem, however, it contains the answer, which was also posted below by Catherine. – Tom J Nowell ♦ Commented Oct 24, 2021 at 20:25