File: /home/mmickelson/theflexguy.com/wp-content/themes/simplex/functions.php
<?php
/**
* simpleX functions and definitions
*
* Sets up the theme and provides some helper functions. Some helper functions
* are used in the theme as custom template tags. Others are attached to action and
* filter hooks in WordPress to change core functionality.
*
* @package WordPress
* @subpackage simpleX
* @since simpleX 2.0
*/
/**
* Set the content width based on the theme's design and stylesheet.
*/
if ( ! isset( $content_width ) )
$content_width = 567;
/**
* Tell WordPress to run simplex_setup() when the 'after_setup_theme' hook is run.
*/
add_action( 'after_setup_theme', 'simplex_setup' );
if ( ! function_exists( 'simplex_setup' ) ):
/**
* Sets up theme defaults and registers support for various WordPress features.
*
* Note that this function is hooked into the after_setup_theme hook, which runs
* before the init hook. The init hook is too late for some features, such as indicating
* support post thumbnails.
*
* To override simplex_setup() in a child theme, add your own simplex_setup to your child theme's
* functions.php file.
*
* @uses load_theme_textdomain() For translation/localization support.
* @uses add_editor_style() To style the visual editor.
* @uses add_theme_support() To add support for post thumbnails, automatic feed links, and Post Formats.
* @uses register_nav_menus() To add support for navigation menus.
* @uses add_custom_background() To add support for a custom background.
* @uses add_custom_image_header() To add support for a custom header.
* @uses register_default_headers() To register the default custom header images provided with the theme.
* @uses set_post_thumbnail_size() To set a custom post thumbnail size.
*
* @since simpleX 2.0
*/
function simplex_setup() {
/* Make simpleX available for translation.
* Translations can be added to the /languages/ directory.
* If you're building a theme based on simpleX, use a find and replace
* to change 'simplex' to the name of your theme in all the template files.
*/
load_theme_textdomain( 'simplex', get_template_directory() . '/lib/languages' );
$locale = get_locale();
$locale_file = get_template_directory() . "/lib/languages/$locale.php";
if ( is_readable( $locale_file ) )
require_once( $locale_file );
// This theme styles the visual editor with editor-style.css to match the theme style.
add_editor_style();
// Grab simpleX inc files.
require( get_template_directory() . '/lib/inc/widgets.php' );
require( get_template_directory() . '/lib/inc/body.php' );
require( get_template_directory() . '/lib/inc/posts.php' );
require( get_template_directory() . '/lib/inc/comments.php' );
require( get_template_directory() . '/lib/inc/postnav.php' );
// Add default posts and comments RSS feed links to <head>.
add_theme_support( 'automatic-feed-links' );
// This theme uses wp_nav_menu() in one location.
register_nav_menu( 'primary', __( 'Primary Menu', 'simplex' ) );
// Add support for a variety of post formats
add_theme_support( 'post-formats', array( 'aside', 'link', 'gallery', 'status', 'quote', 'image' ) );
// Add support for custom backgrounds
add_custom_background();
// This theme uses Featured Images (also known as post thumbnails) for per-post/per-page Custom Header images
add_theme_support( 'post-thumbnails' );
// The next four constants set how simpleX supports custom headers.
// The default header text color
define( 'HEADER_TEXTCOLOR', '000' );
// By leaving empty, we allow for random image rotation.
define( 'HEADER_IMAGE', '' );
// The height and width of your custom header.
// Add a filter to simplex_header_image_width and simplex_header_image_height to change these values.
define( 'HEADER_IMAGE_WIDTH', apply_filters( 'simplex_header_image_width', 950 ) );
define( 'HEADER_IMAGE_HEIGHT', apply_filters( 'simplex_header_image_height', 250 ) );
// We'll be using post thumbnails for custom header images on posts and pages.
// We want them to be the size of the header image that we just defined
// Larger images will be auto-cropped to fit, smaller ones will be ignored. See header.php.
set_post_thumbnail_size( HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT, true );
// Add simpleX's custom image sizes
add_image_size( 'large-feature', HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT, true ); // Used for large feature (header) images
add_image_size( 'small-feature', 500, 300 ); // Used for featured posts if a large-feature doesn't exist
// Turn on random header image rotation by default.
add_theme_support( 'custom-header', array( 'random-default' => true ) );
// Add a way for the custom header to be styled in the admin panel that controls
// custom headers. See simplex_admin_header_style(), below.
add_custom_image_header( 'simplex_header_style', 'simplex_admin_header_style', 'simplex_admin_header_image' );
// ... and thus ends the changeable header business.
// Default custom headers packaged with the theme. %s is a placeholder for the theme template directory URI.
//register_default_headers( array() );
}
endif; // simplex_setup
if ( ! function_exists( 'simplex_header_style' ) ) :
/**
* Styles the header image and text displayed on the blog
*/
function simplex_header_style() {
// If no custom options for text are set, let's bail
// get_header_textcolor() options: HEADER_TEXTCOLOR is default, hide text (returns 'blank') or any hex value
if ( HEADER_TEXTCOLOR == get_header_textcolor() )
return;
// If we get this far, we have custom styles. Let's do this.
?>
<style type="text/css">
<?php
// Has the text been hidden?
if ( 'blank' == get_header_textcolor() ) :
?>
#site-title,
#site-description {
position: absolute !important;
clip: rect(1px 1px 1px 1px); /* IE6, IE7 */
clip: rect(1px, 1px, 1px, 1px);
}
<?php
// If the user has set a custom color for the text use that
else :
?>
#site-title a,
#site-description {
color: #<?php echo get_header_textcolor(); ?> !important;
}
<?php endif; ?>
</style>
<?php
}
endif; // simplex_header_style
if ( ! function_exists( 'simplex_admin_header_style' ) ) :
/**
* Styles the header image displayed on the Appearance > Header admin panel.
*
* Referenced via add_custom_image_header() in simplex_setup().
*/
function simplex_admin_header_style() {
?>
<style type="text/css">
.appearance_page_custom-header #headimg {
border: none;
}
#headimg h1,
#desc {
font-family: "Helvetica Neue", Arial, Helvetica, "Nimbus Sans L", sans-serif;
}
#headimg h1 {
margin: 0;
}
#headimg h1 a {
font-size: 32px;
line-height: 36px;
text-decoration: none;
}
#desc {
font-size: 14px;
line-height: 23px;
padding: 0 0 3em;
}
<?php
// If the user has set a custom color for the text use that
if ( get_header_textcolor() != HEADER_TEXTCOLOR ) :
?>
#site-title a,
#site-description {
color: #<?php echo get_header_textcolor(); ?>;
}
<?php endif; ?>
#headimg img {
max-width: 1000px;
height: auto;
width: 100%;
}
</style>
<?php
}
endif; // simplex_admin_header_style
if ( ! function_exists( 'simplex_admin_header_image' ) ) :
/**
* Custom header image markup displayed on the Appearance > Header admin panel.
*
* Referenced via add_custom_image_header() in simplex_setup().
*
*/
function simplex_admin_header_image() { ?>
<div id="headimg">
<?php
if ( 'blank' == get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR ) || '' == get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR ) )
$style = ' style="display:none;"';
else
$style = ' style="color:#' . get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR ) . ';"';
?>
<h1><a id="name"<?php echo $style; ?> onclick="return false;" href="<?php echo esc_url( home_url( '/' ) ); ?>"><?php bloginfo( 'name' ); ?></a></h1>
<div id="desc"<?php echo $style; ?>><?php bloginfo( 'description' ); ?></div>
<?php $header_image = get_header_image();
if ( ! empty( $header_image ) ) : ?>
<img src="<?php echo esc_url( $header_image ); ?>" alt="" />
<?php endif; ?>
</div>
<?php }
endif; // simplex_admin_header_image
/**
* Sets the post excerpt length to 40 words.
*
* To override this length in a child theme, remove the filter and add your own
* function tied to the excerpt_length filter hook.
*/
function simplex_excerpt_length( $length ) {
return 40;
}
add_filter( 'excerpt_length', 'simplex_excerpt_length' );
/**
* Returns a "Continue Reading" link for excerpts
*/
function simplex_continue_reading_link() {
return ' <a href="'. esc_url( get_permalink() ) . '">' . __( 'Continue reading <span class="meta-nav">→</span>', 'simplex' ) . '</a>';
}
/**
* Replaces "[...]" (appended to automatically generated excerpts) with an ellipsis and simplex_continue_reading_link().
*
* To override this in a child theme, remove the filter and add your own
* function tied to the excerpt_more filter hook.
*/
function simplex_auto_excerpt_more( $more ) {
return ' …' . simplex_continue_reading_link();
}
add_filter( 'excerpt_more', 'simplex_auto_excerpt_more' );
/**
* Adds a pretty "Continue Reading" link to custom post excerpts.
*
* To override this link in a child theme, remove the filter and add your own
* function tied to the get_the_excerpt filter hook.
*/
function simplex_custom_excerpt_more( $output ) {
if ( has_excerpt() && ! is_attachment() ) {
$output .= simplex_continue_reading_link();
}
return $output;
}
add_filter( 'get_the_excerpt', 'simplex_custom_excerpt_more' );
/**
* Get our wp_nav_menu() fallback, wp_page_menu(), to show a home link.
*/
function simplex_page_menu_args( $args ) {
$args['show_home'] = true;
return $args;
}
add_filter( 'wp_page_menu_args', 'simplex_page_menu_args' );
/**
* Return the URL for the first link found in the post content.
*
* @return string|bool URL or false when no link is present.
*/
function simplex_url_grabber() {
if ( ! preg_match( '/<a\s[^>]*?href=[\'"](.+?)[\'"]/is', get_the_content(), $matches ) )
return false;
return esc_url_raw( $matches[1] );
}
/**
* Count the number of footer sidebars to enable dynamic classes for the footer
*/
function simplex_footer_sidebar_class() {
$count = 0;
if ( is_active_sidebar( 'sidebar-3' ) )
$count++;
if ( is_active_sidebar( 'sidebar-4' ) )
$count++;
if ( is_active_sidebar( 'sidebar-5' ) )
$count++;
$class = '';
switch ( $count ) {
case '1':
$class = 'one';
break;
case '2':
$class = 'two';
break;
case '3':
$class = 'three';
break;
}
if ( $class )
echo 'class="' . $class . '"';
}
// Filter wp_nav_menu() to add home link
function simplex_nav_menu_items($items) {
$homelink = '<li class="home';
if(is_home()) { $homelink .= " current-menu-item"; }
$homelink .= '"><a href="' . home_url( '/' ) . '">' . __('Home','simplex') . '</a></li>';
$items = $homelink . $items;
return $items;
}
add_filter( 'wp_nav_menu_items', 'simplex_nav_menu_items' );
if(!is_admin()) { add_action('wp_enqueue_scripts','simplex_enqueue_scripts'); }
// Wrap all required scripts in one function for easier enqueue
function simplex_enqueue_scripts() {
wp_enqueue_script( 'jquery' );
wp_register_script( 'fitvidsjs', get_template_directory_uri().'/lib/js/jquery.fitvids.js', array( 'jquery' ));
wp_enqueue_script( 'fitvidsjs' );
}
// Add DOM ready scripts
add_action('wp_head', 'simplex_load_doc_js');
function simplex_load_doc_js() {
$doc_ready_script = '
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery("#page").fitVids();
});
</script>';
echo $doc_ready_script;
}
//replace default gallery shortcode
remove_shortcode('gallery', 'gallery_shortcode');
add_shortcode('gallery', 'simplex_gallery_shortcode');
function simplex_gallery_shortcode($attr) {
global $post;
static $instance = 0;
$instance++;
// Allow plugins/themes to override the default gallery template.
$output = apply_filters('post_gallery', '', $attr);
if ( $output != '' )
return $output;
// We're trusting author input, so let's at least make sure it looks like a valid orderby statement
if ( isset( $attr['orderby'] ) ) {
$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
if ( !$attr['orderby'] )
unset( $attr['orderby'] );
}
extract(shortcode_atts(array(
'order' => 'ASC',
'orderby' => 'menu_order ID',
'id' => $post->ID,
'itemtag' => 'dl',
'icontag' => 'dt',
'captiontag' => 'dd',
'columns' => 3,
'size' => 'thumbnail',
'include' => '',
'exclude' => ''
), $attr));
$id = intval($id);
if ( 'RAND' == $order )
$orderby = 'none';
if ( !empty($include) ) {
$include = preg_replace( '/[^0-9,]+/', '', $include );
$_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
$attachments = array();
foreach ( $_attachments as $key => $val ) {
$attachments[$val->ID] = $_attachments[$key];
}
} elseif ( !empty($exclude) ) {
$exclude = preg_replace( '/[^0-9,]+/', '', $exclude );
$attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
} else {
$attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
}
if ( empty($attachments) )
return '';
if ( is_feed() ) {
$output = "\n";
foreach ( $attachments as $att_id => $attachment )
$output .= wp_get_attachment_link($att_id, $size, true) . "\n";
return $output;
}
$itemtag = tag_escape($itemtag);
$captiontag = tag_escape($captiontag);
$columns = intval($columns);
$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
$float = is_rtl() ? 'right' : 'left';
$selector = "gallery-{$instance}";
$gallery_style = $gallery_div = '';
$size_class = sanitize_html_class( $size );
$gallery_div = "<div id='$selector' class='gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>";
$output = apply_filters( 'gallery_style', $gallery_style . "\n\t\t" . $gallery_div );
$i = 0;
foreach ( $attachments as $id => $attachment ) {
$link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
if ( $columns > 0 && ++$i % $columns == ($columns - 1) )
$last_class = " last";
else
$last_class = "";
$output .= "<{$itemtag} class='gallery-item{$last_class}'>";
$output .= "
<{$icontag} class='gallery-icon'>
$link
</{$icontag}>";
if ( $captiontag && trim($attachment->post_excerpt) ) {
$output .= "
<{$captiontag} class='wp-caption-text gallery-caption'>
" . wptexturize($attachment->post_excerpt) . "
</{$captiontag}>";
}
$output .= "</{$itemtag}>";
if ( $columns > 0 && ++$i % $columns == 0 )
$output .= '<br style="clear: both" />';
}
$output .= "
<br style='clear: both;' />
</div>\n";
return $output;
}
remove_shortcode('wp_caption', 'img_caption_shortcode');
add_shortcode('wp_caption', 'simplex_img_caption_shortcode');
/**
* Replace the Caption shortcode.
*
*/
function simplex_img_caption_shortcode($attr, $content = null) {
// Allow plugins/themes to override the default caption template.
$output = apply_filters('img_caption_shortcode', '', $attr, $content);
if ( $output != '' )
return $output;
extract(shortcode_atts(array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'caption' => ''
), $attr));
if ( 1 > (int) $width || empty($caption) )
return $content;
if ( $id ) $id = 'id="' . esc_attr($id) . '" ';
return '<div ' . $id . 'class="wp-caption ' . esc_attr($align) . '" style="width: ' . ((int) $width) . 'px">'
. do_shortcode( $content ) . '<p class="wp-caption-text">' . $caption . '</p></div>';
}