Skip to content

Instantly share code, notes, and snippets.

@dingo-d
Created November 26, 2016 10:33
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 dingo-d/f95e046604315a357da65b0a1d1fc5de to your computer and use it in GitHub Desktop.
Save dingo-d/f95e046604315a357da65b0a1d1fc5de to your computer and use it in GitHub Desktop.
functions.php example for WordPress theme
<?php
/**
* Your theme functions and definitions
*
* Set up the theme and provides some helper functions, which 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 yourtheme
* @version 1.0.0
* @author yourfirm ltd. <http://yourfirm.com/>
* @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3
* @link https://yourtheme.com
* @since 1.0.0
*/
define( 'YOURTHEME_THEME_VERSION', '1.0.0' );
define( 'YOURTHEME_TEMPPATH', get_template_directory_uri() );
define( 'YOURTHEME_IMAGES', YOURTHEME_TEMPPATH . '/images' );
define( 'YOURTHEME_TEMPDIR', get_template_directory() );
add_action( 'after_setup_theme', 'yourtheme_theme_setup' );
if ( ! function_exists( 'yourtheme_theme_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 for post thumbnails.
*
* @since 1.0.0
*/
function yourtheme_theme_setup() {
load_theme_textdomain( 'yourtheme', YOURTHEME_TEMPDIR . '/languages' );
$grid_width = get_theme_mod( 'grid_width', '' );
if ( ! isset( $content_width ) ) {
$content_width = $grid_width;
}
add_theme_support( 'automatic-feed-links' );
add_theme_support( 'title-tag' );
add_theme_support( 'post-thumbnails' );
add_theme_support( 'post-formats', array( 'aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio', 'chat' ) );
add_theme_support( 'html5', array( 'comment-list', 'comment-form', 'search-form', 'gallery', 'caption' ) );
add_post_type_support( 'page', 'excerpt' );
add_filter( 'the_content_more_link', 'yourtheme_remove_more_link_scroll_wrap' );
/********* Menus ***********/
add_action( 'init', 'yourtheme_register_my_menus' );
require_once( YOURTHEME_TEMPDIR . '/inc/menu-walker.php' );
/********* Enqueue Scripts ***********/
add_action( 'wp_enqueue_scripts', 'yourtheme_frontend_scripts' );
add_action( 'admin_enqueue_scripts', 'yourtheme_backend_scripts' );
/********* Register sidebars ***********/
require_once( YOURTHEME_TEMPDIR . '/inc/sidebars.php' );
/********* Breadcrumbs! ***********/
require_once( YOURTHEME_TEMPDIR . '/inc/breadcrumbs.php' );
/********* Additional fields in page and post editor ***********/
require_once( YOURTHEME_TEMPDIR . '/inc/admin/page-additional-fields.php' );
require_once( YOURTHEME_TEMPDIR . '/inc/admin/post-additional-fields.php' );
/********* Additional fields in categories ***********/
require_once( YOURTHEME_TEMPDIR . '/inc/admin/categories-additional-fields.php' );
}
}
/********* Theme Customizer ***********/
require_once( YOURTHEME_TEMPDIR . '/inc/customizer/customizer.php' );
/********* Theme Settings ***********/
require_once( YOURTHEME_TEMPDIR . '/inc/theme-settings/theme-settings.php' );
if ( ! function_exists( 'yourtheme_register_my_menus' ) ) {
/**
* Register menu function
*
* Registers new menu locations based on theme option settings.
*
* @since 1.0.0
*/
function yourtheme_register_my_menus() {
register_nav_menus( array(
'header-menu' => esc_html__( 'Header Menu', 'yourtheme' ),
'footer-menu' => esc_html__( 'Footer Menu', 'yourtheme' ),
) );
}
}
/********* Additional Includes ***********/
if ( in_array( 'woocommerce/woocommerce.php', get_option( 'active_plugins' ), true ) ) {
include_once( get_stylesheet_directory() . '/inc/woocommerce-custom.php' );
}
if ( ! function_exists( 'yourtheme_frontend_scripts' ) ) {
/**
* Enqueue scripts and styles.
*
* @since 1.0.0
*/
function yourtheme_frontend_scripts() {
wp_enqueue_style( 'yourtheme_main_css', get_stylesheet_uri(), array( 'wp-mediaelement' ) );
// Styles from options - appends styles to $custom_css variable.
$yourtheme_settings = get_option( 'yourtheme_settings', '' );
if ( isset( $yourtheme_settings->settings->google_maps_enable ) && '1' === $yourtheme_settings->settings->google_maps_enable ) {
if ( '' !== $yourtheme_settings->settings->google_api_key ) {
wp_enqueue_script( 'google_maps_api', 'https://maps.googleapis.com/maps/api/js?key=' . $yourtheme_settings->settings->google_api_key . '&v=3', '', '', true );
} else {
wp_enqueue_script( 'google_maps_api', '//maps.googleapis.com/maps/api/js?v=3', '', '', true );
}
wp_enqueue_script( 'google_maps_style', YOURTHEME_TEMPPATH . '/js/google-maps-style.js', array( 'jquery' ), '', true );
}
wp_enqueue_script( 'yourtheme_custom', YOURTHEME_TEMPPATH . '/js/custom.js', array( 'jquery', 'wp-mediaelement' ),'', true );
}
}
if ( ! function_exists( 'yourtheme_backend_scripts' ) ) {
/**
* Enqueue bac kend scripts and styles.
*
* @param string $hook Hook string to check the page, so that not all scripts are loaded
* unnecessarily on every page.
* @since 1.0.0
*/
function yourtheme_backend_scripts( $hook ) {
wp_enqueue_style( 'yourtheme_admin_style', YOURTHEME_TEMPPATH . '/css/admin.css' );
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_media();
wp_enqueue_script( 'wp-color-picker' );
wp_enqueue_script( 'yourtheme_category_image_upload', YOURTHEME_TEMPPATH . '/js/admin.js' );
if ( is_singular() ) {
wp_enqueue_script( 'comment-reply' );
}
}
}
if ( ! function_exists( 'yourtheme_fonts_url' ) ) {
/**
* Register Google fonts
*
* @since 1.0.0
* @return string Google fonts URL for the theme.
*/
function yourtheme_fonts_url() {
$fonts_url = '';
$ubuntu = esc_html_x( 'on', 'Ubuntu font: on or off', 'yourtheme' );
$vollkorn = esc_html_x( 'on', 'Vollkorn font: on or off', 'yourtheme' );
$roboto = esc_html_x( 'on', 'Roboto font: on or off', 'yourtheme' );
/*
* Translators: If there are characters in your language that are not supported
* by Ubuntu, Vollkorn or Roboto, translate this to 'off'. Do not translate into your own language.
*/
if ( 'off' !== $ubuntu || 'off' !== $vollkorn || 'off' !== $roboto ) {
$font_families = array();
if ( 'off' !== $ubuntu ) {
$font_families[] = 'Ubuntu:300';
}
if ( 'off' !== $vollkorn ) {
$font_families[] = 'Vollkorn:400italic';
}
if ( 'off' !== $roboto ) {
$font_families[] = 'Roboto Condensed:700';
}
$query_args = array(
'family' => rawurlencode( implode( '|', $font_families ) ),
);
$fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
}
return esc_url_raw( $fonts_url );
}
}
/**
* Sanitization Functions
*
* These functions are used for Customizer sanitization.
*/
if ( ! function_exists( 'yourtheme_allowed_tags' ) ) {
/**
* Allowed tags function for wp_kses()
*
* @return array Array of allowed HTML tags
* @since 1.0.0
*/
function yourtheme_allowed_tags() {
return array(
'a' => array(
'href' => array(),
'title' => array(),
),
'br' => array(),
'span' => array(
'class' => array(),
),
'em' => array(),
'ul' => array(),
'ol' => array(),
'li' => array(),
'strong' => array(),
'pre' => array(),
'code' => array(),
'blockquote' => array(
'cite' => true,
),
'i' => array(
'class' => array(),
),
'cite' => array(
'title' => array(),
),
'abbr' => array(
'title' => true,
),
'select' => array(
'id' => array(),
'name' => array(),
),
'option' => array(
'value' => array(),
),
);
}
}
if ( ! function_exists( 'yourtheme_text_sanitization' ) ) {
/**
* Text sanitization function for Customize API
*
* @param string $input Input to be sanitized.
* @return string Sanitized input.
* @since 1.0.0
*/
function yourtheme_text_sanitization( $input ) {
return wp_kses_post( force_balance_tags( $input ) );
}
}
if ( ! function_exists( 'yourtheme_checkbox_sanitization' ) ) {
/**
* Checkbox sanitization function for Customize API
*
* @param string $input Checkbox value.
* @return integer Sanitized value.
* @since 1.0.0
*/
function yourtheme_checkbox_sanitization( $input ) {
if ( '1' === $input ) {
return 1;
} else {
return '';
}
}
}
if ( ! function_exists( 'yourtheme_sanitize_integer' ) ) {
/**
* Integer sanitization function for Customize API
*
* @param string $input Input value to check.
* @return integer Returned integer value.
* @since 1.0.0
*/
function yourtheme_sanitize_integer( $input ) {
if ( is_numeric( $input ) ) {
return intval( $input );
}
}
}
if ( ! function_exists( 'yourtheme_get_the_widget' ) ) {
/**
* Widget render function - used for showing widgets in the menus
*
* The widgets don't usually appear in the menu, and the only way to display it
* is to save the output of the_widget() function to the buffer and then output it.
*
* @param string $widget The widget's PHP class name.
* @param string $instance The widget's instance settings. Either array or query-style string.
* @param string $args The widget's sidebar args. Either array or query-style string.
* @return string Widget that is saved in the output buffer.
* @since 1.0.0
*/
function yourtheme_get_the_widget( $widget, $instance = '', $args = '' ) {
ob_start();
the_widget( $widget, $instance, $args );
return ob_get_clean();
}
}
if ( ! function_exists( 'yourtheme_get_theme_mod_not_empty' ) ) {
/**
* Get theme modification if it is empty
*
* @param string $option Theme modification name.
* @param [type] $default The default value of theme modification.
* @return [type] Returns the default value if there is nothing set.
* @since 1.0.0
*/
function yourtheme_get_theme_mod_not_empty( $option, $default ) {
$return = get_theme_mod( $option, $default );
if ( '' === $return ) {
$return = $default;
}
return $return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment