mizner
1/3/2014 - 4:01 PM

When selecting a theme via URL in Theme Test Drive (https://wordpress.org/plugins/theme-test-drive/) for WordPress, this script will make it

When selecting a theme via URL in Theme Test Drive (https://wordpress.org/plugins/theme-test-drive/) for WordPress, this script will make it so that all of your links will automatically get that theme name added to the link URL.

<?php if($_GET['theme']) { ?>
<script type="text/javascript">
	// Best to put this in footer.php or any file that will be loaded on all pages
	// create variable containing the name of the theme as shown in URL query string
	var theme_name = "<?php if($_GET['theme']) echo $_GET['theme']; else echo "dev"; ?>";
	
	// Add theme name to all URLs in <a> tags
	jQuery(function() {
		jQuery('a[href]').each(function() {
			var url = jQuery(this).attr('href');
			// If URL doesn't have a query string, add one
			if(url.indexOf('?') == -1) {
				url = url + "?theme="+theme_name;
				jQuery(this).attr('href', url);
			}
			// If theme is not listed in query string, add it
			else if(url.indexOf('theme=') == -1) {
				url = url + "&theme="+theme_name;
				jQuery(this).attr('href', url);
			}
		});
	});
</script>
<?php } ?>