Export any WordPress Post URLs using Console and JavaScript

turned-on monitor

Are you looking to export all the post URLs from any WordPress website? Look no further!

In this comprehensive tutorial, we will guide you through the step-by-step process of exporting WordPress post URLs to text using JavaScript code and the browser console.

The best part? It works seamlessly with almost any WordPress site, and you don’t even need to be the website administrator. Let’s get started!

Learn How to Export WordPress URLs using code in 2023

  • Step 1: To begin, open your preferred web browser and navigate to the homepage of the WordPress site from which you want to export the post URLs. You can even try this method with popular WordPress sites like TechCrunch. Once you’ve reached the desired homepage, we need to access the browser console. Simply press the F12 key on your keyboard, and the console will appear, ready for action.
  • Step 2: In the console, you can now paste the JavaScript code snippets that we will provide. Look for the first code snippet labeled as “Code 1” in the video description or accompanying text. This code snippet will initiate an automated action, gathering all the necessary data. Copy and paste it into the console to proceed.
    //COPY & PASTE CODE 1:

let wpUrl = window.location.href;
let wpApiUrl = wpUrl + 'wp-json/wp/v2/posts?per_page=100&page=1';
let arrayPosts = [];
fetch(wpApiUrl)
.then(response => response.json())
.then(json => {
    for (const post of json) {
        arrayPosts.push(post.link);
    }
})
.then(() => {
    console.log(arrayPosts.join('\n'));
});

After pasting the code snippet in the console (and pressing ENTER key), the console will display the first 100 post URLs. Depending on the speed of the website, the results can be instantaneous or take a few seconds to load. This method is compatible with most WordPress API-enabled sites. Fortunately, the WordPress API is typically enabled by default, so you’re good to go!

Step 4: But what if your WordPress site has more than 100 post URLs? No worries! We’ve got you covered. You can use another code snippet, labeled as “Code 2” in the video description, to query the API in a loop and retrieve as many URLs as you need. Be sure to grab this code snippet and paste it into the console as well.

    //COPY & PASTE CODE 2:

let totalPages=3;
let wpUrl = window.location.href;
let arrayPosts = [];
for (let i = 1; i <= totalPages; i++) {
    let wpApiUrl = wpUrl + 'wp-json/wp/v2/posts?per_page=100&page=' + i;
    fetch(wpApiUrl)
    .then(response => response.json())
    .then(json => {
        for (const post of json) {
            arrayPosts.push(post.link);
        }
    })
    .then(() => {
        if (i === totalPages) console.log(arrayPosts.join('\n'));
    });
}

Once you’ve pasted the second code snippet, the console will display URLs for as many pages as you have configured in the code. Each page will show 100 URLs. Exciting, isn’t it? Now you can copy this valuable data and paste it anywhere you want for further analysis or exploration.

If you desire a specific number of pages, you can adjust the variable “totalPages” in the code to query the desired number of pages, with each page displaying 100 URLs. Feel free to customize it to suit your needs.

Congratulations! You’ve successfully exported all the post URLs from your WordPress website using JavaScript and the browser console. If you encounter any difficulties or need additional assistance, don’t hesitate to leave a comment. We’re here to help you every step of the way.

Thank you for watching this tutorial, and happy data exploration! If you don’t want to miss out on more exciting codes for your browser console, be sure to subscribe to our channel.

Code mirror: https://codepen.io/netgrowstech/pen/zYMomKj

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top