Skip to content

Instantly share code, notes, and snippets.

View badcrocodile's full-sized avatar

Jason badcrocodile

  • The Motley Fool
  • Austin, Texas
View GitHub Profile
@badcrocodile
badcrocodile / custom-routes.php
Created June 19, 2015 18:47
Configuring custom routes in wordpress
<?php
/* ========================= */
/* == Configure our routes == */
/* ========================= */
// For month and/or year based archives
function saos_rewrite_tags() {
add_rewrite_tag('%getyear%', '([^&]+)'); // adds year to permalinks
add_rewrite_tag('%getmonth%', '([^&]+)'); // adds month to permalinks
// add_rewrite_tag( '%bf_events_year%', '([0-9]{4})' ); // adds year to single page permalinks
@badcrocodile
badcrocodile / gravity-form-to-email.php
Created June 19, 2015 17:23
Send output of gravity form to email address
<?php
/* -- Gravity Forms send email to me -- */
function set_post_content($entry, $form) {
//Gravity Forms has validated the data
//Our Custom Form Submitted via PHP will go here
// Lets get the IDs of the relevant fields and prepare an email message
$message = print_r($entry, true);
// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70);
// Send
@badcrocodile
badcrocodile / year-month-archive-list.php
Created June 15, 2015 01:01
Generate a year -> month based list of posts in WordPress, similar to the native Archives widget, only with months being children of years as oposed to a flat list
<?php // In functions.php:
/* -- Year and month based archive listings -- */
function posts_by_year() {
// array to use for results
$years = array();
// get posts from WP
$posts = get_posts(array(
'numberposts' => -1,
'orderby' => 'post_date',
@badcrocodile
badcrocodile / is-current-page.php
Last active August 29, 2015 14:23
Compare permalink to current URL
<?php
/* -- Compare the URL to the permalink, return true if matches -- */
function is_current_page($permalink) {
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($url,$permalink) !== false)
return true;
else
return false;
}
?>
@badcrocodile
badcrocodile / custom-slug.php
Last active August 29, 2015 14:23
Custom slugs and query string parameters in WordPress URL's
<?php
/**
* This is an example of a custom loop with pagination and a custom URL structure
* get_year and get_month are our custom query string variables
* EX: http://saos.co/clevelandplus/public/news-press/cle-in-the-national-press/2014/09/page/3/
* where /2014/ and /09/ represent our custom query string parameters
*/
global $wp_query;
if(isset($wp_query->query_vars['getyear'])) {
<?php
// Break down an instance of wp_nav_menu to its simplest components
// source from https://codex.wordpress.org/Function_Reference/wp_get_nav_menu_items
// Get the nav menu based on $menu_name (same as 'theme_location' or 'menu' arg to wp_nav_menu)
// This code based on wp_nav_menu's code to get Menu ID from menu slug
$menu_name = 'custom_menu_slug';
if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
@badcrocodile
badcrocodile / instantiate_walker.php
Created June 12, 2015 14:02
Walk a wordpress menu, changing the href of each menu item to value based on custom field (pdf file upload in this case)
<?php
// To instantiate the walker, pass it to wp_nav_menu
wp_nav_menu(array(
'theme_location' => 'newsletter-archive-menu',
'walker' => new saos_walker_nav_menu()
));
?>
<?php $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'newsletters',
'posts_per_page' => 4,
'paged' => $paged
);
$query = new WP_Query($args);
// hack to get pagination to work on custom post type custom query
$temp_query = $wp_query;
$wp_query = NULL;
@badcrocodile
badcrocodile / wp-menu-accordion.js
Last active August 29, 2015 14:22
Accordion style navigation using default wordpress menus
/**
This function assumes a "fake" menu item as the parent of each sub-menu
*/
function mainMenuSlide() {
$('li.menu-item-has-children .sub-menu').hide(); // hide all submenus
$('li.menu-item-has-children .sub-menu li').each(function() { //
if($(this).hasClass('current_page_item')) { // if current page is a sub-menu page, don't hide the sub-menu
$(this).parent('.sub-menu').show();
}
});

Contract Killer

The popular open-source contract for web designers and developers by Stuff & Nonsense

  • Originally published: 23/12/2008
  • Revised date: 15/12/2013
  • Original post