How to Extract All External Links from a WordPress Site — Four Methods

How to get all external links from a website

When I got this task and started working on it, I found it very interesting. It can be solved in a few ways, depending on what you have: knowledge of the console, access to the database, or a WordPress admin account.

Problem: Get all external links from a website: with access to the database or to WordPress, or without any access.

Solution: Four ways, from a ready-made application to a small shell script.

1. SEO Spider

SEO Spider by Screaming Frog is a cross-platform application (Ubuntu, macOS, Windows). It crawls the site and exports all outgoing links. There is a free version, limited to 500 URLs, and a paid one without limits (£149 per year at the time of writing). If your site is small and you have no access, this may be the solution for you. My site was bigger, so I moved on.

2. Broken Link Checker

Broken Link Checker is a free WordPress plugin. If you have admin access, you can try it: it collects all links from the content and can filter external ones. See this topic on the support forum for details. On a big site the first scan takes a long time.

3. A database dump and three commands

Now the more interesting solutions. For example, we have a huge site with tens of thousands of posts, many custom post types, etc., and we have the database dump. We need Linux and three commands.

Get all links (only the domain part) from the dump:

grep -Eo "https?://[^/\"' <>]+" database_name.sql > all_links

Remove the links to your own domain:

sed '/your_domain_name\.com/d' all_links > external_links

Keep only unique ones:

sort -u external_links > unique_external_links

4. No access at all: the sitemap and lynx

Let’s make the previous task a bit harder: a huge site, and no access rights. To get the list of pages, look at sitemap.xml. I was lucky, my site had this file. Otherwise a sitemap generator service can help, for example this one (the free version is limited to 500 URLs).

Save the page URLs from the sitemap into a file, one per line:

curl -s https://www.site_name.org/sitemap.xml | grep -Eo '<loc>[^<]+' | sed 's/<loc>//' > sitemap_links

If the sitemap is an index of other sitemaps, run the same command for each of them.

Then a script opens every page with the text browser lynx, takes all links from it with awk, removes the links to our domain and writes the rest to a file:

#!/bin/sh
q=0
while read -r url; do
    lynx -dump -listonly "$url" | awk '/http/{print $2}' | sed '/www\.site_name\.org/d' | grep -Eo '^https?://[^/]+' >> external_links
    q=$((q+1))
    echo "$q $url"
done < sitemap_links

sort -u external_links -o external_links

Install lynx with sudo apt install lynx (Debian/Ubuntu) or brew install lynx (macOS).

NOTE: The script requests every page of the site. On a big site add sleep 1 inside the loop, so the server (or its firewall) does not take you for an attack.

Good luck! 😉