WordPress.org

Support

Support » Plugins and Hacks » Open external links in a new window » Multiple urls in ignore field, not working.

Multiple urls in ignore field, not working.

Viewing 2 replies - 1 through 2 (of 2 total)
  • I would also like this system to be introduced, tried the comma thing but that didn’t work.

    I would like to add all .pdf, .docs, .ppts etc to open in a new window/tab, but I can only add a single specification.

    Temporary solution was to specify all links containing ‘/wp-content/uploads/’ to open a blank target, but that would also include every file in that directory, I’ve excluded jpgs, but then I’m also missing out on excluding audio, video, jpegs, gifs, png etc.

    So a way you can enter multiple Includes and Excludes would be a great enhancement, otherwise this is on the right track and very useful plugin to avoid having to trawl through 100+ page WP and add _blank manually to the links. Thanks!

    This is certainly a pain on this plugin. I think the programmer abonded the ship just a bit too early here. But I have modified the code and the following code is working for multi URL’s. Hopefully the programmer will modify this, otherwise on an update you might have to do it again.

    File: open-external-links-in-a-new-window.php

    If you want to add multiple URL’s to force or ignore, separate them by a semicolon(;). The modified code will split and iterate the URL’s you have entered. For the rest it follows the original code of the maker of this plugin.

    Replace the function external_links_in_new_window_client with this one:

    // Loads the code for the website
    function external_links_in_new_windows_client() {
        echo "\n\n<!-- " . __("Plugin: Open external links a new window.", "open-external-links-in-a-new-window") . " " . __("Plugin by", "open-external-links-in-a-new-window") . " Kristian Risager Larsen, http://kristianrisagerlarsen.dk . " . __("Download it at", "open-external-links-in-a-new-window") . " http://wordpress.org/extend/plugins/open-external-links-in-a-new-window/ -->\n";
    
        $blogdomain = parse_url(get_option('home'));
        echo "<script type=\"text/javascript\">//<![CDATA[";
        echo "
    	function external_links_in_new_windows_loop() {
    		if (!document.links) {
    			document.links = document.getElementsByTagName('a');
    		}
    		var change_link = false;
    		var force = '" . get_option("external_links_in_new_windows_force") . "';
    		var ignore = '" . get_option("external_links_in_new_windows_ignore") . "';
    
                    var force_split = force.split(';');
                    var ignore_split = ignore.split(';');
    
    		for (var t=0; t<document.links.length; t++) {
    			var all_links = document.links[t];
    			change_link = false;
    
    			if(document.links[t].hasAttribute('onClick') == false) {
    				// forced if the address starts with http (or also https), but does not link to the current domain
    				if(all_links.href.search(/^http/) != -1 && all_links.href.search('" . $blogdomain['host'] . "') == -1) {
    					// alert('Changeda '+all_links.href);
    					change_link = true;
    				}
    
                                    var found_force = -1;
                                    for (var i = 0; i < force.length; i++) {
                                        if (all_links.href.search(trim(force_split[i])) != -1) {
                                           found_force = 0;
                                        }
                                    }
    
                                    var found_ignore = -1;
                                    for (var i = 0; i < ignore.length; i++) {
                                        if (all_links.href.search(trim(ignore_split[i])) != -1) {
                                           found_ignore = 0;
                                        }
                                    }
    
    				if(force != '' && found_force != -1) {
    					// forced
    					// alert('force '+all_links.href);
    					change_link = true;
    				}
    
    				if(ignore != '' && found_ignore != -1) {
    					// alert('ignore '+all_links.href);
    					// ignored
    					change_link = false;
    				}
    
    				if(change_link == true) {
    					// alert('Changed '+all_links.href);
    					document.links[t].setAttribute('onClick', 'javascript:window.open(\\''+all_links.href+'\\'); return false;');
    					document.links[t].removeAttribute('target');
    				}
    			}
    		}
    	}
    
    	// Load
    	function external_links_in_new_windows_load(func)
    	{
    		var oldonload = window.onload;
    		if (typeof window.onload != 'function'){
    			window.onload = func;
    		} else {
    			window.onload = function(){
    				oldonload();
    				func();
    			}
    		}
    	}
    
    	external_links_in_new_windows_load(external_links_in_new_windows_loop);
    	";
    
        echo "//]]></script>\n\n";
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Multiple urls in ignore field, not working.’ is closed to new replies.
Skip to toolbar