Skip to:
Content
Pages
Categories
Search
Top
Bottom

Open Links in new window

  • What can I change to have all links that are posted on the site open in a new window instead of the window that the forum is using? Thanks!

Viewing 25 replies - 1 through 25 (of 49 total)
  • anyone?

    I also would like to know the answer to this question. Can anyone say if it’s been done before?


    chrishajer
    Participant

    @chrishajer

    Courtesy of _ck_ http://ckon.wordpress.com/2007/07/12/bbpress-plugin-bb-tweaks/

    <?php
    /*
    Plugin Name: Target Blank
    Description: append target="_blank" to all links
    Plugin URI: http://ckon.wordpress.com/2007/07/12/bbpress-plugin-bb-tweaks/
    Author: _ck_
    Version: 0.04
    */
    //add target=_blank to post links
    function bb_target_blank( $text ) {
    $text = preg_replace('|<a (.+?)>|i', '<a $1 target="_blank">', $text);
    return $text;
    }
    add_filter('pre_post', 'bb_target_blank');
    ?>


    Sam Bauers
    Participant

    @sambauers

    The target attribute is deprecated (for those who care), use this instead:

    <?php
    /*
    Plugin Name: Links in new window
    */
    //add onclick="window.open(this.href); return false;" to post links
    function bb_links _in_new_window( $text ) {
    $text = preg_replace('|<a (.+?)>|i', '<a $1 onclick="window.open(this.href); return false;">', $text);
    return $text;
    }
    add_filter('pre_post', 'bb_links _in_new_window');
    ?>


    chrishajer
    Participant

    @chrishajer

    Sam, that won’t work with Javascript turned off though, will it?

    That said, I think opening links in a new window or tab, based on browser configuration, is a bad idea anyway.

    Hey Chris, just wondering why you would think this?

    When I’m reading a newspaper article with links, I don’t want to be taken out of the article to quickly browse the other link.

    The same is true for a forum. My main attention is on the thread’s conversation, but I may want to browse links that people include when referring to an issue. What I don’t want is to be taken out of the thread completely, and need to go back hoping I’ll find my place.

    Anyway, thanks for putting up a possible solution. I’ll try it out.


    Sam Bauers
    Participant

    @sambauers

    @chrishajer

    Luckily the sort of people who turn off javascript also happen to be the sort of people who hate having new windows forced on them. :)

    @jaydedman

    I agree with chrishajer that forcing new windows is bad. If I want a new window or tab then I can make that happen myself. But when a new window is forced on me I can’t make it not do it.


    chrishajer
    Participant

    @chrishajer

    jaydedman

    I think my dislike of it started when tabbed browsers came around. There is no target="_newtab", and I want to control where the windows open. If I want it in a new tab or new window, I do that with my browser. Let me control it rather than specifying where it happens.

    I dislike it more on some days than others, not as much as I dislike <marquee> and <blink>, but I still tend to dislike it. It’s just a personal preference though. Here’s something related with a couple discussions linked to it:

    http://www.456bereastreet.com/archive/200603/the_target_attribute_and_opening_new_windows/


    _ck_
    Participant

    @_ck_

    TARGET is deprecated?!

    Throw off your shackles of validation, seriously.

    I promise you that browsers 10+ years from now will still support TARGET. I’ll take html over likely-blocked javascript code anyday. At least firefox can intercept the TARGET and open it as a tab which is the user-prefered behavior.


    chrishajer
    Participant

    @chrishajer

    > shackles of validation

    Most awesome quote of the day!


    811038
    Inactive

    Thanks for the help, I knew it, but I would like to insert my ‘target=”_blank” ‘ in the file Adapted place to see the coments with url added , and of course, I would they works. My question is:

    which file is it? (load.php, post.php, target.php)

    or , better, which function ?

    thank you again


    chrishajer
    Participant

    @chrishajer

    I’m sorry, I don’t understand the question. This won’t be in a template file, and it’s a bad idea to modify core files, which is why the best practice is to use a function as was posted here.


    811038
    Inactive

    thank you Chrishajer: Excus me my bad english , I hope to explain better now:

    I know that it’s a bad idea to modify some bbPress files , I know , I know. But my question is that I think I can insert the ‘target=”_blank” ‘ tag in the place qhere the php engine reads the user comment and shows it and the url link goes to a new window explorer.

    you can see the exemple in my web, and I would like this link works in another window. Click on “BIENVENIDOS AL FORO !!!! ” and see the third comment of ” jcvandamm Admin Principal and there’s some url link:

    http://www.capsir.es/rafa/forum/

    thanks for your help and if you can tell me something, please, sincerelly do it:


    chrishajer
    Participant

    @chrishajer

    What you are asking for is best done with a plugin as described. I’m not sure which core files would need to be modified and I wouldn’t recommend that anyway. What is the opposition to using a plugin?


    811038
    Inactive

    thanks Chrishajer:

    the opposition is that I’m not really develloper, I’m administrator systems but I like to program my website, but I have to tell you that I’m very happy to have bbpress forum in my web, but I had thought that inserting the ‘polemical’ target in question ) thought my forum will be completly perfect. Anyway…

    Well, thank you to try to help me , I’m very gratefull to know you.

    Thank you very much, I will visit this web frecuently to see more ..

    Sincerelly , thank you sir.

    anyone know if this plugin works with the latest bbpress? (0.9.0.2 )

    I cant seem to get it to work.

    Plugin is activated, but links do not have _blank added into the <a tag.

    any ideas? thank you!!


    TrishaM
    Participant

    @trisham

    I actually have a closely related but slightly different need – I need bbPress to NOT strip out the target if I manually enter it into an <a> tag and, ideally, to also not strip out the rel=”nofollow”

    This plugin does add the rel=”nofollow” to links, but I wouldn’t want that added to my internal links, just external ones. But it is not adding the target=”_blank” and bbPress is still stripping it if I add it manually. Again, I wouldn’t want it added for internal links, just external (we want ALL internal (on-site) links to open in the same window, but want ALL external links to open in a new window).

    The only way I know of to get this granularity of control is to do them manually, but dang it bbPress keeps removing them.

    I agree totally that it’s not good to mess with core files, and I try to avoid it at all costs, but this is a very important issue for us – so if there is no plugin to add this level of control, how can I modify my file(s) to do this? I would happily add a functions.php to my theme and put it there, but am not a good enough coder to write this function myself.

    Any suggestions?


    TrishaM
    Participant

    @trisham

    Bump – I know this may wind up being a dead issue (at least until a future bbPress update), but is anyone aware of any plugin to give greater control over how links are handled?

    I don’t want to dig into the core files, but I need to have internal links handled differently from external links, and need to be able to not have manually entered links (not using the URL button) altered to remove or add anything to the <a>tag…..so I’m hoping a plugin exists that will allow for link customization….


    _ck_
    Participant

    @_ck_

    updated, see below


    TrishaM
    Participant

    @trisham

    Thanks _ck_ – that’s a good suggestion – I’ll play around with that a bit…..maybe I can even find some javascript to get rid of the “rel=”nofollow”” on our internal links, as we do want internal links followed……


    _ck_
    Participant

    @_ck_

    Well we could cheat and make the plugin skip links that have your domain name or no http:// in it.

    function bb_target_blank( $text ) {
    $text = preg_replace('|<a (?=http://)(?!travel-writers-exchange.com)(.+?)>|i', '<a $3 rel="nofollow" target="_blank">', $text);
    return $text;
    }

    This is untested.

    It’s a fancy regex feature called negative lookahead.

    Try it and see what happens.

    Doing nofollow for only externals is essentially the same thing, I’ve added it to the replacement above.Make sure you have no additional tweaks for nofollow. I don’t think it’s on by default? If so, try

    remove_filter('post_text', 'bb_rel_nofollow');
    remove_filter('pre_post', 'bb_rel_nofollow');


    TrishaM
    Participant

    @trisham

    Hmmm…..okay well I did add this code to bb_tweaks……I replaced the function bb_target_blank section with what you have here, and I removed the add_filter('pre-post','bb_rel_nofollow'); and added the remove_filter actions above…….but it did not seem to have any effect – bbPress still strips out any “target” reference and still adds the rel=”nofollow” to all links…..

    I haven’t made any other tweaks to this….


    _ck_
    Participant

    @_ck_

    Okay I’ll play with it some more and see what I can do.

    update: the problem lies within make_clickable which has nofollow hard coded and impossible to “unfilter” at that level

    I’ll have to come up with a way to do it at display time and cleanup the mess bbpress (actually wordpress functions) create


    _ck_
    Participant

    @_ck_

    Okay this one is testing working.

    For anyone else that wants to use this, you have to replace the domain name by hand. It’s hardcoded for speed, sorry.

    All other target=”_blank” plugins should be uninstalled. Any existing links with target=”_blank” will be left in place for performance since target is not added by bbPress by default.

    <?php
    /*
    Plugin Name: Target Nofollow External Only
    Description: append target="_blank" and rel="nofollow" only on external links
    Plugin URI:
    Author: _ck_
    Version: 0.0.1
    */

    add_filter('post_text', 'target_nofollow_external_only',999); // unfortunately we have to do this on every page load and not in pre_post

    function target_nofollow_external_only( $text ) {
    $domain="travel-writers-exchange.com"; // domain to exclude from target and nofollow
    $text = preg_replace('|<a (.*)rel=['"]nofollow['"](.+)?>|iU','<a $1$2>', $text); // strip ALL nofollow
    $text = preg_replace('|<a (?=([^>]+http://))(?!([^>]+'.$domain.'))(.+)>|iU', '<a $3 rel="nofollow" target="_blank">', $text); // add back in when needed
    return $text;
    }
    ?>

    I’m not happy about the performance of this technique because it has to be done in post_text for every time a page is displayed, but there’s no other easy way around bbPress/WordPress’s unfortunate use of make_clickable with hardcoded “nofollow” in post_text.


    TrishaM
    Participant

    @trisham

    This worked perfectly! I deactivated the bb_tweaks plugin so as to not cause any conflict (I think I only installed it for the target=”_blank function) but in theory if I wanted to have it activated as well, I would just need to comment out the function for bb_target_blank, correct?

    Thanks so much for this!

Viewing 25 replies - 1 through 25 (of 49 total)
  • You must be logged in to reply to this topic.