Site icon Hip-Hop Website Design and Development

The best way to authorize viewing and clicking a operate solely logged customers?

I’m making a plugin that enable the customers logged to favourite the posts. At first, I create a shortcode for put within the posts.

operate add_favorite_shortcode() {
        world $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&amp;postid='. $post_id .'" title="teste" rel="nofollow">♥ Favorito</a></div>';
        return $output;
     }
     add_shortcode( 'favorito', 'add_favorite_shortcode' );

Now, I would love the allow this operate just for customers logged, how one can do it?

I discovered this operate,

operate only_authorised_rest_access( $outcome )
    {
        if( ! is_user_logged_in() ) {
            return new WP_Error( 'rest_unauthorised', __( 'Solely authenticated customers can entry the REST API.', 'rest_unauthorised' ), array( 'standing' => rest_authorization_required_code() ) );
        }

        return $outcome;
    }
    add_filter( 'rest_authentication_errors', 'only_authorised_rest_access');

however just isn’t working to me