-
-
Save bewho/670641dc7916f2e5c4f1ea59c4b24b82 to your computer and use it in GitHub Desktop.
WordPress functions.php file with various cleanup, security, performance and helper functions. Obviously requires customisation as per theme needs.
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 | |
function im_setup() { | |
// ++++++++++++++++ REGISTER THEME OBJECTS ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // | |
// Register and enqueue Javascript | |
function im_js() { | |
// Modernizr | |
wp_register_script( 'modernizr-script', get_template_directory_uri() . '/_inc/modernizr.min.js', false, '1.0', false ); | |
wp_enqueue_script( 'modernizr-script' ); | |
// jQuery | |
wp_deregister_script( 'jquery' ); | |
wp_register_script('jquery', get_template_directory_uri() . '/_inc/jquery.min.js', false, '2.0.0', true ); | |
wp_enqueue_script( 'jquery' ); | |
// Custom | |
wp_register_script( 'im-script', get_template_directory_uri() . '/_inc/script.min.js', array('jquery'), false, '1.2', true ); | |
wp_enqueue_script( 'im-script' ); | |
} | |
add_action( 'wp_enqueue_scripts', 'im_js' ); | |
// Register menu | |
register_nav_menu( 'primary', 'Primary Menu' ); | |
// Register Feed Links, Post Formats, Custom Thumbnails, HTML5 | |
if ( function_exists( 'add_theme_support' ) ): | |
add_theme_support( 'automatic-feed-links' ); | |
add_theme_support( 'post-formats', array( 'aside', 'image', 'link', 'quote', 'status' ) ); | |
add_theme_support( 'post-thumbnails' ); | |
add_theme_support( 'html5', array( 'comment-list', 'comment-form', 'search-form' ) ); | |
endif; | |
// Register image sizes | |
if ( function_exists( 'add_image_size' ) ): | |
add_image_size( 'large', 800, 600 ); | |
endif; | |
// ++++++++++++++++ PERFORMANCE +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // | |
// Enable GZIP compression | |
if(extension_loaded("zlib") && (ini_get("output_handler") != "ob_gzhandler")) | |
add_action('wp', create_function('', '@ob_end_clean();@ini_set("zlib.output_compression", 1);')); | |
// Remove the version query string from scripts and styles - allows for better caching | |
function im_remove_script_version( $src ){ | |
$parts = explode( '?', $src ); | |
return $parts[0]; | |
} | |
add_filter( 'script_loader_src', 'im_remove_script_version', 15, 1 ); | |
add_filter( 'style_loader_src', 'im_remove_script_version', 15, 1 ); | |
// Prevents WordPress from testing SSL capability on domain.com/xmlrpc.php?rsd when XMLRPC not in use | |
remove_filter('atom_service_url','atom_service_url_filter'); | |
// ++++++++++++++++ SECURITY MODIFICATIONS ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // | |
// Hide login form error messages | |
add_filter('login_errors',create_function('$a', "return 'Error';")); | |
// Remove WordPress version number | |
function im_remove_wp_version() { return ''; } | |
add_filter('the_generator', 'im_remove_wp_version'); | |
// Disable XMLRPC | |
add_filter('xmlrpc_enabled', '__return_false'); | |
// ++++++++++++++++ WP CLEANUP ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // | |
// General Cleanup - removes unnecessary WordPress features | |
remove_action( 'wp_head', 'feed_links_extra', 3 ); | |
remove_action( 'wp_head', 'feed_links', 2 ); | |
remove_action( 'wp_head', 'rsd_link' ); | |
remove_action( 'wp_head', 'wlwmanifest_link' ); | |
remove_action( 'wp_head', 'index_rel_link' ); | |
remove_action( 'wp_head', 'parent_post_rel_link' ); | |
remove_action( 'wp_head', 'start_post_rel_link', 10, 0 ); | |
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); | |
remove_action( 'wp_head', 'wp_generator' ); | |
remove_action( 'wp_head', 'wp_shortlink_wp_head'); | |
// Unregister all default WP Widgets | |
function im_unregister_default_wp_widgets() { | |
unregister_widget('WP_Widget_Pages'); | |
unregister_widget('WP_Widget_Calendar'); | |
unregister_widget('WP_Widget_Archives'); | |
unregister_widget('WP_Widget_Links'); | |
unregister_widget('WP_Widget_Meta'); | |
unregister_widget('WP_Widget_Search'); | |
unregister_widget('WP_Widget_Text'); | |
unregister_widget('WP_Widget_Categories'); | |
unregister_widget('WP_Widget_Recent_Posts'); | |
unregister_widget('WP_Widget_Recent_Comments'); | |
unregister_widget('WP_Widget_RSS'); | |
unregister_widget('WP_Widget_Tag_Cloud'); | |
} | |
add_action('widgets_init', 'im_unregister_default_wp_widgets', 1); | |
// Cleanup Jetpack styles | |
function im_remove_jetpack_styles(){ | |
wp_deregister_style('AtD_style'); // After the Deadline | |
wp_deregister_style('jetpack-carousel'); // Carousel | |
wp_deregister_style('grunion.css'); // Grunion contact form | |
wp_deregister_style('the-neverending-homepage'); // Infinite Scroll | |
wp_deregister_style('infinity-twentyten'); // Infinite Scroll - Twentyten Theme | |
wp_deregister_style('infinity-twentyeleven'); // Infinite Scroll - Twentyeleven Theme | |
wp_deregister_style('infinity-twentytwelve'); // Infinite Scroll - Twentytwelve Theme | |
wp_deregister_style('noticons'); // Notes | |
wp_deregister_style('post-by-email'); // Post by Email | |
wp_deregister_style('publicize'); // Publicize | |
wp_deregister_style('sharedaddy'); // Sharedaddy | |
wp_deregister_style('sharing'); // Sharedaddy Sharing | |
wp_deregister_style('stats_reports_css'); // Stats | |
wp_deregister_style('jetpack-widgets'); // Widgets | |
} | |
add_action('wp_print_styles', 'im_remove_jetpack_styles'); | |
// Remove Jetpark Open Graph tags | |
remove_action( 'wp_head', 'jetpack_og_tags'); | |
// Cleanup Jetpack scripts | |
function im_dequeue_devicepx() { | |
wp_dequeue_script( 'devicepx' ); | |
} | |
add_action( 'wp_enqueue_scripts', 'im_dequeue_devicepx', 20 ); | |
// Remove inline style from Recent Comments widget | |
function im_remove_recent_comments_style() { | |
global $wp_widget_factory; | |
remove_action( 'wp_head', array( $wp_widget_factory->widgets['WP_Widget_Recent_Comments'], 'recent_comments_style' ) ); | |
} | |
add_action( 'widgets_init', 'im_remove_recent_comments_style' ); | |
// Set the maximum number of post revisions unless the constant is already set in wp-config.php | |
if (!defined('WP_POST_REVISIONS')) define('WP_POST_REVISIONS', 5); | |
// Disable Auto-Formatting in Content and Excerpt | |
remove_filter( 'the_content', 'wpautop' ); | |
remove_filter( 'the_excerpt', 'wpautop' ); | |
// Disable Auto Linking of URLs in comments | |
remove_filter('comment_text', 'make_clickable', 9); | |
// Disable self-ping | |
function im_self_ping( &$links ) { | |
$home = get_option( 'home' ); | |
foreach ( $links as $l => $link ) | |
if ( 0 === strpos( $link, $home ) ) | |
unset($links[$l]); | |
} | |
add_action( 'pre_ping', 'im_self_ping' ); | |
// ++++++++++++++++ ADMIN MODIFICATIONS +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // | |
// Add visual editor stylesheet | |
add_editor_style( 'editor-style.css' ); | |
// Change the default WordPress greeting in Admin | |
function im_replace_howdy( $wp_admin_bar ) { | |
$my_account=$wp_admin_bar->get_node('my-account'); | |
$newtitle = str_replace( 'Howdy,', 'Welcome, ', $my_account->title ); | |
$wp_admin_bar->add_node( array( | |
'id' => 'my-account', | |
'title' => $newtitle, | |
) ); | |
} | |
add_filter( 'admin_bar_menu', 'im_replace_howdy', 25 ); | |
// Add new Admin menu item "All Settings" | |
function im_all_settings_link() { | |
add_options_page(__('All Settings'), __('All Settings'), 'administrator', 'options.php'); | |
} | |
add_action('admin_menu', 'im_all_settings_link'); | |
// Remove dashboard menu items | |
function dashboard_tweaks() { | |
global $wp_admin_bar; | |
$wp_admin_bar->remove_menu('wp-logo'); | |
$wp_admin_bar->remove_menu('about'); | |
$wp_admin_bar->remove_menu('wporg'); | |
$wp_admin_bar->remove_menu('documentation'); | |
$wp_admin_bar->remove_menu('support-forums'); | |
$wp_admin_bar->remove_menu('feedback'); | |
$wp_admin_bar->remove_menu('view-site'); | |
} | |
add_action( 'wp_before_admin_bar_render', 'dashboard_tweaks' ); | |
// ++++++++++++++++ LOGIN MODIFICATIONS +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // | |
// Custom login logo | |
function im_custom_login_logo() { | |
echo '<style type="text/css"> | |
.login #login { padding-top: 0;} | |
.login h1 { width: 320px; height: 200px; } | |
.login h1 a { background:url(' . get_template_directory_uri() . '/_img/login.png) 50% 50% no-repeat; background-size:contain; height: 200px; width: 320px; } | |
</style>'; | |
} | |
add_action('login_head', 'im_custom_login_logo'); | |
// Custom login URL | |
function im_custom_login_url(){ | |
return ('http://imaginarymedia.com.au/'); | |
} | |
add_filter('login_headerurl', 'im_custom_login_url'); | |
// Custom login URL Title Attribute | |
function im_custom_login_title(){ | |
return ('Imaginary Media'); | |
} | |
add_filter('login_headertitle', 'im_custom_login_title'); | |
// ++++++++++++++++ CUSTOM POST TYPES +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // | |
// Show Custom Post Types in search results | |
function im_searchAll( $query ) { | |
if ( $query->is_search ) { $query->set( 'post_type', array( 'site', 'plugin', 'theme', 'person' )); } | |
return $query; | |
} | |
add_filter( 'the_search_query', 'im_searchAll' ); | |
// Add Custom Post Types to the default RSS feed | |
function im_custom_feed_request( $vars ) { | |
if (isset($vars['feed']) && !isset($vars['post_type'])) | |
$vars['post_type'] = array( 'post', 'site', 'plugin', 'theme', 'person' ); | |
return $vars; | |
} | |
add_filter( 'request', 'im_custom_feed_request' ); | |
// ++++++++++++++++ CUSTOM USER PROFILE FIELDS ++++++++++++++++++++++++++++++++++++++++++++++++++++ // | |
function im_custom_userfields( $contactmethods ) { | |
$contactmethods['contact_phone'] = 'Contact Number'; | |
$contactmethods['social_fb'] = 'Facebook Profile'; | |
$contactmethods['social_tw'] = 'Twitter Profile'; | |
$contactmethods['social_gp'] = 'Google Plus Profile'; | |
return $contactmethods; | |
} | |
add_filter('user_contactmethods','im_custom_userfields',10,1); | |
// ++++++++++++++++ BASIC HELPER FUNCTIONS ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // | |
// Change the length of the default excerpt (number of words, default is 55) | |
function im_excerpt_length($length) { | |
return 80; | |
} | |
add_filter('excerpt_length', 'im_excerpt_length'); | |
// Disable RSS feeds | |
function im_disable_feed() { | |
wp_die( __('No feed available,please visit our <a href="'. get_bloginfo('url') .'">homepage</a>!') ); | |
} | |
add_action('do_feed', 'im_disable_feed', 1); | |
add_action('do_feed_rdf', 'im_disable_feed', 1); | |
add_action('do_feed_atom', 'im_disable_feed', 1); | |
add_action('do_feed_rss', 'im_disable_feed', 1); | |
add_action('do_feed_rss2', 'im_disable_feed', 1); | |
// Gets the URL of the current page | |
function get_url() { | |
return (isset($_SERVER['HTTPS']) == 'on' ? 'https' : 'http').'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; | |
} | |
// Allow custom MIME types to be uploaded | |
function im_custom_upload_mimes ( $existing_mimes=array() ) { | |
$existing_mimes['vcf'] = 'text/vcard'; | |
return $existing_mimes; | |
} | |
add_filter('upload_mimes', 'im_custom_upload_mimes'); | |
// Get post/page excerpt from ID | |
function get_excerpt_by_id($post_id){ | |
$the_post = get_post($post_id); | |
$the_excerpt = $the_post->post_content; | |
$excerpt_length = 35; | |
$the_excerpt = strip_tags(strip_shortcodes($the_excerpt)); | |
$words = explode(' ', $the_excerpt, $excerpt_length + 1); | |
if(count($words) > $excerpt_length) : | |
array_pop($words); | |
array_push($words, '…'); | |
$the_excerpt = implode(' ', $words); | |
endif; | |
$the_excerpt = '<p>' . $the_excerpt . '</p>'; | |
return $the_excerpt; | |
} | |
// Use HTML5 FIGURE and FIGCAPTION for images and captions | |
function im_cleaner_captions( $output, $attr, $content ) { | |
if ( is_feed() ) | |
return $output; | |
$defaults = array( | |
'id' => '', | |
'align' => 'alignnone', | |
'width' => '', | |
'caption' => '' | |
); | |
$attr = shortcode_atts( $defaults, $attr ); | |
if ( 1 > $attr['width'] || empty( $attr['caption'] ) ) | |
return $content; | |
$attributes = ( !empty( $attr['id'] ) ? ' id="' . esc_attr( $attr['id'] ) . '"' : '' ); | |
$attributes .= ' class="wp-caption ' . esc_attr( $attr['align'] ) . '"'; | |
$output = '<figure' . $attributes .'>'; | |
$output .= do_shortcode( $content ); | |
$output .= '<figcaption class="wp-caption-text"><p>' . $attr['caption'] . '</p></figcaption>'; | |
$output .= '</figure>'; | |
return $output; | |
} | |
add_filter( 'img_caption_shortcode', 'im_cleaner_captions', 10, 3 ); | |
// ++++++++++++++++ COMMENTS ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // | |
// Disable comments on media files | |
function filter_media_comment_status( $open, $post_id ) { | |
$post = get_post( $post_id ); | |
if( $post->post_type == 'attachment' ) { | |
return false; | |
} | |
return $open; | |
} | |
add_filter( 'comments_open', 'filter_media_comment_status', 10 , 2 ); | |
// Close comments globally | |
function im_closeCommentsGlobaly($data) { return false; } | |
add_filter('comments_number', 'im_closeCommentsGlobaly'); | |
add_filter('comments_open', 'im_closeCommentsGlobaly'); | |
// ++++++++++++++++ LOAD THEME OPTIONS ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // | |
// Load theme options | |
require_once ( get_template_directory() . '/_inc/options.php' ); | |
// ====== END ====== // | |
} | |
add_action( 'after_setup_theme', 'im_setup' ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment