Create your own endpoint with WordPress "WP REST API"

Create your own endpoint with WordPress

Create your own endpoint with WordPress


Create your own endpoint with WordPress, I will introduce how to use the custom endpoint function provided in “WP REST API” to add an original endpoint that is easier to use and get the information you want.By adding endpoints, you can get a deeper understanding of how the WP REST API works inside WordPress.

table of contents

About custom endpoint function Add an endpoint Access the added endpoint Finally

Create your own endpoint with WordPress, About custom endpoint function

Create your own endpoint with WordPress, The “WP REST API” comes with a variety of endpoints from the beginning, but you may not be able to get the information you want with just those endpoints. In such a case, there is a custom endpoint function that allows you to add the original endpoint and freely implement processing such as what value to get from which table .“WP REST API” is often used to link WordPress with other systems. In such a case, if you create your own endpoint with the custom endpoint function, it will match the data format of the linked system. It is very convenient because you can freely format the execution result .Also, custom endpoint features aren’t just useful for browsing data. If you want to perform complicated processing (registration in the case of …, deletion in the case of …) through “WP REST API” , you can create an original endpoint and all the processing will be completed with one call. ..In the next chapter, we will actually add an endpoint of the function “Get a list of users and a list of articles posted by those users” to WordPress.

Create your own endpoint with WordPress, Add an endpoint

In order to create the original endpoint, we will add the ” URL definition” and “process to be executed ” of the endpoint to be added to the functions.php of the theme currently applied in WordPress .

Define the URL of the endpoint

Create your own endpoint with WordPress, First, define the URL to run the endpoint. What we define here is the “/ wp / v2 / posts” part of / wp-json / wp / v2 / posts (for endpoints that get posts ).Please add the following contents.functions.php/ * ================================ * Added original endpoint of WP REST API * ================================ * / function add_rest_original_endpoint () {// Register the endpoint register_rest_route ( ‘wp / custom’ , ‘/ posts_by_users’ , array ( ‘methods’ => ‘GET’ , // Function executed when accessing the endpoint ‘callback’ => ‘get_posts_by_users’ , ));} add_action ( ‘rest_api_init’ , ‘add_rest_original_endpoint’ );The first argument “wp / custom” and the second argument “/ posts_by_users” of the register_rest_route function are the URL of the endpoint.The URL of the endpoint created this time is <WordPress top page> / wp-json / wp / custom / posts_by_users .attributes of the specified array as the third argument of register_rest_route function ‘callback’ , the name of the function is executed when you access to the endpoint ( Get_posts_by_user to specify) in the string.

Create your own endpoint with WordPress, Implement the process

Create your own endpoint with WordPress, Create the get_posts_by_user function specified as the callback destination in 2.1 and implement the processing contents. The content of the process will be added to functions.php as in 2.1 .functions.phpfunction get_posts_by_users () { $ args = array ( orderby => ‘ID’ , order => ‘ASC’ , // Maximum data for 20 users number => 20 , ); // Get a list of users $ users = get_users ( $ args );$ result = array (); if ( $ users ) { // Process for the number of users foreach ( $ users as $ user ) { $ postargs = array ( author => $ user- > ID , Numberpost => – 1 ); // Get all blogs posted by users $ post_list = get_posts ( $ postargs );$ post_results = array (); // Process the number of acquired blogs foreach ( $ post_list as $ tmppost ) { // Extract only the title and URL for the return value array_push ( $ post_results , array ( $ post_results , array ( title => $ tmppost- > post_title , url => get_permalink ( $ tmppost- > ID ) )); } // Add data to the return array array_push ( $ result , array ( $ result , array ( //USER ID user_id => $ user- > ID , // User’s nickname user_name => $ user- > display_name , // List of blog information posted by users posts => $ post_results )); } } return $ result ; }get_users function After obtaining the information of the user (up to 20 people) with, those of the blog user submitted get_posts function is the process of getting in.Since the execution result of the get_users function and get_posts function includes information unnecessary for this API, only the necessary information (user ID, user name, posted blog title, blog URL) is extracted and the return value is $. It is set to result .Create your own endpoint with WordPress, What you can receive as the result of executing the API is a character string in the data format called JSON, but for the return value of the get_posts_by_users function created this time , please return the PHP object (array) as it is. * Since it is automatically converted to a JSON character string, there is no need to convert it using the json_encode function.

Create your own endpoint with WordPress,  Access the added endpoint

Create your own endpoint with WordPress, First, let’s access the original endpoint added in 2. with a web browser.<WordPress top page> / wp-json / wp / custom / posts_by_usersMake sure that you can get the user information and blog information in the JSON string.If you can’t get it, check that the URL is correct and that the implementation of 2. is correct.After confirming, let’s use JavaScript to acquire data from the original endpoint and display a list of posts for each user on the screen of the Web browser.<script> $ ( function () { $ . Ajax ({ type : ‘GET’ , url : ‘ http : //’ , dataType : ‘json’ }). done ( function ( json ) { var html = ” ; // Iterate for the number of users $ . Each ( Json , Function ( I , Row ) { html + = ‘<article class = “wp-article”>’ ; //username Var Name = Row . User_name html + = ‘<h3>’ + name + ‘post list </ h3>’ ;Var Posts = Row . Posts ; If ( Posts . Length > 0 ) { For ( I = 0 ; I < Posts . Length ; I Tasutasu ) { Html Tasu = ‘<the p-> <A Href=”‘ Tasu Posts [I ]. Url Tasu'”> ‘ Tasu ( I Tasu 1 ) Tasu ‘:’ Tasu Posts [ I .] Title Tasu ‘</A> </ p>’ ; }} else { html + = ‘<p> No posts </ p>’ ; }html + = ‘</ article>’ ; }); $ ( ‘#wordpress_blog’ ). append ( html )}). fail ( function ( json ) { Console . Error ( ‘failed WordPress blog post acquisition.’ ) }); }); </ script>Specify the URL of the original endpoint added in 2. in the url of $ .ajax. All you have to do is assemble the HTML according to the execution result.

Create your own endpoint with WordPress, Finally

Create your own endpoint with WordPress, This time, I introduced how to add an original endpoint using the custom endpoint function of “WP REST API”.The “WP REST API” endpoint can also specify request parameters (including custom endpoints) . For example, it is possible to implement a process such as acquiring the post information of the user who matches the specified user ID (or user name).By combining request parameters and custom endpoints, you can implement more flexible endpoint processing, so please check it out and make full use of
VPS is in stock, Use this coupon code "VPSBrustDeal" to get 20% discountOrder Now
+ +