WP API and OAuth - Using WordPress without WordPress
This tutorial demonstrates installing and using the WP-API with OAuth, a WordPress plugin providing REST-like API endpoints. Unauthenticated users can read content, while authenticated users (via OAuth or cookies) can write content. The process isn't intuitive, so this guide simplifies the setup, assuming basic terminal and Vagrant familiarity.
Key Concepts:
- The plugin uses REST-like endpoints for content access.
- The built-in WP-API exposes WordPress internals, allowing JSON retrieval (e.g.,
/wp-json/posts
). - OAuth1 handles authentication, requiring
wp-cli
for terminal commands. Note: WordPress uses the less efficient 3-legged OAuth flow. - The tutorial shows how an external application authenticates with WordPress and submits a post via the API using Guzzle.
Installation:
Using a Homestead Improved instance:
git clone https://github.com/swader/homestead_improved hi_wp_github cd hi_wp_github sed -i '' "s@map\: \.@map\: $PWD@g" Homestead.yaml
Modify Homestead.yaml
's sites
block:
sites: - map: test.app to: /home/vagrant/Code/wptest
(Ensure test.app
is in your /etc/hosts
file.)
Install WordPress:
cd ~/Code wget https://wordpress.org/latest.tar.gz tar -xvzf latest.tar.gz mv wordpress wptest cd wptest cp wp-config-sample.php wp-config.php
Configure wp-config.php
with database credentials. Access the site via your browser.
WP-API Setup:
Install the WP-API plugin (version 1.2.* or later) through the WordPress plugin manager. After installation, /wp-json/posts
should return a JSON array of posts. However, content submission requires further steps.
OAuth Server Setup:
WordPress uses the outdated OAuth1. Install wp-cli
:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar chmod +x wp-cli.phar sudo mv wp-cli.phar /usr/local/bin/wp
Install the OAuth1 plugin:
cd ~/Code/wptest git clone https://github.com/WP-API/OAuth1 wp-content/plugins/oauth-server
Activate the plugin and generate keys/secrets:
wp oauth1 add
This will output an ID, key, and secret for OAuth authentication. WordPress utilizes the less efficient 3-legged OAuth flow.
OAuth Client Setup:
Create a new project (submitter
) in your VM, adding a new site to Homestead.yaml
:
sites: - map: test.app to: /home/vagrant/Code/wptest - map: test2.app to: /home/vagrant/Code/submitter
Re-provision the VM (vagrant provision
). Create index.php
, callback.php
, and credentials.php
in the submitter
directory. Populate credentials.php
with the keys from the previous step.
Install required Composer packages:
composer require --dev symfony/var-dumper guzzlehttp/guzzle:~5 guzzlehttp/oauth-subscriber
Modify the WordPress default-filters.php
to allow redirects:
git clone https://github.com/swader/homestead_improved hi_wp_github cd hi_wp_github sed -i '' "s@map\: \.@map\: $PWD@g" Homestead.yaml
The following sections detail the index.php
, callback.php
, and makepost.php
files for the 3-legged OAuth flow and API interaction. (Code omitted for brevity, refer to original input for complete code examples).
Conclusion:
This tutorial guides you through setting up WP-API with OAuth. While complex, this guide simplifies the process. Further improvements and refinements are possible.
The above is the detailed content of WP API and OAuth - Using WordPress without WordPress. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



WordPress is easy for beginners to get started. 1. After logging into the background, the user interface is intuitive and the simple dashboard provides all the necessary function links. 2. Basic operations include creating and editing content. The WYSIWYG editor simplifies content creation. 3. Beginners can expand website functions through plug-ins and themes, and the learning curve exists but can be mastered through practice.

Can learn WordPress within three days. 1. Master basic knowledge, such as themes, plug-ins, etc. 2. Understand the core functions, including installation and working principles. 3. Learn basic and advanced usage through examples. 4. Understand debugging techniques and performance optimization suggestions.

WordPressisgoodforvirtuallyanywebprojectduetoitsversatilityasaCMS.Itexcelsin:1)user-friendliness,allowingeasywebsitesetup;2)flexibilityandcustomizationwithnumerousthemesandplugins;3)SEOoptimization;and4)strongcommunitysupport,thoughusersmustmanageper

WordPress itself is free, but it costs extra to use: 1. WordPress.com offers a package ranging from free to paid, with prices ranging from a few dollars per month to dozens of dollars; 2. WordPress.org requires purchasing a domain name (10-20 US dollars per year) and hosting services (5-50 US dollars per month); 3. Most plug-ins and themes are free, and the paid price ranges from tens to hundreds of dollars; by choosing the right hosting service, using plug-ins and themes reasonably, and regularly maintaining and optimizing, the cost of WordPress can be effectively controlled and optimized.

Wix is suitable for users who have no programming experience, and WordPress is suitable for users who want more control and expansion capabilities. 1) Wix provides drag-and-drop editors and rich templates, making it easy to quickly build a website. 2) As an open source CMS, WordPress has a huge community and plug-in ecosystem, supporting in-depth customization and expansion.

WordPress is a Content Management System (CMS). It provides content management, user management, themes and plug-in capabilities to support the creation and management of website content. Its working principle includes database management, template systems and plug-in architecture, suitable for a variety of needs from blogs to corporate websites.

People choose to use WordPress because of its power and flexibility. 1) WordPress is an open source CMS with strong ease of use and scalability, suitable for various website needs. 2) It has rich themes and plugins, a huge ecosystem and strong community support. 3) The working principle of WordPress is based on themes, plug-ins and core functions, and uses PHP and MySQL to process data, and supports performance optimization.

The core version of WordPress is free, but other fees may be incurred during use. 1. Domain names and hosting services require payment. 2. Advanced themes and plug-ins may be charged. 3. Professional services and advanced features may be charged.
