Logo

dev-resources.site

for different kinds of informations.

setUrl() for WP endpoints

Published at
2/8/2021
Categories
Author
Aad Pouw
Categories
1 categories in total
open
setUrl() for WP endpoints

What it is?

A little js function to pass a wp endpoint url that updates itself if you upload from your local - to your live website!

Why?

Lately I did that and forgot to update my local - to my live endpoint url. The result was that when navigating on my live I ended up on my local and what is not intended. Also, on chrome it invalidated my ssl certificate too!

The code:

async function setUrl(namespace, endpoint){
    if((namespace !== null) && (endpoint !== null))
        return `https://${location.hostname}/wp-json/${namespace}/${endpoint}`;
    else
        return console.error('enter a namespace and/or an endpoint');
}

Example:

const response = await fetcher.fetchData(await setUrl('my/namespace','my_endpoint'),{ //other stuff });

Note:

I'm using a js class here for this example and is out of scoop of this post.
Also, feel free to use this little snippet, for sure without any warranty or reliability!

Featured ones: