• Resolved ChrisL

    (@chrslcy)


    Referring to this support post, I was wondering whether and progress has been made on cleaning embed URLs (such as YouTube) which email clients have automatically converted to hyperlinks.

    This is a real problem for the school I work with as the staff’s mail clients are set to convert URLs to hyperlinks by default.

    Many thanks!

    Chris

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Wayne Allen

    (@wayneallen-1)

    Sorry, not yet.

    Thread Starter ChrisL

    (@chrslcy)

    No worries. Would be great to see this as part of Postie’s core, but I’ve created a function that seems to do the trick for anyone in need:

    (modified from this https://stackoverflow.com/questions/55007093/remove-tags-a-to-a-specific-url-domain-php)

    add_filter('postie_post_before', 'unlink_embed_links');
    
    function unlink_embed_links($post) {
    	$post['post_content'] = remove_domain($post['post_content'], 'amazon.com', 'animoto.com', 'cloudup.com', 'crowdsignal.com', 'dailymotion.com', 'flickr.com', 'giphy.com', 'imgur.com', 'issuu.com', 'kickstarter.com', 'meetup.com', 'mixcloud.com', 'photobucket.com', 'reddit.com', 'reverbnation.com', 'scribd.com', 'slideshare.net', 'smugmug.com', 'soundcloud.com', 'speakerdeck.com', 'spotify.com', 'ted.com', 'tiktok.com', 'tumblr.com', 'twitter.com', 'videopress.com', 'vimeo.com', 'wordpress.org', 'wordpress.tv', 'youtube.com', 'youtu.be');
        return $post;
    }
    
    function remove_domain($str, $domainsToRemove) {
        $domainsToRemove = is_array($domainsToRemove) ? $domainsToRemove : array_slice(func_get_args(), 1);
    
        $dom = new DOMDocument;
        $dom->loadHTML("<div>{$str}</div>", LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
    
        $anchors = $dom->getElementsByTagName('a');
        // Code taken and modified from: http://php.net/manual/en/domnode.replacechild.php#50500
        $i = $anchors->length - 1;
        while ($i > -1) {
            $anchor = $anchors->item($i);
    
            foreach ($domainsToRemove as $domain) {
                if (strpos($anchor->getAttribute('href'), $domain) !== false) {
    				if (strpos($anchor->textContent, $domain) !== false) { 
    				// only do it if the link text contains the domain (otherwise it is a deliberate text link and should remain linked)
    					$new = $dom->createElement('p', htmlentities($anchor->textContent)); // wrap url in p tag
    					//$new = $dom->createTextNode($anchor->textContent); // alternatively, place url only without wrapping
    					$anchor->parentNode->replaceChild($new, $anchor);
    				}      
                }
            }
    
            $i--;
        }
    
        // Create HTML string, then remove the wrapping div.
        $html = $dom->saveHTML();
        $html = substr($html, 5, strlen($html) - (strlen('</div>') + 1) - strlen('<div>'));
    
        return $html;
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Cleaning hyperlinked URLs (such as YouTube)’ is closed to new replies.