我正在创建一个插件,允许用户登录到最喜欢的帖子。首先,我创建了一个短代码,用于放置在帖子中。
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\' );
现在,我想只为登录的用户启用此功能,如何做到这一点?
我找到了这个函数,
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\');
但对我不起作用