PWaddict Posted October 9, 2016 Posted October 9, 2016 Hello, I would like to automatically add a class to every internal link. The below module adds a class to every external link. Can someone modify it to do the same thing for internal links only? Thanks in advance. <?php /** * ProcessWire Mark External Links * * ProcessWire 2.x * Copyright (C) 2010 by Ryan Cramer * Licensed under GNU/GPL v2, see LICENSE.TXT * * http://www.processwire.com * http://www.ryancramer.com * */ class TextformatterMarkExternalLinks extends Textformatter { public static function getModuleInfo() { return array( 'title' => 'Mark External Links', 'version' => 102, 'summary' => "Adds a class='external' and a rel='nofollow' to every external link." ); } public function format(&$str) { $str = preg_replace_callback('%<(a\s[^>]+)>%isU', array($this,"mark_external"), $str); } function mark_external($match) { list($original, $tag) = $match; $blog_url = 'http://'.$this->config->httpHost.$this->config->urls->root; if(strpos($tag, "nofollow")) { return $original; } elseif(!strpos($tag, 'http')) { return $original; } elseif(strpos($tag, $blog_url)) { return $original; } else { return "<$tag rel='nofollow' class='external'>"; } } } ?>
teppo Posted October 9, 2016 Posted October 9, 2016 First things first: what do you need this for? If it's a style-related thing use attribute selectors instead. Basically the same thing goes for JavaScript stuff. If you really need a server-side solution to this, the polite thing to do would be giving it a try yourself before asking someone to do it for you. That being said, what you are asking for would mean a) checking that the link isn't relative (href="/foo/" or href="index.html") and b) checking that it doesn't point to your own site (href="your-own-site...) It's really not a very difficult thing to do if you put your mind to it, so why don't you give it a try and if you run into any issues ask help with those? 4
LostKobrakai Posted October 9, 2016 Posted October 9, 2016 As you've already found nico's textformatter you could also use a:not(.external) to get all internal links. 3
PWaddict Posted October 9, 2016 Author Posted October 9, 2016 Thanks for the replies. LostKobrakai's solution is perfect. So, no need to have a module for that. Maybe this topic should be moved to another category. @teppo Already tried to do it but without any success. It's very confusing for me, that's why I asked for help. I wanted the class for ajax loading not to style it. @LostKobrakai Thanks for helping me once again
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now