-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-modifylinks.php
43 lines (40 loc) · 1.27 KB
/
wp-modifylinks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
/*
Plugin Name: Modify Links
Plugin URI: http://robertsky.com
Description: This plugin modifies links in posts upon saves, adding title automatically.
Version: 0.0.1
Author: Robert Sim
Author URI: http://robertsky.com
Text Domain: wp-modifylinks
*/
function rsky_modify_link_posts($data) {
//Get content
$dom = new DOMDocument;
$dom->loadHTML($data);
foreach ($dom->getElementsByTagName('a') as $node) {
if($node->hasAttribute('href')){
if(!($node->hasAttribute('title'))) {
$url = $node->getAttribute('href')->textContent;
$url = esc_url_raw(trim($url,'"'));
$args = array(
'sslverify' => false,
);
$webpage = wp_remote_get($url);
if(!is_wp_error($webpage)) {
if(wp_remote_retrieve_response_code($webpage) === 200 && $body = wp_remote_retrieve_body($webpage)) {
$remote_dom = new DOMDocument;
$remote_dom->loadHTML($body);
$title = $dom->getElementsByTagName('title');
if($title->length > 0) {
$node->setAttribute('title', $title->item(0)->textContent);
}
}
}
}
}
}
$data = preg_replace('/^<!DOCTYPE.+?>/', '', str_replace( array('<html>', '</html>', '<body>', '</body>'), array('', '', '', ''), $dom->saveHTML()));
return $data;
}
add_filter('content_save_pre','rsky_modify_link_posts');