1

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

  • Is it possible to achieve this via a plugin?

  • Is it possible to achieve this with a built-in wordpress api?

  • Is it possible to achieve this via some free 3rd party tool?

What approach should be followed for achieving this?

1
  • 2
    Please add some information about what you have done so far, best with some code - see How to Ask for help. Mar 21, 2014 at 19:58

2 Answers 2

3

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
  • 1
    'posts_per_page' => -1 gets all posts.
    – Jake
    May 23, 2019 at 22:08
  • I know this stack is a while ago - but has some really great info and code. The issue I'm seeing is that the regular expression is not parsing the links correctly when they're contained in the same line of text. I'm terrible at regex but trying to work through it. Any thoughts? Thx again.
    – Daniel
    Apr 26 at 15:09
4

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-content\themes\YOUR_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.

3
  • It is only displaying external links for a single page and it also includes links from footer and sidebar. What I need is a list of all external links present on the site (inside the post content only). Mar 22, 2014 at 17:02
  • Have you made the change $_site_url = 'google.com'; to your domain $_site_url = 'YOUR_URL.com';. This code is not designed to fetch any footer content. get_post_field( 'post_content', $_post_id); fetches only post content part. Mar 23, 2014 at 3:27
  • Yes, I did but the code only fetched links from the main page. Mar 24, 2014 at 6:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.