<?php | |
/* | |
* Create a private WordPress page with permalink as links. | |
* Upload the code with the file name as page-links.php in the active themes directory. | |
* Upload simple_html_dom file from here (https://sourceforge.net/projects/simplehtmldom/files/simplehtmldom/1.5/) | |
* to the same directory | |
* All done. | |
* Now logged in admin, visit yoursite.com/links and you will see a list of all external links from your WordPress blog. | |
* Whitelist the links and domains in the array $_allowed_links and $_allowed_domain respectively to | |
* reduce the number of links shown in the report. | |
* If you don't wish to whitelabel the complete domain, add them to $_allowed_links array. | |
*/ | |
?> | |
<ol> | |
<?php | |
$_allowed_links = array( | |
'http://imojo.in/@shabbirbhimani', | |
'https://www.siteground.com/go/imtipsco', | |
); | |
$_allowed_domains = array( | |
'github.com', | |
'wordpress.com', | |
'en.wikipedia.org', | |
'policies.google.com', | |
'google.com', | |
'imtips.co', | |
); | |
$_output_domains = array(); | |
function is_allowed_domain($url) | |
{ | |
global $_allowed_links, $_allowed_domains, $_output_domains; | |
if(in_array($url, $_allowed_links)) return true; | |
$the_domain = strtolower(parse_url($url, PHP_URL_HOST)); | |
if(empty($the_domain)) return true; // Hash based url | |
if(in_array($the_domain, $_allowed_domains)) return true; | |
if (strpos($the_domain,'www.') !== false) { | |
$more_domain = substr($the_domain, 4); | |
} | |
else{ | |
$more_domain = 'www.' . $the_domain; | |
} | |
if(in_array($more_domain, $_allowed_domains)) return true; | |
if(!in_array($the_domain, $_output_domains)) | |
$_output_domains[] = $the_domain; | |
return false; | |
} | |
global $post; | |
include_once( get_stylesheet_directory() . '/simple_html_dom.php' ); | |
$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(); | |
$html = str_get_html($_post_content); | |
$links = $html->find('a'); | |
foreach($links as $link) { | |
if (!is_allowed_domain($link->href)) { | |
echo '<li><a target="_blank" href="'.get_permalink($post->ID) . '">' . get_the_title() .'</a> - '. $link->outertext . ' - ' .$link->href . '</li>'; | |
} | |
} | |
endforeach; | |
// Only change few params for pages | |
$args['post_type'] = 'page'; | |
$mypages = get_pages( $args ); | |
foreach( $mypages as $post ) : setup_postdata($post); | |
//Get Post content | |
$_post_content = get_the_content(); | |
if(!empty($_post_content)) { | |
$html = str_get_html($_post_content); | |
$links = $html->find('a'); | |
foreach($links as $link) { | |
if (!is_allowed_domain($link->href)) { | |
echo '<li><a target="_blank" href="'.get_permalink($post->ID) . '">' . get_the_title() .'</a> - '. $link->outertext . ' - ' .$link->href . '</li>'; | |
} | |
} | |
} | |
endforeach; | |
echo '<h1>Linked Domains</h1>'; | |
foreach($_output_domains as $domain) | |
{ | |
echo "'$domain',\r\n<br />"; | |
} | |
wp_reset_postdata(); | |
?> | |
</ol> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment