Updated 2/12/16 - Add "Remove WordPress Admin Bar
- Hide WordPress Update Nag to All But Admins
- Create Proper WordPress Titles
- Create Custom WordPress Dashboard Widget
- Remove All Dashboard Widgets
- Insert Custom Login Logo
- Modify Admin Footer Text
- Enqueue Styles and Scripts
- Enqueue Google Fonts
- Modify Excerpt Length
- Change Read More Link
- Change More Excerpt
- Disable Emoji Mess
- Remove Comments
- Change Media Gallery URL
- Create Custom Thumbnail Size
- Add Categories for Attachments
- Add Tags for Attachments
- Add Custom Excerpt to Pages
- Create a Global String
- Support Featured Images
- [Show Thumbnails on Recent Posts Widget] (#thumbnails-recent-posts)
- Support Search Form
- Excluding pages from search
- Disable XMLRPC
- Escape HTML in Posts
- Create Custom Global Settings
- Remove WordPress Admin Bar
// Hide WordPress Update Nag to All But Admins
function hide_update_notice_to_all_but_admin() {
if (!current_user_can('update_core')) {
remove_action( 'admin_notices', 'update_nag', 3 );
}
}
add_action( 'admin_head', 'hide_update_notice_to_all_but_admin', 1 );
Update: As of WP 4.1, the long version is no longer required - simply add the following to functions.php and remove the <title>
tag from your header.
add_theme_support( 'title-tag' );
Here is the old version, in case you're on an older version of WP for some reason.
// WordPress Title
function wordpress_title( $title, $sep ) {
global $paged, $page;
if ( is_feed() ) {
return $title;
}
// Add the site name.
$title .= get_bloginfo( 'name' );
// Add the site description for the home/front page.
$site_description = get_bloginfo( 'description', 'display' );
if ( $site_description && ( is_home() || is_front_page() ) ) {
$title = "$title $sep $site_description";
}
return $title;
}
add_filter( 'wp_title', 'wordpress_title', 10, 2 );
Add to header.php
<title><?php wp_title( '|', true, 'right' ); ?></title>
// Create Custom WordPress Dashboard Widget
function dashboard_widget_function() {
echo '
<h2>Custom Dashboard Widget</h2>
<p>Custom content here</p>
';
}
function add_dashboard_widgets() {
wp_add_dashboard_widget('custom_dashboard_widget', 'Custom Dashoard Widget', 'dashboard_widget_function');
}
add_action( 'wp_dashboard_setup', 'add_dashboard_widgets' );
// Remove All Dashboard Widgets
function remove_dashboard_widgets() {
global $wp_meta_boxes;
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']);
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_drafts']);
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']);
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
remove_meta_box( 'dashboard_activity', 'dashboard', 'normal' );
}
add_action('wp_dashboard_setup', 'remove_dashboard_widgets');
// Insert Custom Login Logo
function custom_login_logo() {
echo '
<style>
.login h1 a { background-image:url(IMAGE)!important;background-size: 234px 67px;width:234px;height:67px;display:block; }
</style>
';
}
add_action( 'login_head', 'custom_login_logo' );
// Modify Admin Footer Text
function modify_footer() {
echo 'Created by <a href="mailto:you@example.com">you</a>.';
}
add_filter('admin_footer_text', 'modify_footer ');
// Enqueue Styles and Scripts
function custom_scripts() {
wp_enqueue_style( 'bootstrap', get_template_directory_uri() . '/css/bootstrap.min.css', array(), '3.3.6' );
wp_enqueue_style( 'style', get_template_directory_uri() . '/css/style.css' );
wp_enqueue_script( 'bootstrap', get_template_directory_uri() . '/js/bootstrap.min.js', array('jquery'), '3.3.6', true );
wp_enqueue_script( 'script', get_template_directory_uri() . '/js/script.js' );
}
add_action( 'wp_enqueue_scripts', 'custom_scripts' );
// Enqueue Google Fonts
function google_fonts() {
wp_register_style('OpenSans', 'http://fonts.googleapis.com/css?family=Open+Sans:400,600,700,800');
wp_enqueue_style( 'OpenSans');
}
add_action( 'wp_print_styles', 'google_fonts' );
// Modify Excerpt Length
function custom_excerpt_length( $length ) {
return 25;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
// Change Read More Link
function custom_read_more_link() {
return '<a href="' . get_permalink() . '">Read More</a>';
}
add_filter( 'the_content_more_link', 'custom_read_more_link' );
// Change More Excerpt
function custom_more_excerpt( $more ) {
return '...';
}
add_filter('excerpt_more', 'custom_more_excerpt');
// Disable Emoji Mess
function disable_wp_emojicons() {
remove_action( 'admin_print_styles', 'print_emoji_styles' );
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_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
add_filter( 'tiny_mce_plugins', 'disable_emojicons_tinymce' );
}
add_action( 'init', 'disable_wp_emojicons' );
function disable_emojicons_tinymce( $plugins ) {
if ( is_array( $plugins ) ) {
return array_diff( $plugins, array( 'wpemoji' ) );
} else {
return array();
}
}
// Removes from admin menu
add_action( 'admin_menu', 'my_remove_admin_menus' );
function my_remove_admin_menus() {
remove_menu_page( 'edit-comments.php' );
}
// Removes from post and pages
add_action('init', 'remove_comment_support', 100);
function remove_comment_support() {
remove_post_type_support( 'post', 'comments' );
remove_post_type_support( 'page', 'comments' );
}
// Removes from admin bar
function mytheme_admin_bar_render() {
global $wp_admin_bar;
$wp_admin_bar->remove_menu('comments');
}
add_action( 'wp_before_admin_bar_render', 'mytheme_admin_bar_render' );
// Change Media Gallery URL
update_option( 'upload_url_path', 'http://s3.website.com/wp-content/uploads' );
// Add 250 x 250 Custom Thumbnail Size
add_image_size( 'custom-thumbnail', 250, 250, true );
Retrieve Thumbnail
<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'custom-thumbnail' );
echo $thumb[0]; ?>
// Add Categories for Attachments
function add_categories_for_attachments() {
register_taxonomy_for_object_type( 'category', 'attachment' );
}
add_action( 'init' , 'add_categories_for_attachments' );
// Add Tags for Attachments
function add_tags_for_attachments() {
register_taxonomy_for_object_type( 'post_tag', 'attachment' );
}
add_action( 'init' , 'add_tags_for_attachments' );
// Add Custom Excerpt to Pages
function add_page_excerpt() {
add_post_type_support('page', array('excerpt'));
}
add_action('init', 'add_page_excerpt');
// Create a Global String
function global_string(){
return 'String';
}
Retrieve Field
<?php echo global_string(); ?>
// Support Featured Images
add_theme_support( 'post-thumbnails' );
###Feature Image Thumbnails for Recent Posts
/**
* Extend Recent Posts Widget
*
* Adds different formatting to the default WordPress Recent Posts Widget
*/
Class My_Recent_Posts_Widget extends WP_Widget_Recent_Posts {
function widget($args, $instance) {
if ( ! isset( $args['widget_id'] ) ) {
$args['widget_id'] = $this->id;
}
$title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Posts' );
/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
$number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
if ( ! $number )
$number = 5;
$show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;
/**
* Filter the arguments for the Recent Posts widget.
*
* @since 3.4.0
*
* @see WP_Query::get_posts()
*
* @param array $args An array of arguments used to retrieve the recent posts.
*/
$r = new WP_Query( apply_filters( 'widget_posts_args', array(
'posts_per_page' => $number,
'no_found_rows' => true,
'post_status' => 'publish',
'ignore_sticky_posts' => true
) ) );
if ($r->have_posts()) :
?>
<?php echo $args['before_widget']; ?>
<?php if ( $title ) {
echo $args['before_title'] . $title . $args['after_title'];
} ?>
<ul>
<?php while ( $r->have_posts() ) : $r->the_post(); ?>
<li>
<?php the_post_thumbnail(); ?>
<a href="<?php the_permalink(); ?>"><?php get_the_title() ? the_title() : the_ID(); ?></a>
<?php if ( $show_date ) : ?>
<span class="post-date"><?php echo get_the_date(); ?></span>
<?php endif; ?>
</li>
<?php endwhile; ?>
</ul>
<?php echo $args['after_widget']; ?>
<?php
// Reset the global $the_post as this query will have stomped on it
wp_reset_postdata();
endif;
}
}
function my_recent_widget_registration() {
unregister_widget('WP_Widget_Recent_Posts');
register_widget('My_Recent_Posts_Widget');
}
add_action('widgets_init', 'my_recent_widget_registration');
// Support Search Form
add_theme_support( 'html5', array( 'search-form' ) );
// Excluding pages from search
function exclude_pages_from_search() {
global $wp_post_types;
$wp_post_types['page']->exclude_from_search = true;
}
add_action('init', 'exclude_pages_from_search');
// Disable XML RPC
add_filter('xmlrpc_enabled', '__return_false');
remove_action( 'wp_head', 'rsd_link' );
remove_action( 'wp_head', 'wlwmanifest_link' );
// Escape HTML in <code> or <pre><code> tags.
function escapeHTML($arr) {
if (version_compare(PHP_VERSION, '5.2.3') >= 0) {
$output = htmlspecialchars($arr[2], ENT_NOQUOTES, get_bloginfo('charset'), false);
}
else {
$specialChars = array(
'&' => '&',
'<' => '<',
'>' => '>'
);
// decode already converted data
$data = htmlspecialchars_decode($arr[2]);
// escapse all data inside <pre>
$output = strtr($data, $specialChars);
}
if (! empty($output)) {
return $arr[1] . $output . $arr[3];
} else {
return $arr[1] . $arr[2] . $arr[3];
}
}
function filterCode($data) { // Uncomment if you want to escape anything within a <pre> tag
//$modifiedData = preg_replace_callback('@(<pre.*>)(.*)(<\/pre>)@isU', 'escapeHTML', $data);
$modifiedData = preg_replace_callback('@(<code.*>)(.*)(<\/code>)@isU', 'escapeHTML', $data);
$modifiedData = preg_replace_callback('@(<tt.*>)(.*)(<\/tt>)@isU', 'escapeHTML', $modifiedData);
return $modifiedData;
}
add_filter( 'content_save_pre', 'filterCode', 9 );
add_filter( 'excerpt_save_pre', 'filterCode', 9 );
Modified from Escape HTML.
// Create Custom Global Settings
function custom_settings_page() { ?>
<div class="wrap">
<h1>Custom Settings</h1>
<form method="post" action="options.php">
<?php
settings_fields('section');
do_settings_sections('theme-options');
submit_button();
?>
</form>
</div>
<?php }
function custom_settings_add_menu() {
add_menu_page( 'Custom Settings', 'Custom Settings', 'manage_options', 'custom-settings', 'custom_settings_page', null, 99);
}
add_action( 'admin_menu', 'custom_settings_add_menu' );
// Example setting
function setting_twitter() { ?>
<input type="text" name="twitter" id="twitter" value="<?php echo get_option('twitter'); ?>" />
<?php }
function custom_settings_page_setup() {
add_settings_section('section', 'All Settings', null, 'theme-options');
add_settings_field('twitter', 'Twitter Username', 'setting_twitter', 'theme-options', 'section');
register_setting('section', 'twitter');
}
add_action( 'admin_init', 'custom_settings_page_setup' );
Retrieve Field
<?php echo get_option('twitter'); ?>
Modified from Create a WordPress Theme Settings Page with the Settings API.
function remove_admin_bar() {
remove_action('wp_head', '_admin_bar_bump_cb');
}
add_action('get_header', 'remove_admin_bar');
add_filter( 'woocommerce_get_price_html', 'remove_price');
function remove_price($price){
return ;
}
Ref: https://wisdmlabs.com/blog/the-right-way-to-hide-add-to-cart-button-in-woocommerce/
add_filter( 'woocommerce_is_purchasable', '__return_false');
// Change WooCommerce "Related products" text
add_filter('gettext', 'change_rp_text', 10, 3);
add_filter('ngettext', 'change_rp_text', 10, 3);
function change_rp_text($translated, $text, $domain)
{
if ($text === 'Related products' && $domain === 'woocommerce') {
$translated = esc_html__('You might also like', $domain);
}
return $translated;
}
// Change WooCommerce 'read more' text to 'out of stock'
add_filter( 'woocommerce_product_add_to_cart_text', function( $text ) {
if ( 'Read more' == $text ) {
$text = __( 'Out of stock', 'woocommerce' );
}
return $text;
} );
// Change out of stock message on individual products
add_filter( 'woocommerce_get_availability', 'change_out_of_stock_text_woocommerce', 1, 2);
function change_out_of_stock_text_woocommerce( $availability, $product_to_check ) {
if ( ! $product_to_check->is_in_stock() ) {
$availability['availability'] = __('Due to overwhelming demand we are currently out of stock on this product. We apologise for the inconvenience.', 'woocommerce');
}
return $availability;
}
// Update Street Address Shipping Label
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );
// Our hooked in function - $address_fields is passed via the filter
function custom_override_default_address_fields( $address_fields ) {
$address_fields['address_1']['label'] = 'Street address. Please note we are unable to deliver to PO Boxes.';
return $address_fields;
}
// Add PO Box shipping notice to "ship to different address checkbox"
add_filter('gettext', 'custom_strings_translation', 20, 3);
function custom_strings_translation( $translated_text, $text, $domain ) {
switch ( $translated_text ) {
case 'Ship to a different address?' :
$translated_text = __( 'Ship to a different address? Please note we are unable
to deliver to PO Boxes.', 'woocommerce' );
break;
}
return $translated_text;
}
add_filter('woocommerce_single_product_carousel_options', 'ud_update_woo_flexslider_options');
function ud_update_woo_flexslider_options($options) {
// show arrows
$options['directionNav'] = true;
$options['animation'] = "slide";
// control par texte (boolean) ou bien par vignettes
// $options['controlNav'] = true;
//$options['controlNav'] = "thumbnails";
// $options['mousewheel'] = true;
return $options;
}
/**
* Hide shipping rates when free shipping is available.
* Updated to support WooCommerce 2.6 Shipping Zones.
*
* @param array $rates Array of rates found for the package.
* @return array
*/
function my_hide_shipping_when_free_is_available( $rates ) {
$free = array();
foreach ( $rates as $rate_id => $rate ) {
if ( 'free_shipping' === $rate->method_id ) {
$free[ $rate_id ] = $rate;
break;
}
}
return ! empty( $free ) ? $free : $rates;
}
add_filter( 'woocommerce_package_rates', 'my_hide_shipping_when_free_is_available', 100 );