Please be aware if you see weird stuff like cpt_custom_post_type[singular_label]
thats for future refence in a puppeteer script for automatically testing this.
- Write or copy docker-compose file.
- Run
Docker-compose up -d
- Install WP
#Postman
http://localhost:8000/wp-json
will error- Login to WP-admin and go to
http://localhost:8000/wp-admin/options-permalink.php
- click "Post name" ```input value = "/%postname%/"
- Use postman to make a GET request to
http://localhost:8000/wp-json/wp/v2/posts
- repeat for
http://localhost:8000/wp-json/wp/v2/users/1
- Make a POST request with postman
http://localhost:8000/wp-json/wp/v2/posts
set the HEADERS toContent-Type
and value =application/json
. Then for the BODY added some json.
{
"title":"Post One"
"content":"This is post one"
}
Should get 401 because the header didn't get "Authorization" and "Bearer someHash".
-
Autheticate with JWT goto
http://localhost:8000/wp-admin/plugin-install.php
search for JWT and install "JWT Authentication for WP REST API" click install and active. -
Change .htaccess per the JWT Authentication for WP REST API docs
RewriteEngine on
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
-
Change
wp-config.php
adddefine('JWT_AUTH_SECRET_KEY', 'your-top-secret-key');
enable CORSdefine('JWT_AUTH_CORS_ENABLE', true);
-
Make a POST request to
http://localhost:8000/wp-json/jwt-auth/v1/token
Must use key[{"key":"Content-Type","value":"application/json","description":"","enabled":true}]
RAW should be:
{
"username": "admin",
"password": "password"
}
You should get a response like:
{
"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJUAzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9sb2NhbGhvc3Q6ODAwMCIsImlhdCI6MTU1Nzk0NTUyNSwibmJmIjoxNTU3OTQ1NTI1LCJlePBiOjE1NTg1NTAzMjUsImRhdGEiOnsidXNlciI6eyJpZCI6IjEifX19.epspJ4MUR1p7-1Q3ZqkmrLxRGblPBqIJzND1OR3Ro2I",
"user_email": "you@site.com",
"user_nicename": "admin",
"user_display_name": "admin"
}
- Make a new blog post using a "POST request" at the url
http://localhost:8000/wp-json/wp/v2/posts
. Copy the token in your POST requst HEADER.
"headers": {
"Content-Type": "application/json",
"Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJUAzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9sb2NhbGhvc3Q6ODAwMCIsImlhdCI6MTU1Nzk0NTUyNSwibmJmIjoxNTU3OTQ1NTI1LCJlePBiOjE1NTg1NTAzMjUsImRhdGEiOnsidXNlciI6eyJpZCI6IjEifX19.epspJ4MUR1p7-1Q3ZqkmrLxRGblPBqIJzND1OR3Ro2I",
},
Then in the BODY of the request add your new post data (RAW):
{
"title": "Post Two",
"content": "Look you made a new post with the REST API !",
"status": "publish"
}
Now you should be able to see the new post added.
- Install & Activate Custom Post Type UI
Configure a New Post
type "books" in cpt_custom_post_type[name]
&& cpt_custom_post_type[label]
and type book in cpt_custom_post_type[singular_label]
Show in REST API must be true cpt_custom_post_type[show_in_rest]
Excerpts should also be checked.
Grab stuff from https://www.lipsum.com/
- Check post man for books using GET
http://localhost:8000/wp-json/wp/v2/books
Note the following. This will be important when working with react because you will need to escape the html incontent.rendered
.
"content": {
"rendered": "\n<p> ... </p>\n",
"protected": false
},
- Install & Activate ACF to REST API go to the new "Custom Fields" and add new. Field label = Publisher
Now if you go to your books. You should see a new field for publisher.
- Use create-react-app
npx create-react-app frontend
- Once it is done run
cd frontend/ && npm i axios react-router-dom
- Once everything is install and you are in the /frontend, run
npm start
- clean up react boilerplate by deleting
cd /src/ && rm rm App.test.js index.css serviceWorker.js logo.svg
- open "index.js and app.js" and remove references to the files.
- run
mkdir components && touch components/Books.js
- components/Books.js is where we will make the initial request to the API
- add proxy to package.json
"proxy":"http://localhost:8000"
and RESTART the server with npm start. - using the visual studio extention "es 6 react redux graph QL snippets" in Books.js, run ```RCE`` and hit tab. This creates a class based component.
- Import axios to Books.js.
Set the state of the component to an empty array with isLoaded false.
Mount the Component to make a GET request to
/wp-json/wp/v2/books
so that the RESponse SETs the STATE of books: equal to the RESponse's data. Then console.log the state (before the returned html). Finally, import books in app.js and call the<Books />
component into the return statement. Inspect the console and you should now see the array of books. - In Books.js, pull the state with destructuring.
const {books, isLoaded} = this.state;
. And create a conditional where if isLoaded:true, you return a map of thebooks.title.rendered
, else you should return a statement that nothing was loaded.
return (
<div>
{books.map(book => (
<h4> {book.title.rendered} </h4>
)
)}
</div>
)
- Create a new component called
BookItem.js
this will be a class-based component that will return the title via props in an ```
const { title } = this.props.book;
return (
<div>
<h2>{title.rendered}</h2>
</div>
)
Import this component into books. The component will be used like <BookItem key={books.id} book={book} />
and requires PropTypes.
-
GET and display the except with dangerouslySetInnerHTML. Now import PropTypes and axios and set the BookItem component to have a static propType.
-
Getting featured_medi
XX. Create a class based component called BookPage.
XX. (42min) goto app.js