Creating A WordPress Post Using The REST API
The created NextJs application allows for the creation of a new blog post using the WordPress REST API by sending a HTTP POST request to /wp/v2/posts using the schema defined on the WordPress developer website(WordPress, n.d.).
A custom built page was created that contains a form for creating blog posts. This page can only be accessed if the user has a currently authenticated session.
The Create functional component reads the state of the login session by accessing the AuthContext using the useContext hook. More can be read about the implementation of the persistence of an authenticated session by accessing this blog entry.
Creating The Posts Content
Users can provide a blog with a title, an excerpt and can create HTML content for the post within the Post Content textarea shown in the following image.
Each form input on this page uses the useForm form validation hook that was created for performing real time, reactive form validation against regular expressions. More can be read about this hook via this link.
By clicking these buttons on the left of the screen the text found within the code extract shown below is added into the textarea. The strings are assigned to enumerator values as shown below.
An example of some content added can be seen below.
File Upload
The following screenshot shows how the media upload process works. The application listens for a change on an input element and upon a change in the elements value will store the image file selected in a React state variable. By storing this value in a React state variable the user interface can be updated to show the name of the file to the user.
Once the user submits the form the file is appended to an instance of the FormData object. The content type is not set as the FormData object will automatically set this based on the type of content contained within itself. The FormData.append( ) method requires a key and a value that is either of type String or Blob. The File type is an instance of Blob(Mozilla, 2021), meaning the file can be sent using this approach.
// File instanceof Blob => true
Once the response is retrieved, which is in the shape of the previously defined Media type the type is then passed to the function that uploads the blog to the API endpoint for creation of posts. The creation of posts via the REST API is covered in the following section.
Post Creation
Example Requests
This subsection of the post shows the network logs that are a result of the creation of a new post. Before the post creation happens, a file upload occurs to upload an image file to the WordPress server as the posts featured image. Both of these requests are shown below:
Conclusion
References
Mozilla (2021). File - Web APIs | MDN. [online] developer.mozilla.org. Available at: https://developer.mozilla.org/en-US/docs/Web/API/File [Accessed 14 Apr. 2021].
WordPress (n.d.). Posts | REST API Handbook. [online] WordPress Developer Resources. Available at: https://developer.wordpress.org/rest-api/reference/posts/#create-a-post [Accessed 16 Apr. 2021].










Comments
Post a Comment