Created
December 17, 2019 02:44
-
-
Save the-cc-dev/8fe30ae406ee1389f9152b48ad7842db to your computer and use it in GitHub Desktop.
snippets for wp themes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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"> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
get_header(); | |
if ( have_posts() ) : | |
while ( have_posts() ) : the_post(); | |
the_content(); | |
endwhile; | |
endif; | |
get_footer(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment