Skip to content

Instantly share code, notes, and snippets.

@zoerooney
Last active December 17, 2015 10:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zoerooney/5598730 to your computer and use it in GitHub Desktop.
Save zoerooney/5598730 to your computer and use it in GitHub Desktop.
A few different ways to use URL information in WordPress themes, from a mini-tutorial here: http://zoerooney.com/blog/tutorials/using-the-referring-page-in-wordpress/
jQuery(document).ready(function($){
// Read URL and determine if it contains "free"
if(window.location.href.indexOf("free") > -1 ) {
// If it does, filter the list
$.filtrify("patterns-list", "filter", {
close : true, // let's not have any drop downs open
query : {
"cost" : ["free"] // pre-set the filter to only show free patterns
},
callback : writeLegend // and display the legend
});
} else {
// If it doesn't, show the regular full list of patterns, unfiltered
$.filtrify("patterns-list", "filter", {
close : true, // still closed drop downs
callback : writeLegend // still with legend functionality
});
}
$('li.three a').click(function(){
// Force the page to reload when you are on Patterns
// and click "Free Patterns" via the menu
// since usually hash URLs on the same page don't reload
// (There may be a better way to do this, but this works)
window.location.hash = 'free';
window.location.reload();
});
// Most of this part is Filtrify's legend code
// http://luis-almeida.github.io/filtrify/legend.html
function writeLegend ( query, match, mismatch ) {
if ( !mismatch.length ) {
$("#legend").html("");
} else {
var category, tags, i, tag, legend = "<h4>Currently filtered by:</h4>";
for ( category in query ) {
tags = query[category];
if ( tags.length ) {
legend += "";
for ( i = 0; i < tags.length; i++ ) {
tag = tags[i];
legend += "" + tag + "&nbsp;&nbsp;&nbsp;&nbsp;";
};
legend += "";
};
};
$("#legend").html( legend );
};
// I do want to check if the user has filtered by "Free" in the drop down
// I found it easiest to do this using my legend
// This way it's triggered every time the legend changes
if ($('#legend:contains("free")').length) {
// if the legend has "free" in it, add the hash
// and change the page title
location.hash = 'free';
$('.page-title').html('Free Patterns').addClass('free');
} else {
// otherwise, remove the hash
// and change the title back to normal
location.hash = '';
$('.page-title').html('Patterns').removeClass('free');
}
};
});
<div id="back-to-blog">
<?php
// assign the referring page URL to the variable $url
$url = htmlspecialchars($_SERVER['HTTP_REFERER']);
// check if the URL contains /blog/ using a double negative
if (strpos($url,'/blog/') !== false) {
// if it does, print the link to that URL
echo "<a href='$url'>BACK TO THE BLOG</a>";
}
?>
</div>
<?php
// use a WordPress tag http://codex.wordpress.org/Function_Reference/wp_get_referer
// to assign the referring URL to the variable $referer
$referer = wp_get_referer();
// check if the URL is a specific one
if ( $referer == "http://lovetaza.com/once-upon-a-time/" ) {
// if it is, do something
} else {
// if it isn't, do something else
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment