Most of Us Who Work With WordPress Development Sometimes May Have to Work With WordPress Rest API for Better User Experience or Any Other Purpose.Most of The Cases We Use siteurl/wp-json/ as Endpoint of the WordPress Rest API. But Sometimes We Have Issue With The .htaccess File Generation or User Selected Plain Text in The Permalink Settings. Then The /wp-json/ Endpoint Won’t Work. Then it Should Be Something Like This:- siteurl?rest_route=/wp/v2/… . So You Should Dynamically Get the The Rest Endpoint With The WordPress Provided Function get_rest_url() . Which Return The Correct Rest API Endpoint. Also Most of The Cases We Use Rest With Js.So if We Had to Get The Endpoint URL Dynamically With Js We Can Get It From a HTML Generated in The Head of All Pages if You Used wp_head()
in the Header of the WordPress.The HTML line May Like This:-
<link rel="https://api.w.org/" href="http://localhost/wp-json/">
So You Can Get it By
let endpoint = document.querySelector('link[rel="https://api.w.org/').href;
The JS I Provided May Change in Future if The HTML Line Change in Future Version.I Am Considering That You Have Knowledge of Basic JS and querySelector in JS.
This is What I Found When I am in Trouble of This User Experience Issue Where My Theme User Must Have to Change Their Site Permalink Setting From “Plain Text” to Any to Take Advantages of REST API Functionalities on My Theme.Hope It Help You In Future.Thanks for Reading
Please Read More:-
get_rest_url()
get_rest_url() User Contributed Note