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?

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

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>
  • 'posts_per_page' => -1 gets all posts. – Jake May 23 '19 at 22:08
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.

  • 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). – Nadeem Khan Mar 22 '14 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. – Sudeep K Rana Mar 23 '14 at 3:27
  • Yes, I did but the code only fetched links from the main page. – Nadeem Khan Mar 24 '14 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.