DEV Community

loading...

setUrl() for WP endpoints

aadswebdesign profile image Aad Pouw ・1 min read

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');
}
Enter fullscreen mode Exit fullscreen mode

Example:

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

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!

Discussion (0)

pic
Editor guide