DEV Community

Aad Pouw
Aad Pouw

Posted on

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');
}
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!

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay