This project demonstrates how to connect to WordPress.com using OAuth2 in pure PHP, without any external dependencies. It uses PHP's built-in cURL functions for HTTP requests and handles the OAuth2 flow manually. The demo allows users to authenticate with their WordPress.com account and view their profile information.
- No Composer or external libraries required
- Manual OAuth2 flow using PHP sessions and cURL
- Simple, self-contained codebase
- Basic error handling
config.php— Configuration constants and session startindex.php— Home page with "Connect with WordPress.com" buttoncallback.php— Handles OAuth2 callback and token exchangeprofile.php— Displays authenticated user's WordPress.com profilelogout.php— Logs out the user and clears the session
-
Create a WordPress.com Application
- Go to WordPress.com Developer Apps
- Create a new app and set the Redirect URI to:
http://localhost:8000/callback.php - Note your Client ID and Client Secret
-
Configure the Application
- Open
config.php - Replace the placeholders with your actual credentials:
define('WPCOM_CLIENT_ID', 'your_client_id'); define('WPCOM_CLIENT_SECRET', 'your_client_secret'); define('WPCOM_REDIRECT_URI', 'http://localhost:8000/callback.php');
- Open
-
Start the PHP Development Server
php -S localhost:8000
-
Open the Demo in Your Browser
- Visit http://localhost:8000
- Click "Connect with WordPress.com" and follow the authentication flow
- index.php — Presents a login button that redirects to WordPress.com for OAuth2 authentication.
- callback.php — Handles the redirect from WordPress.com, exchanges the authorization code for an access token, and stores it in the session.
- profile.php — Uses the access token to fetch and display the user's profile information from the WordPress.com REST API.
- logout.php — Clears the session and logs the user out.
- SSL Verification: For demonstration, SSL verification is disabled in cURL requests. In production, always enable SSL verification by setting
CURLOPT_SSL_VERIFYPEERtotrue. - Credential Storage: Do not store sensitive credentials directly in code for production. Use environment variables or a secure secrets manager.
- Error Handling: This demo uses basic error handling. For production, implement more robust error and exception handling.
- CSRF Protection: The OAuth2 flow should include CSRF protection (e.g., using a
stateparameter) in production.
This project is provided for educational purposes and does not include a license. Please adapt and secure it for your own use.