Skip to content

Instantly share code, notes, and snippets.

@teolopez
Last active December 29, 2015 05:29
Show Gist options
  • Save teolopez/7622458 to your computer and use it in GitHub Desktop.
Save teolopez/7622458 to your computer and use it in GitHub Desktop.
My functions.php file for WordPress Theme with some starter actions/filters. Please remember to Prefix function names, to do so simply replace the word PREFIX in this file with the themes name. Or create a custom prefix. To learn more on how to build a functions.php file head over to Justin Tadlock site and read this article: http://justintadloc…
<?php
// -------------------------------------------------
// Remember to replace the word prefix in the function name and actions/filters to the themes name, use find replace for quick change.
// -------------------------------------------------
add_action( 'after_setup_theme', 'prefix_theme_setup' );
function prefix_theme_setup() {
global $content_width;
/* Set the $content_width for things such as video embeds. */
if ( !isset( $content_width ) )
$content_width = 800;
/* Add theme support for automatic feed links. */
add_theme_support( 'automatic-feed-links' );
/* Add theme support for post thumbnails (featured images). */
add_theme_support( 'post-thumbnails' );
/* Add theme support for custom backgrounds. */
add_theme_support( 'custom-background' );
/*- Actions
-----------------------------------------------------------------------------------*/
/* Add your nav menus function to the 'init' action hook. */
add_action( 'init', 'prefix_register_menus' );
/* Add your sidebars function to the 'widgets_init' action hook. */
add_action( 'widgets_init', 'prefix_register_sidebars' );
/* Load JavaScript files on the 'wp_enqueue_scripts' action hook. */
add_action( 'wp_enqueue_scripts', 'prefix_load_scripts' );
/* Add a Favicon to theme. */
add_action('wp_head', 'prefix_site_favicon');
add_action('admin_init', 'prefix_remove_imagelink', 10);
/*- Filters
-----------------------------------------------------------------------------------*/
/* Remove P tags from images in content. */
add_filter('the_content', 'prefix_filter_ptags_on_images');
}
/*-- ACTIONS - below are actions ------------------------------------------------ */
/* - Add custom menus -*/
function prefix_register_menus() {
/* Register nav menus using register_nav_menu() or register_nav_menus() here. */
register_nav_menus( array(
'primary' => __( 'Primary Navigation', 'primary' ),
//'example' => __( 'Example Navigation', 'wpfme' ),
) );
}
/* - Register Sidebars -*/
function prefix_register_sidebars() {
/* Register dynamic sidebars using register_sidebar() here. */
}
/* - Enqueue Scripts - jQuery, MyScripts, - Proper way to enqueue scripts and styles. -*/
function prefix_load_scripts() {
/*- Scripts ----------------*/
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery' , "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js", false, null);
wp_enqueue_script( 'jquery' );
// My Special Scripts for theme
wp_enqueue_script( 'script-myscripts', get_stylesheet_directory_uri() . '/js/scripts.min.js', array('jquery'), '', true );
/*- Load the comment reply JavaScript. -*/
if ( is_singular() && get_option( 'thread_comments' ) && comments_open() )
wp_enqueue_script( 'comment-reply' );
/*- Style ----------------*/
// wp_enqueue_style( 'script-bootstrap', get_stylesheet_directory_uri() . '/css/bootstrap.min.css', array(), '1.0.0', false );
}
/* - Add a Favicon to your Theme -*/
function prefix_site_favicon() {
echo '<link rel="Shortcut Icon" type="image/x-icon" href="' . get_stylesheet_directory_uri() . '/favicon.ico" />';
}
/* - Removing Default Image Link in WordPress -*/
function prefix_remove_imagelink() {
$image_set = get_option( 'image_default_link_type' );
if ($image_set !== 'none') {
update_option('image_default_link_type', 'none');
}
}
/*-- FILTERS - below are filters ------------------------------------------------ */
/* - Remove P tags from images in content -*/
function prefix_filter_ptags_on_images($content){
return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
}
/* - Set Post Thumbnail Size -*/
set_post_thumbnail_size(200, 200, true); // Normal post thumbnails
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment