Skip to content

Instantly share code, notes, and snippets.

@knm1993
Last active December 2, 2019 10:08
Show Gist options
  • Save knm1993/4b4fc635caccba017118648404e0ee06 to your computer and use it in GitHub Desktop.
Save knm1993/4b4fc635caccba017118648404e0ee06 to your computer and use it in GitHub Desktop.
Setup a Wordpress with enabled wp-json api

Wordpress with api basic-auth

MOVED TO Repo for more easy management https://github.com/knm1993/Wordpress-with-api-basic-auth/tree/master

  • create a wordpress server
  • enable api
  • api with authentication
  • written by the goodest BEST siuga

TOC

Installation

  1. Download Wordpress
  2. Unzip the wordpress zip then place into a server folder. (example: /home/ubuntu/var/www)
  3. Start a database of MYSQL with name "wordpress"
  4. Go to <http://example.com/wordpress/readme> following the instrustion
  5. The pre-config database should be working in the config setup.
  6. Login the panel
  7. Go to plugin
  8. Install "WordPress REST API Authentication" from the plugin market
  9. Enable the "WordPress REST API Authentication"
  10. Go to "WordPress REST API Authentication" setting from sidebar
  11. On "Configure API Authentication" tab, select Basic Auth as Authentication Method and Username : Password as Basic Auth Authentication Key
  12. Save configuration
  13. Complete!

LAMP troubleshoot

Kernel

sudo nano /var/www/html/.htaccess

.htaccess

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Kernal

sudo service apache2 restart

Kernal

sudo nano /etc/apache2/apache.conf

apache.conf

<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>

CHANGE None TO All in apache.conf

<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

Kernal

sudo a2enmod rewrite

Kernal

sudo service apache2 restart

Testing

1. Create Post

Endpoint:
(http://example.com/wordpress/wp-json/wp/v2/posts) Method:
POST Authorization:
Basic Auth - Username:Password Header:

{
	"Content-Type":"application/json"
}

Body:

{
	"date":"2019-11-29 16:00:00",
	"date_gmt":"2019-11-29 16:00:00",
	"status":"publish",
	"title":"PageTesto",
	"content":"Standart Content"
}

Response:

{
	"id": 5,
	"date": "2019-11-29T16:00:00",
	"date_gmt": "2019-11-29T08:00:00",
	"guid": {
		"rendered": "http://localhost/wordpress/?p=5",
		"raw": "http://localhost/wordpress/?p=5"
	},
	"modified": "2019-11-29T16:00:00",
	"modified_gmt": "2019-11-29T08:00:00",
	"password": "",
	"slug": "pagetesto",
	"status": "future",
	"type": "post",
	"link": "http://localhost/wordpress/?p=5",
	"title": {
		"raw": "PageTesto",
		"rendered": "PageTesto"
	},
	"content": {
		"raw": "Standart Content",
		"rendered": "<p>Standart Content</p>\n",
		"protected": false,
		"block_version": 0
	},
	"excerpt": {
		"raw": "",
		"rendered": "<p>Standart Content</p>\n",
		"protected": false
	},
	"author": 1,
	"featured_media": 0,
	"comment_status": "open",
	"ping_status": "open",
	"sticky": false,
	"template": "",
	"format": "standard",
	"meta": [],
	"categories": [1],
	"tags": [],
	"permalink_template": "http://localhost/wordpress/2019/11/29/%postname%/",
	"generated_slug": "pagetesto",
	"_links": {	
		"self": [
			{
				"href": "http://localhost/wordpress/wp-json/wp/v2/posts/5"
			}
		],
		"collection": [
			{
				"href": "http://localhost/wordpress/wp-json/wp/v2/posts"
			}
		],
		"about": [
			{
				"href": "http://localhost/wordpress/wp-json/wp/v2/types/post"
			}
		],
		"author": [
			{
				"embeddable": true,
				"href": "http://localhost/wordpress/wp-json/wp/v2/users/1"
			}
		],
		"replies": [
			{
				"embeddable": true,
				"href": "http://localhost/wordpress/wp-json/wp/v2/comments?post=5"
			}
		],
		"version-history": [
			{
				"count": 0,
				"href": "http://localhost/wordpress/wp-json/wp/v2/posts/5/revisions"
			}
		],
		"wp:attachment": [
			{
				"href": "http://localhost/wordpress/wp-json/wp/v2/media?parent=5"
			}
		],
		"wp:term": [
			{
				"taxonomy": "category",
				"embeddable": true,
				"href": "http://localhost/wordpress/wp-json/wp/v2/categories?post=5"
			},
			{
				"taxonomy": "post_tag",
				"embeddable": true,
				"href": "http://localhost/wordpress/wp-json/wp/v2/tags?post=5"
			}
		],
		"wp:action-publish": [
			{
				"href": "http://localhost/wordpress/wp-json/wp/v2/posts/5"
			}
		],
		"wp:action-unfiltered-html": [
			{
				"href": "http://localhost/wordpress/wp-json/wp/v2/posts/5"
			}
		],
		"wp:action-sticky": [
			{
				"href": "http://localhost/wordpress/wp-json/wp/v2/posts/5"
			}
		],
		"wp:action-assign-author": [
			{
				"href": "http://localhost/wordpress/wp-json/wp/v2/posts/5"
			}
		],
		"wp:action-create-categories": [
			{
				"href": "http://localhost/wordpress/wp-json/wp/v2/posts/5"
			}
		],
		"wp:action-assign-categories": [
			{
				"href": "http://localhost/wordpress/wp-json/wp/v2/posts/5"
			}
		],
		"wp:action-create-tags": [
			{
				"href": "http://localhost/wordpress/wp-json/wp/v2/posts/5"
			}
		],
		"wp:action-assign-tags": [
			{
				"href": "http://localhost/wordpress/wp-json/wp/v2/posts/5"
			}
		],
		"curies": [
			{
				"name": "wp",
				"href": "https://api.w.org/{rel}",
				"templated": true
			}
		]
	}
}

2. Get Post

Endpoint:
(http://example.com/wordpress/wp-json/wp/v2/posts) Method:
GET Authorization:
Not Required Header:
Not Required Body: Not Required

Response:

{
	"id": 5,
	"date": "2019-11-29T16:00:00",
	"date_gmt": "2019-11-29T08:00:00",
	"guid": {
		"rendered": "http://localhost/wordpress/?p=5",
		"raw": "http://localhost/wordpress/?p=5"
	},
	"modified": "2019-11-29T16:00:00",
	"modified_gmt": "2019-11-29T08:00:00",
	"password": "",
	"slug": "pagetesto",
	"status": "future",
	"type": "post",
	"link": "http://localhost/wordpress/?p=5",
	"title": {
		"raw": "PageTesto",
		"rendered": "PageTesto"
	},
	"content": {
		"raw": "Standart Content",
		"rendered": "<p>Standart Content</p>\n",
		"protected": false,
		"block_version": 0
	},
	"excerpt": {
		"raw": "",
		"rendered": "<p>Standart Content</p>\n",
		"protected": false
	},
	"author": 1,
	"featured_media": 0,
	"comment_status": "open",
	"ping_status": "open",
	"sticky": false,
	"template": "",
	"format": "standard",
	"meta": [],
	"categories": [1],
	"tags": [],
	"permalink_template": "http://localhost/wordpress/2019/11/29/%postname%/",
	"generated_slug": "pagetesto",
	"_links": {	
		"self": [
			{
				"href": "http://localhost/wordpress/wp-json/wp/v2/posts/5"
			}
		],
		"collection": [
			{
				"href": "http://localhost/wordpress/wp-json/wp/v2/posts"
			}
		],
		"about": [
			{
				"href": "http://localhost/wordpress/wp-json/wp/v2/types/post"
			}
		],
		"author": [
			{
				"embeddable": true,
				"href": "http://localhost/wordpress/wp-json/wp/v2/users/1"
			}
		],
		"replies": [
			{
				"embeddable": true,
				"href": "http://localhost/wordpress/wp-json/wp/v2/comments?post=5"
			}
		],
		"version-history": [
			{
				"count": 0,
				"href": "http://localhost/wordpress/wp-json/wp/v2/posts/5/revisions"
			}
		],
		"wp:attachment": [
			{
				"href": "http://localhost/wordpress/wp-json/wp/v2/media?parent=5"
			}
		],
		"wp:term": [
			{
				"taxonomy": "category",
				"embeddable": true,
				"href": "http://localhost/wordpress/wp-json/wp/v2/categories?post=5"
			},
			{
				"taxonomy": "post_tag",
				"embeddable": true,
				"href": "http://localhost/wordpress/wp-json/wp/v2/tags?post=5"
			}
		],
		"wp:action-publish": [
			{
				"href": "http://localhost/wordpress/wp-json/wp/v2/posts/5"
			}
		],
		"wp:action-unfiltered-html": [
			{
				"href": "http://localhost/wordpress/wp-json/wp/v2/posts/5"
			}
		],
		"wp:action-sticky": [
			{
				"href": "http://localhost/wordpress/wp-json/wp/v2/posts/5"
			}
		],
		"wp:action-assign-author": [
			{
				"href": "http://localhost/wordpress/wp-json/wp/v2/posts/5"
			}
		],
		"wp:action-create-categories": [
			{
				"href": "http://localhost/wordpress/wp-json/wp/v2/posts/5"
			}
		],
		"wp:action-assign-categories": [
			{
				"href": "http://localhost/wordpress/wp-json/wp/v2/posts/5"
			}
		],
		"wp:action-create-tags": [
			{
				"href": "http://localhost/wordpress/wp-json/wp/v2/posts/5"
			}
		],
		"wp:action-assign-tags": [
			{
				"href": "http://localhost/wordpress/wp-json/wp/v2/posts/5"
			}
		],
		"curies": [
			{
				"name": "wp",
				"href": "https://api.w.org/{rel}",
				"templated": true
			}
		]
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment