Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save FrancoStino/8f6684ebf74421025c70779f5421bf5c to your computer and use it in GitHub Desktop.
Save FrancoStino/8f6684ebf74421025c70779f5421bf5c to your computer and use it in GitHub Desktop.
This code snippet adds a simple Basic Authentication plugin for WordPress REST API. It checks if the username or password are valid, and then authenticates it using wp_authenticate(). If there is an error during authentication, it sets the current user ID to

WordPress Basic Authentication for REST API

Preview:
/*
Plugin Name: Basic Authentication for REST API
Plugin URI: https://example.com
Description: A simple Basic Authentication plugin for WordPress REST API.
Version: 1.0
Author: Il tuo nome
Author URI: https://example.com
License: GPL2
*/

add_filter('rest_authentication_errors', function ($result) {
    if (!empty($result)) {
        return $result;
    }

    if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW'])) {
        return new WP_Error(
            'rest_authentication_failed',
            __('Missing username or password.'),
            array('status' => 401)
        );
    }

    $user = wp_authenticate($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']);

    if (is_wp_error($user)) {
        return $user;
    }

    wp_set_current_user($user->ID);

    return true;
});
Associated Context
Type Code Snippet ( .php )
Associated Tags Plugin Name Plugin URI Basic Authentication WordPress REST API WP_Error rest_authentication_errors wp_authenticate is_wp_error current_user Framework: WordPress WordPress REST API Authentication Security PHP Plugin Authorization HTTP Headers WordPress Hooks
💡 Smart Description This code snippet adds a simple Basic Authentication plugin for WordPress REST API. It checks if the username or password are valid, and then authenticates it using wp_authenticate(). If there is an error during authentication, it sets the current user ID to
Implements basic authentication for WordPress REST API using PHP_AUTH_USER and PHP_AUTH_PW headers.
🔎 Suggested Searches How to add custom authentication plugin for WordPress REST API
Code snippet for setting current user in WordPress REST API using wp_authenticate()
How to check if User ID is valid and set the current user in WordPressREST API
PHP code example for adding a filter with WP_Authenticate() method
Example of creating an administrator on WordPress REST API
WordPress
REST API
Basic Authentication
PHP
wp_authenticate
rest_authentication_errors
Related Links https://example.com
https://www.geeksforgeeks.org/php-print_r-function/
https://www.geeksforgeeks.org/php-sessions/
https://www.geeksforgeeks.org/php-str_replace-function/
https://www.oauth.com/oauth2-servers/access-tokens/refreshing-access-tokens/
https://oauth1.wp-api.org/docs/basics/Signing.html
https://stripe.com/docs/api/authentication
https://developer.wordpress.org/rest-api/
Related People Davide Ladisa, Sergio Pellicani
Sensitive Information No Sensitive Information Detected
Shareable Link https://davideladisa.pieces.cloud/?p=7a4f488f97
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment