Skip to content

Instantly share code, notes, and snippets.

@ihorvorotnov
Forked from gregrickaby/grd_functions.php
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ihorvorotnov/1371338ddc02568b04d4 to your computer and use it in GitHub Desktop.
Save ihorvorotnov/1371338ddc02568b04d4 to your computer and use it in GitHub Desktop.
<?php
/**
* Must-Use Functions
*
* A class filled with functions that will never go away upon theme deactivation.
*
* @package WordPress
* @subpackage GRD
*/
class GRD_Functions {
public function __construct() {
add_action( 'after_setup_theme', array( $this, 'define_constants' ), 1 );
add_action( 'init', array( $this, 'add_post_type' ) );
add_action( 'init', array( $this, 'add_taxonomy' ) );
add_action( 'wp_head', array( $this, 'print_header_scripts' ), 997 );
add_action( 'wp_head', array( $this, 'enqueue_scripts' ), 998 );
add_action( 'wp_footer', array( $this, 'print_footer_scripts' ), 999 );
}
public function define_constants() {
// Path to the child theme directory
$this->maybe_override_contant( 'GRD_DIR', get_stylesheet_directory_uri() );
$GRD_DIR = trailingslashit( GRD_DIR );
// Path to the css directory
$this->maybe_override_contant( 'GRD_CSS', $GRD_DIR . 'css' );
// Path to the images directory
$this->maybe_override_contant( 'GRD_IMG', $GRD_DIR . 'images' );
// Path to the included files directory
$this->maybe_override_contant( 'GRD_INC', $GRD_DIR . 'inc' );
// Path to the javascript directory
$this->maybe_override_contant( 'GRD_JS', $GRD_DIR . 'js' );
// Path to the sass directory
$this->maybe_override_contant( 'GRD_SASS', $GRD_DIR . 'sass' );
// Path to CDN
$this->maybe_override_contant( 'GRD_CDN', 'http://cdn.gregrickaby.com/' );
$GRD_CDN = trailingslashit( GRD_CDN );
// Path to CDN styles
$this->maybe_override_contant( 'GRD_CDN_CSS', $GRD_CDN . 'css' );
// Path to CDN images
$this->maybe_override_contant( 'GRD_CDN_IMG', $GRD_CDN . 'images' );
// Path to CDN scripts
$this->maybe_override_contant( 'GRD_CDN_JS', $GRD_CDN . 'js' );
}
public function maybe_override_contant( $constant, $value ) {
if ( ! defined( $constant ) ) {
define( $constant, $value ); // Constants can be overidden via wp-config.php
}
}
public function enqueue_scripts() {
wp_enqueue_script( 'some-script', GRD_JS . '/some-script.js', null, null, true );
}
public function add_post_type() {
$this->setup_post_type( array( 'Code', 'Code', 'code', 'code' ), array( 'menu_position' => '1' ) );
$this->setup_post_type( array( 'Gif', 'Gifs', 'gifs', 'gifs' ), array( 'menu_position' => '2' ) );
$this->setup_post_type( array( 'Photo', 'Photos', 'photos', 'photos' ), array( 'menu_position' => '3' ) );
$this->setup_post_type( array( 'Recipe', 'Recipes', 'recipes', 'recipies' ), array( 'menu_position' => '4' ) );
}
public function setup_post_type( $type, $args = array() ) {
if ( is_array( $type ) ) {
$types = isset( $type[1] ) ? $type[1] : $type . 's';
$key = isset( $type[2] ) ? $type[2] : strtolower( str_ireplace( ' ', '_', $type[1] ) );
$slug = isset( $type[3] ) ? $type[3] : str_ireplace( '_', '-', $key );
$type = $type[1];
} else {
$types = $type . 's';
$key = strtolower( str_ireplace( ' ', '_', $type ) );
$slug = str_ireplace( '_', '-', $key );
}
$labels = array(
'name' => $type,
'singular_name' => $type,
'add_new' => 'Add New',
'add_new_item' => 'Add New ' . $type,
'edit_item' => 'Edit ' . $type,
'new_item' => 'New ' . $type,
'view_item' => 'View ' . $type,
'search_items' => 'Search ' . $types,
'not_found' => 'No ' . $types . ' found',
'not_found_in_trash' => 'No ' . $types . ' found in Trash',
'parent_item_colon' => '',
'menu_name' => $types
);
$rewrite = array(
'slug' => $slug,
'with_front' => true,
'pages' => true,
'feeds' => true,
);
$args = wp_parse_args( $args, array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => $rewrite,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => '5',
'has_archive' => true,
'exclude_from_search' => false,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'genesis-cpt-archives-settings' ),
'taxonomies' => array()
));
register_post_type( $key, $args );
}
public function add_taxonomy() {
$this->setup_taxonomy( 'Tag', 'Code Tags', 'code', 'code-tags', array( 'code' ) );
$this->setup_taxonomy( 'Tag', 'Gif Tags', 'gifs', 'gif-tags', array( 'gifs' ) );
$this->setup_taxonomy( 'Tag', 'Photo Tags', 'photos', 'photos-tags', array( 'photos' ) );
$this->setup_taxonomy( 'Tag', 'Recipe Tags', 'recipes', 'recipes-tags', array( 'recipes' ) );
}
public function setup_taxonomy( $type, $types, $key, $url_slug, $post_type_keys ) {
$labels = array(
'name' => $types,
'singular_name' => $type,
'search_items' => 'Search ' . $types,
'popular_items' => 'Common ' . $types,
'all_items' => 'All ' . $types,
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => 'Edit ' . $type,
'update_item' => 'Update ' . $type,
'add_new_item' => 'Add New ' . $type,
'new_item_name' => 'New ' . $type . ' Name',
'separate_items_with_commas' => 'Separate ' . $types . ' with commas',
'add_or_remove_items' => 'Add or remove ' . $types,
'choose_from_most_used' => 'Choose from the most used ' . $types
);
$rewrite = array(
'slug' => $url_slug,
'with_front' => true,
'hierarchical' => true,
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'query_var' => true,
'rewrite' => $rewrite
);
register_taxonomy( $key, $post_type_keys, $args );
}
public function print_header_scripts() { ?>
<script>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXX-XX']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<?php }
public function print_footer_scripts() { ?>
<script>
// some javascript or jquery here
</script>
<?php }
}
// Instantiate the class
$GRD_Functions = new GRD_Functions();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment