How to list all external URLs that I have posted on my WordPress blog?

I want to list all the external links of my posts in a single place/file. How can I achieve this?

What approach should be followed for achieving this?

Related posts

Leave a Reply

2 comments

  1. The above code had pagination concerns when I wanted to parse each and every page. So, building on the above, plus a kicker to search for links without ‘nofollow’ attributes, I came up with the following:

    <ol>
    <?php 
    
    global $post;
    
    //Write Site URL below.
    //Don't write http:// or anything like that. just domain.com or domain.net
    $_site_url = 'google.com';
    
    $args = array(
        'posts_per_page'   => 9999999999,
        'offset'           => 0,
        'category'         => '',
        'category_name'    => '',
        'orderby'          => 'ID',
        'order'            => 'DESC',
        'include'          => '',
        'exclude'          => '',
        'meta_key'         => '',
        'meta_value'       => '',
        'post_type'        => 'post',
        'post_mime_type'   => '',
        'post_parent'      => '',
        'author'       => '',
        'author_name'      => '',
        'post_status'      => 'publish',
        'suppress_filters' => true 
    );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) : setup_postdata($post); 
    
      //Get Post content
      $_post_content =  get_the_content();
    
      $site_parts = explode('.',$_site_url);
      $site_suffix = '.'.$site_parts[1];
    
      //Using regular expression to match hyperlink
      preg_match_all('|<a.*(?=href="([^"]*)")[^>]*>([^<]*)</a>|i', $_post_content, $match);
    
      foreach($match[0] as $link){
        //Filtering out internal links
        $parts = explode($site_suffix, $link);
        $domain = explode('//',$parts[0]);
        //echo $domain[1];
        if ($domain[1] != 'www.'.$site_parts[0] && $domain[1] != $site_parts[0] && strpos($link, 'nofollow') === FALSE){
            echo '<li><a href="'.get_permalink($post->ID) . '">' . get_the_title() .'</a>,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'. $link.'</li>';
        }
      }
    
    endforeach;
    wp_reset_postdata(); 
    
    ?>
    </ol>
    
  2. Create a page with the name links. Pages >> Add New into your wordpress panel. Just name it links and publish. See below.

    enter image description here

    Now go to your theme folder wp-contentthemesYOUR_ACTIVE_THEME_FOLDER and create a page page-links.php.

    And paste this code in page-links.php

    <?php  
    
    $the_query = new WP_Query(array('post_type' => 'post'));
    while($the_query->have_posts()):
        $the_query->the_post();
        $_post_id = get_the_id();
    
    //Write Site URL below.
    //Don't write http:// or anything like that. just domain.com or domain.net
    $_site_url = 'google.com';
    
    //Getting Post content
    $_post_content =  get_post_field( 'post_content', $_post_id);
    
    
    $site_parts = explode('.',$_site_url);
    $site_suffix = '.'.$site_parts[1];
    //Using regular expression to match hyperlink
    preg_match_all('|<a.*(?=href="([^"]*)")[^>]*>([^<]*)</a>|i', $_post_content, $match);
    
    foreach($match[0] as $link){
        //Filtering out internal links
        $parts = explode($site_suffix, $link);
        $domain = explode('//',$parts[0]);
        //echo $domain[1];
        if ($domain[1] != 'www.'.$site_parts[0] && $domain[1] != $site_parts[0]  ){
            echo $link.'<br>';
        }
    }
    
    endwhile;
        wp_reset_postdata();
    ?>
    

    Now you need to make a change in the code above.

        //Write Site URL below.
    //Don't write http:// or anything like that. just domain.com or domain.net
    $_site_url = 'google.com';
    

    In place of $_site_url = 'google.com'; write your own domain. Make sure it’s without www and without http://

    Have a look at the page.

    NOTE: All pages that are linked internally are avoided, only pages that are linked to external sites will be visible.

    Customize code for the needful.