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 Answers
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>, '. $link.'</li>';
}
}
endforeach;
wp_reset_postdata();
?>
</ol>