Set external buy now links to open in new tab

Hey

I'd like to set marketpress so that all the buy now links for external products (e.g. ones on amazon) open in a new tab (target="_blank").

I don't want any normal products sold directly on the site to be affected, just the ones that go to an external link.

Could you direct me to where i should edit the plugin code to make this happen?

Thanks

  • Arun Basil Lal
    • New Recruit

    Hey there,

    You can do this by adding the following into your functions.php:

    add_action('wp_head', 'external_links_in_new_windows_client');
    function external_links_in_new_windows_client()
    {
    
      $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")."';
    
    		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;
    				}
    
    				if(force != '' && all_links.href.search(force) != -1) {
    					// forced
    					// alert('force '+all_links.href);
    					change_link = true;
    				}
    
    				if(ignore != '' && all_links.href.search(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";
    }

    Extracted from: http://wordpress.org/extend/plugins/open-external-links-in-a-new-window/

    I have it here as well: http://pastebin.com/6XhLAgJF

    Good luck!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.