Get prompts and use custom endpoints to simplify data loading. As you remember in the last part of this series, what we are doing now is loading all the data and then using react to display any individual citations. The
Considering that there are only a few quotations to be processed, this is not a big problem, but what if there are thousands of quotations? The situation may soon deteriorate. Instead, you will use a custom endpoint to import a list of all post IDS, select one at random from the list, and load the data each time. Let’s start with custom endpoint settings. The following code mixes rest API documentation examples with delicious brance tutorials. In other words, a function of the active topic directory. At the bottom of PHP\/\/ Returns all post IDs.
Functions walden\u get\u all\u post\u ids(){
$all\u post\u ids=get\u posts (array(
\
\
\
));
$all_post_ids returns;
}The
\/\/Walden\/v1\/get all post IDS path addition
Add\u action (\
Register_rest_route (\
\
\
));
}); Now, now http:\/\/walden.dev\/wp-json\/walden\/v1\/get-all-post-ids When called, all post IDs are organized in order. The
Now, let’s quickly change the realt code to use this data instead of the previously executed data. We will start by passing the new custom endpoint and the URL of the previous rest API post. Reactdom Render (male), document.getelementbyid (\
Getallid: function (){
Console Log (\
$ Ajax ({
Url: this Props Idurl,
Data type: \
Cache: false,
Success: function (data){
This Setstate ({data: Data});
This Chooserandomquote();
} Binding (this)
Error: function (XHR, status, error){
Console Error (this.props.url, status, err.tostring();
} Binding (this)
});
},
Selectrandomquote: function (){
Var randomnumber = math Floor (math.random() * this State Data Length);
Var selectedquote = this State Data[randomnumber];
This Setstate ({selectedquoteid: selectedquote});
This Getquote();
},
Getquote: function (){
$ Ajax ({
Url: this Props Dataurl + this State Selectedquoteid,
Data type: \
Cache: false,
Success: function (data){
This Setstate ({selectedquotecontent: data.co
Ntent Rendered});
} Binding (this)
Error: function (XHR, status, error){
Console Error (this.props.url, status, err.tostring();
} Binding (this)
});
},
Componentdidmount: function (){
This Getallids();
}You will also make some changes to the content passed in the quote component to reflect the above changes. The
Quickly refresh the basic application, and everything seems to be going well. We still have the quotation marks and new loading capabilities provided by the rest API. Just use the custom endpoint to complete the task. It is worth pointing out that we have taken a very happy road so far. Skilled react or word press developers may object to everything from the displayed coding style to inadequate general error handling, message loading, and so on. The
We will leave most of the potential improvements to the reader for practice, but there is a small project to be solved at this stage. In this step of cleaning up HTML output in WordPress, we continue to use the dangerous setinnerhtml method, which sounds a bit ominous in the quote component. If WordPress can reprocess the business instead of being embarrassed in realt, it will be a very ideal choice. Var reference =react. Createclass ({
Rendering: functions (){
Returns(
Male
);
}The
}); Fortunately, as described in the rest API documentation, it is easier to add additional fields to an existing endpoint using register\u api\u field. Again look forward to the dellishers think tank tutorial mentioned earlier, the following features. The existing code in PHP has been modified to add additional fields to the beautiful, friendly plain text version of the post response with quotation marks. The
\/\/Return the plain text content of the post
Function walden\u return\u plaintext\u content ($object, $field\u name, $request){
Return strip_tags (html\u entity_decode ($object[‘content’][‘rendered’]);
}The
Add\u action (‘rest\u api\u init’,’setup\u rest\u route’);
Add\u action (‘rest\u api\u init’,’add\u plaintext\u response’);
\/\/Walden\/v1\/get all post IDS path addition
Function setup_rest_route(){
Register_rest_route (\
\
\
));
}The
Function add\u plaintext\u response(){
\/\/Add plain text content to get requests for individual posts
Register\u api\u field(
\
Plain text
Sort(
‘get\u callback’= >’walden\u return\u plaintext\u content’,
)The
}The
Customize endpoints for security http:\/\/walden.dev\/wp-json\/walden\/v1\/get-all-post-ids We will check whether to continue running. Now especially http:\/\/walden.dev\/wp-json\/wp\/v2\/posts\/4 Let’s see what happens with calling a post. Happy days