Skip to content

Instantly share code, notes, and snippets.

@the-cc-dev
Created December 17, 2019 02:44
Show Gist options
  • Save the-cc-dev/8fe30ae406ee1389f9152b48ad7842db to your computer and use it in GitHub Desktop.
Save the-cc-dev/8fe30ae406ee1389f9152b48ad7842db to your computer and use it in GitHub Desktop.
snippets for wp themes
<?php
// Setup Theme
function theme_setup() {
/* * * * *
* Enable plugins to manage the document title
* http://codex.wordpress.org/Function_Reference/add_theme_support#Title_Tag
*/
add_theme_support('title-tag');
/* * * * *
* Enable post formats
* http://codex.wordpress.org/Post_Formats
*/
add_theme_support(
'post-formats',
['aside', 'gallery', 'link', 'image', 'quote', 'video', 'audio']
);
/* * * * *
* Enable HTML5 markup support
* http://codex.wordpress.org/Function_Reference/add_theme_support#HTML5
*/
add_theme_support('html5', [
'caption',
'comment-form',
'comment-list',
'gallery',
'search-form',
]);
add_theme_support('featured_images');
/* * * * *
* Register wp_nav_menu() menus
* http://codex.wordpress.org/Function_Reference/register_nav_menus
*/
register_nav_menus([
'primary_navigation' => __('Primary Navigation', THEME_NAME),
'secondary_navigation' => __('Secondary Navigation', THEME_NAME),
'header_navigation' => __('Header Navigation', THEME_NAME),
'rail_navigation' => __('Rail Navigation', THEME_NAME),
'sidebar_navigation' => __('Sidebar Navigation', THEME_NAME),
'footer_navigation' => __('Footer Navigation', THEME_NAME),
]);
/* *
* ACF Options Pages
*/
if (function_exists('acf_add_options_page')) {
acf_add_options_page(array(
'page_title' => 'Site Options',
'menu_title' => 'Site Options',
'menu_slug' => 'site-options',
'capability' => 'edit_posts',
'redirect' => false,
));
}
}
add_action('after_setup_theme', 'theme_setup');
// Register sidebars
function theme_widgets() {
// header sidebar
register_sidebar([
'name' => __('Header Widgets', THEME_NAME),
'id' => 'sidebar-header',
'before_widget' => '<div id="%1$s" class="%2$s widget item">',
'after_widget' => '</div>',
'before_title' => '<h3 class="ui sr-only widget-title header">',
'after_title' => '</h3>',
]);
// rail sidebar
register_sidebar([
'name' => __('Rail Widgets', THEME_NAME),
'id' => 'sidebar-rail',
'before_widget' => '<div id="%1$s" class="%2$s widget item">',
'after_widget' => '</div>',
'before_title' => '<h3 class="ui widget-title header">',
'after_title' => '</h3>',
]);
// footer sidebar
register_sidebar([
'name' => __('Footer Widgets', THEME_NAME),
'id' => 'sidebar-footer',
'before_widget' => '<div id="%1$s" class="%2$s widget item">',
'after_widget' => '</div>',
'before_title' => '<h3 class="ui widget-title header">',
'after_title' => '</h3>',
]);
// page sidebar
register_sidebar([
'name' => __('Sidebar Widgets', THEME_NAME),
'id' => 'sidebar-page',
'before_widget' => '<div id="%1$s" class="%2$s widget item">',
'after_widget' => '</div>',
'before_title' => '<h3 class="ui widget-title header">',
'after_title' => '</h3>',
]);
register_sidebar([
'name' => __('Primary Widget Area', THEME_NAME),
'id' => 'sidebar-primary',
'before_widget' => '<div id="%1$s" class="widget %2$s item">',
'after_widget' => '</div>',
'before_title' => '<h3 class="ui widget-title header">',
'after_title' => '</h3>',
]);
register_sidebar([
'name' => __('Secondary Widget Area', THEME_NAME),
'id' => 'sidebar-secondary',
'before_widget' => '<div id="%1$s" class="widget %2$s item">',
'after_widget' => '</div>',
'before_title' => '<h3 class="ui widget-title header">',
'after_title' => '</h3>',
]);
}
add_action('widgets_init', 'theme_widgets');
// Add to <head></head>
function theme_wp_head() {
?>
<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="<?php bloginfo('charset'); ?>" />
<style type="text/css">
.acf-field #wp-content-editor-tools {
background: transparent;
padding-top: 0;
}
</style>
<?php
}
add_action('wp_head', 'theme_wp_head');
// Theme assets
function theme_public_assets() {
wp_deregister_script("jquery");
wp_register_script("jquery", "https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js");
wp_enqueue_script( 'ui-scripts', "https://cdn.jsdelivr.net/npm/[email protected]/dist/semantic.min.js", ['jquery']);
wp_enqueue_script( 'theme-scripts', THEME_URI . '/js/theme-scripts.js', ['ui-scripts'] );
wp_enqueue_style( 'ui-styles', THEME_URI . '/css/semantic.css' );
wp_enqueue_style( 'theme-styles', THEME_URI . '/css/theme-styles.css', ['ui-styles'] );
}
add_action('wp_enqueue_scripts', 'theme_public_assets');
<?php
// acf/update_value - add listings fields to featured listing fields
function theme_acf_update_value($value, $post_id, $field) {
$title = get_the_title($post_id);
if( empty($value) ) {
if ($key === $title_key) {
$value = $title;
}
}
return $value;
}
add_filter('acf/update_value', 'theme_acf_update_value', 10, 3);
// Body Classes
function theme_body_classes($classes) {
global $post;
if (isset($post)) {
$classes[] = $post->post_type;
$classes[] = $post->post_name;
$classes[] = $post->post_type . '-' . $post->post_name;
}
$classes[] = "automotive";
$classes[] = "theme";
return $classes;
}
add_filter('body_class', 'theme_body_classes');
//add has_children key to menu item args
function theme_nav_menu_objects($sorted_menu_items, $args) {
$parents = array();
foreach ($sorted_menu_items as $key => $obj) {
$parents[] = $obj->menu_item_parent;
}
foreach ($sorted_menu_items as $key => $obj) {
$has_children = in_array($obj->ID, $parents);
$sorted_menu_items[$key]->has_children = ($has_children)
? true
: false;
}
return $sorted_menu_items;
}
add_filter('wp_nav_menu_objects', 'theme_nav_menu_objects', 10, 2);
// wrap theme nav menus with custom container
function theme_nav_menu_items($items, $args) {
return $items;
}
//add_filter('wp_nav_menu_items', 'theme_nav_menu_items');
<!doctype html>
<html <?php language_attributes(); ?>>
<head><?php wp_head(); ?></head>
<body <?php body_class();?>>
<?php theme_body_open(); ?>
<div id="rail" class="ui theme-rail left vertical inverted sidebar rail menu">
<div class="menu">
<?php theme_menu('rail', array()); ?>
</div>
<?php dynamic_sidebar('sidebar-rail'); ?>
</div><!-- /.rail -->
<header class="ui theme-header fixed top fluid borderless menu">
<a class="left site-title item" href="<?php echo site_url(); ?>">
<?php echo bloginfo('name'); ?>
</a><!-- /.site-title -->
<button id="railToggle" class="ui right mobile-only rail-toggle icon button item">
<i class="bars icon"></i>
</button><!-- /.rail-toggle -->
<div class="right header-nav hide-mobile menu">
<?php theme_menu('header', array());?>
</div><!-- /.header-nav -->
</header><!-- /.theme-header -->
<div id="page-container" class="ui pushable pusher theme-page-container fluid container">
<main id="content" class="ui theme-page centered stackable very padded grid">
<div class="ten wide theme-content column">
<?php
get_header();
if ( have_posts() ) :
while ( have_posts() ) : the_post();
the_content();
endwhile;
endif;
get_footer();
<?php
$unique_id = esc_attr(uniqid('search-form-'));
$form_action = esc_url(home_url('/'));
$label_text = __('Search for:', 'consulting');
$input_value = get_search_query();
$input_placeholder = __('Search...', 'consulting');
?>
<form class="ui search form" method="get" action="<?php echo $form_action; ?>">
<label class="sr-only screen-reader-text" for="<?php echo $unique_id; ?>">
<?php echo $label_text; ?>
</label>
<div class="ui mini action input">
<input class="prompt" type="search" name="s" value="<?php echo $input_value; ?>" placeholder="<?php echo $input_placeholder; ?>" />
<button class="ui mini icon button" type="submit">
<i class="search icon"></i>
</button>
</div>
</form>
<?php
// Semantic-UI Nav Menu Walker
class Sui_Walker_Nav_Menu extends Walker_Nav_Menu {
public function start_lvl(&$output, $depth = 0, $args = array()) {
// Default class.
$classes = array('sub menu');
$class_names = join(' ', apply_filters('nav_menu_submenu_css_class', $classes, $args, $depth));
$class_names = $class_names ? ' class="' . esc_attr($class_names) . '"' : '';
$output .= "<div$class_names>";
}
public function end_lvl(&$output, $depth = 0, $args = array()) {
$output .= "</div>";
}
public function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
global $post;
$top_title = get_the_title();
$classes = ($item->title === $top_title) ? array('active') : array();
foreach ($item->classes as $class) {
$classes[] = $class;
}
$classes[] = 'menu-item-' . $item->ID;
$classes[] = 'item';
$args = apply_filters('nav_menu_item_args', $args, $item, $depth);
$class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item, $args, $depth));
$id = apply_filters('nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args, $depth);
$atts = array();
$atts['id'] = esc_attr($id);
$atts['class'] = esc_attr($class_names);
$atts['title'] = !empty($item->attr_title) ? $item->attr_title : '';
$atts['target'] = !empty($item->target) ? $item->target : '';
if ('_blank' === $item->target && empty($item->xfn)) {
$atts['rel'] = 'noopener noreferrer';
} else {
$atts['rel'] = $item->xfn;
}
$atts['href'] = !empty($item->url) ? $item->url : '';
$atts['aria-current'] = $item->current ? 'page' : '';
$atts = apply_filters('nav_menu_link_attributes', $atts, $item, $args, $depth);
$attributes = '';
foreach ($atts as $attr => $value) {
if (!empty($value)) {
$value = ('href' === $attr) ? esc_url($value) : esc_attr($value);
$attributes .= ' ' . $attr . '="' . $value . '"';
}
}
$title = apply_filters('the_title', $item->title, $item->ID);
$title = apply_filters('nav_menu_item_title', $title, $item, $args, $depth);
$item_output = $args->before;
$item_output .= '<a' . $attributes . '>';
$item_output .= $args->link_before . $title . $args->link_after;
$item_output .= '</a>';
$item_output .= $args->after;
$output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
}
public function end_el(&$output, $item, $depth = 0, $args = array()) {
$output .= "";
}
}
<?php
// Sidebar
function sidebar_cb($atts) {
$a = shortcode_atts(array(
'sb' => 'sidebar-page',
), $atts);
$sb_slug = $a['sb'];
ob_start();
dynamic_sidebar($sb_slug);
return ob_get_clean();
}
add_shortcode('dynamic_sidebar', 'sidebar_cb');
// Hidden Divider
function hidden_divider_cb($atts) {
$a = shortcode_atts([
'classes' => ''
], $atts);
$class = 'ui hidden ';
$class .= $a['classes'] ? $a['classes'].' ' : '';
$class .= 'divider';
ob_start();
?>
<div class="<?php echo $class; ?>"></div><!-- /.hidden.divider -->";
<?php return ob_get_clean();
}
add_shortcode('hidden_divider', 'hidden_divider_cb');
/*
Theme Name:
Theme URI: http://www.github.com/the-cc-dev/
Description:
Version: 1.0.0
Author: cat
Author URI: http://www.github.com/the-cc-dev
Text Domain:
License: MIT License
License URI: http://opensource.org/licenses/MIT
*/
<?php
// acf option field template tags
if( function_exists('get_field') ) {
if( ! function_exists('get_opfield') ) {
function get_opfield($field) {
return get_field($field, 'options');
}
}
if( ! function_exists('the_opfield') ) {
function the_opfield($field) {
echo get_opfield($field);
}
}
}
// template tag to display menus with the sui nav menu walker
if( ! function_exists('theme_menu') && class_exists('Sui_Walker_Nav_Menu') ) {
function theme_menu($location = 'primary', $args = array()) {
$defaults = array(
'container' => '',
'walker' => new Sui_Walker_Nav_Menu(),
'theme_location' => $location . '_navigation',
'items_wrap' => '%3$s',
);
$args = wp_parse_args($args, $defaults);
wp_nav_menu($args);
}
}
// template tag to utilize wp_body_open functi2on/action
if( ! function_exists('theme_body_open') ) {
function theme_body_open() {
if( function_exists('wp_body_open') ) {
wp_body_open();
} else {
do_action('wp_body_open');
}
}
}
// template tag to conditionally do something based on page's title
if( ! function_exists('page_is') ) {
function page_is($page_name = 'Home') {
if(get_the_title() === $page_name) {
return true;
} else {
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment