Skip to content

Instantly share code, notes, and snippets.

@bewho
Created September 1, 2018 07:12
Show Gist options
  • Save bewho/b7c8b3b758107a2d5f9101dc3c57df95 to your computer and use it in GitHub Desktop.
Save bewho/b7c8b3b758107a2d5f9101dc3c57df95 to your computer and use it in GitHub Desktop.
Wordpress child theme functions boiler template
<?php
if ( ! defined( 'ABSPATH' ) ) exit;
/*-----------------------------------------------------------------------------------
Title: Child theme functions.php
Description: Functies/Action/filter/hooks used for Wordpress childtheme
Author: Auke Jongbloed
Author Uri: www.wordpressassist.nl
Version: 1.4.0
Tags: Wordpress, wordpressassist, functions.php
TABLE OF CONTENTS
- Enqueue the parent and child theme stylesheets
- Redirect all RSS feeds to FeedBurner
- 15-minute publishing buffer for RSS feeds
- Add RSS links to <head> section
- Remove admin bar
- Add post thumbnail support
- Add page excerpt support
- Load jQuery
- Clean up the <head>
- Enable sidebar widgets
- Enable custom navigation menus
- Create the home (homepage) option in custom navigation menus
- Enable Post Format support
- Add class to post excerpts
- Wrap all post images in figure element
- Remove Date TimeStamp from Articles in Google SERP
- Set Yoast SEO robots meta tags
- remove the yoast canonical tag on archive, search and 404 pages
- Filter function used to remove the tinymce emoji plugin.
- Remove WP embed script
- Remove Query Strings
- Page specific dequeue scripts
-----------------------------------------------------------------------------------*/
// Enqueue the parent and child theme stylesheets
function my_theme_enqueue_styles() {
$parent_style = 'parent-style';
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'child-style',
get_stylesheet_directory_uri() . '/style.css',
array( $parent_style )
);
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
/**
* Translations can be filed in the /languages/ directory
*
*/
load_theme_textdomain( 'html5reset', TEMPLATEPATH . '/languages' );
$locale = get_locale();
$locale_file = TEMPLATEPATH . "/languages/$locale.php";
if ( is_readable($locale_file) )
require_once($locale_file);
/**
* Redirect all RSS feeds to FeedBurner
*
*/
function custom_feed_link($output, $feed) {
$feed_url = 'http://feeds.feedburner.com/statesupply';
$feed_array = array(
'rss' => $feed_url,
'rss2' => $feed_url,
'atom' => $feed_url,
'rdf' => $feed_url,
'comments_rss2' => ''
);
$feed_array[$feed] = $feed_url;
$output = $feed_array[$feed];
return $output;
}
add_filter('feed_link','custom_feed_link', 1, 2);
/**
*
*
*/
function other_feed_links($link) {
$link = 'http://feeds.feedburner.com/statesupply';
return $link;
}
add_filter('category_feed_link', 'other_feed_links');
add_filter('author_feed_link', 'other_feed_links');
/**
* 15-minute publishing buffer for RSS feeds
*
*/
function publish_later_on_feed($where) {
global $wpdb;
if (is_feed()) {
// timestamp in WP-format
$now = gmdate('Y-m-d H:i:s');
// value for wait; + device
$wait = '15'; // integer
$device = 'MINUTE'; // MINUTE, HOUR, DAY, WEEK, MONTH, YEAR
$where .= " AND TIMESTAMPDIFF($device, $wpdb->posts.post_date_gmt, '$now') > $wait ";
}
return $where;
}
add_filter('posts_where', 'publish_later_on_feed');
/**
* Add RSS links to <head> section
*
*/
automatic_feed_links();
/**
* Remove admin bar
*
*/
show_admin_bar(false);
/**
* Add post thumbnail support
*
*/
add_theme_support('post-thumbnails');
add_image_size('large-thumbnail', 246, 140);
/**
* Add page excerpt support
*
*/
add_post_type_support('page', 'excerpt');
/**
* Load jQuery
*
*/
if ( !function_exists(core_mods) ) {
function core_mods() {
if ( !is_admin() ) {
wp_deregister_script('jquery');
wp_register_script('jquery', ("https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"), false);
wp_enqueue_script('jquery');
}
}
core_mods();
}
/**
* Clean up the <head>
*
*/
function removeHeadLinks() {
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link');
}
add_action('init', 'removeHeadLinks');
remove_action('wp_head', 'wp_generator');
/**
* Enable sidebar widgets
*
*/
if (function_exists('register_sidebar')) {
register_sidebar(array(
'name' => 'Post Sidebar',
'description' => 'Place widgets here that you would like to display on the sidebar of the single post page.',
'before_widget' => '<div class="widget">',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>',
));
}
/**
* Enable custom navigation menus
*
*/
if (function_exists('register_nav_menus')) {
register_nav_menus(array(
'main_nav' => 'Main Navigation Menu'
));
}
/**
* Create the home (homepage) option in custom navigation menus
*
*/
function home_page_menu_item($args) {
$args['show_home'] = true;
return $args;
}
add_filter('wp_page_menu_args', 'home_page_menu_item');
/**
* Enable Post Format support
*
*/
add_theme_support('post-formats', array('aside', 'gallery', 'link', 'image', 'quote', 'status', 'audio', 'chat', 'video')); // Add 3.1 post format theme support.
/**
* Add class to post excerpts
*
*/
function add_excerpt_class( $excerpt ) {
$excerpt = str_replace( "<p", "<p class=\"excerpt\"", $excerpt );
return $excerpt;
}
add_filter( "the_excerpt", "add_excerpt_class" );
/**
* Wrap all post images in figure element
*
*/
function wrap_my_div($html, $id, $caption, $title, $align, $url, $size, $alt){
return '<figure class="figure">'.$html.'</figure>';
}
add_filter('image_send_to_editor', 'wrap_my_div', 10, 8);
/**
* Remove Date TimeStamp from Articles in Google SERP
*/
function remove_post_dates() {
add_filter('the_time', '__return_false');
add_filter('get_the_time', '__return_false');
add_filter('the_modified_time', '__return_false');
add_filter('get_the_modified_time', '__return_false');
add_filter('the_date', '__return_false');
add_filter('get_the_date', '__return_false');
add_filter('the_modified_date', '__return_false');
add_filter('get_the_modified_date', '__return_false');
add_filter('get_comment_date', '__return_false');
add_filter('get_comment_time', '__return_false');
}
add_action('loop_start', 'remove_post_dates');
/**
* Set Yoast SEO robots meta tags
* meta robots tag vullen met de juiste instelling per pagina type
* Gepagineerde pagina’s (zonder parameters) moeten index, follow zijn.
* Gepagineerde pagina’s met parameters mogen een noindex, nofollow
*/
function add_noindex_tags( $string= "" ){
global $post;
# Get page number for paginated archives.
$paged = intval( get_query_var( 'paged', false ) );
# Add noindex tag to all archive, search and 404 pages.
if( is_archive() && count($_GET) == 0 ){
// gepagineerde pagina's zonder parameters
$string= "index,follow";
//echo '<meta name="robots" content="index,follow, noodp">';
}else if( count($_GET) >= 1){
// gepagineerde pagina's met parameters || alle pagina's met parameters
$string= "noindex,nofollow";
//echo '<meta name="robots" content="noindex,nofollow">';
}else if( is_search() ){
$string= "noindex,nofollow";
//echo '<meta name="robots" content="noindex,nofollow">';
}else if( is_404() ){
$string= "noindex,nofollow";
//echo '<meta name="robots" content="noindex,nofollow">';
}else if(( is_home() || is_front_page() ) && $paged >= 2 ){
# Add noindex tag to homepage paginated pages.
$string= "noindex,follow";
//echo '<meta name="robots" content="noindex,follow">';
}
return $string;
}
add_filter('wpseo_robots', 'add_noindex_tags', 999);
/**
* remove the yoast canonical tag on archive, search and 404 pages.
* Pagina’s met filter of sorteeropties in de URL moeten geen canonical hebben
* .
*/
add_filter( 'wpseo_canonical', 'wpseo_canonical_exclude' );
function wpseo_canonical_exclude( $canonical ) {
global $post;
if( count($_GET) >= 1){
// gepagineerde pagina's met parameters || alle pagina's met parameters
$canonical = false;
remove_action('wp_head', 'rel_canonical');
}
if( is_search() || is_404() ){
$canonical = false;
remove_action('wp_head', 'rel_canonical');
}
return $canonical;
}
/**
* Disable the emoji's
*/
function disable_emojis() {
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );
remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
add_filter( 'tiny_mce_plugins', 'disable_emojis_tinymce' );
}
add_action( 'init', 'disable_emojis' );
/**
* Filter function used to remove the tinymce emoji plugin.
*
* @param array $plugins
* @return array Difference betwen the two arrays
*/
function disable_emojis_tinymce( $plugins ) {
if ( is_array( $plugins ) ) {
return array_diff( $plugins, array( 'wpemoji' ) );
} else {
return array();
}
}
// Remove WP embed script
function speed_stop_loading_wp_embed() {
if (!is_admin()) {
wp_deregister_script('wp-embed');
}
}
add_action('init', 'speed_stop_loading_wp_embed');
// Remove Query Strings
function _remove_script_version( $src ){
$parts = explode( '?ver', $src );
return $parts[0];
}
add_filter( 'script_loader_src', '_remove_script_version', 15, 1 );
add_filter( 'style_loader_src', '_remove_script_version', 15, 1 );
/*
* Page specific dequeue scripts
* Zorg dat bepaalde pagina specifieke javascripts alleen op de pagina die nodig is worden geladen.
* In dit voorbeeld laad contact form 7 javascripts alleen op de contactpagina.
*/
add_action( 'wp_enqueue_scripts', 'child_themename_dequeue_scripts' );
function page_specific_dequeue_scripts() {
//* Plugins scripts:
// Contact Form 7 scripts on pages without forms
if( !is_page( array( 'contact' ) ) ) {
wp_dequeue_script( 'contact-form-7' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment