Important WordPress theme functions
Updated as on April 9, 2026
I have been working on WordPress for years and I still carry aroung a standard functions.php file while develop a new WordPress theme.
I am explaining here how to modernizing functions.php file so it will work seamlessly with WordPress 6 +.
These are the list of very common and useful features included here.
- Initial theme setup with navigation menu, featured image, custom image size, excerpt support
- Sidebar registration
- Image compression while uploading
- Responsive image fixing
- Enable sidebar widget
- Shorten post / page title by word limit
- Shorten excerpt by word limit
- Shorten content by word limit
- Strip images from content
- Display the content of a page by page ID
- Display the content of a page by page slug
- Anti spam email shortcode inside content editor
- Change default sender name and sender email address
- Remove WordPress version from Head
- Enqueue script and CSS
- Disable admin bar for all users but admins in the front end
- Disable default widgets
Add these codes in functions.php file of the active theme.
<?php
/**
* Theme Setup
*/
function mytheme_setup() {
// Navigation Menus
register_nav_menus([
'top-menu' => __('Header Navigation', 'mytheme'),
'bottom-menu' => __('Footer Navigation', 'mytheme'),
]);
// Featured Images
add_theme_support('post-thumbnails');
// Custom Image Sizes
add_image_size('blog_featured', 600, 300, true);
// Add excerpt support to pages
add_post_type_support('page', 'excerpt');
}
add_action('after_setup_theme', 'mytheme_setup');
/**
* Sidebar Registration
*/
function mytheme_widgets_init() {
register_sidebar([
'name' => __('Sidebar', 'mytheme'),
'id' => 'sidebar',
'description' => __('Main Sidebar', 'mytheme'),
'before_widget' => '<div class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
]);
}
add_action('widgets_init', 'mytheme_widgets_init');
/**
* Image Quality (Modern WebP/JPEG)
*/
add_filter('wp_editor_set_quality', function() {
return 82;
});
/**
* Remove width & height attributes (responsive images fix)
*/
function mytheme_remove_image_dimensions($html) {
return preg_replace('/(width|height)="\d*"\s?/', '', $html);
}
add_filter('post_thumbnail_html', 'mytheme_remove_image_dimensions');
add_filter('image_send_to_editor', 'mytheme_remove_image_dimensions');
/**
* Trim Title by Words
*/
function mytheme_short_title($length = 8, $after = '...') {
$title = wp_strip_all_tags(get_the_title());
$words = explode(' ', $title);
if (count($words) > $length) {
$words = array_slice($words, 0, $length);
return implode(' ', $words) . $after;
}
return $title;
}
/**
* Custom Excerpt Length
*/
function mytheme_excerpt($limit = 25) {
return wp_trim_words(get_the_excerpt(), $limit, '...');
}
/**
* Custom Content Trim
*/
function mytheme_content($limit = 25) {
return wp_trim_words(
wp_strip_all_tags(get_the_content()),
$limit,
'...'
);
}
/**
* Remove Images from Content
*/
function mytheme_strip_images($content) {
return preg_replace('/<img[^>]+>/i', '', $content);
}
// Usage: add_filter('the_content', 'mytheme_strip_images');
/**
* Get Page Content by ID
*/
function mytheme_get_page_content_by_id($page_id) {
$page = get_post($page_id);
if (!$page) return '';
return apply_filters('the_content', $page->post_content);
}
/**
* Get Page Content by Slug
*/
function mytheme_get_page_content_by_slug($slug) {
$page = get_page_by_path($slug);
if (!$page) return '';
return apply_filters('the_content', $page->post_content);
}
/**
* Email Shortcode [email]example@mail.com[/email]
*/
function mytheme_email_shortcode($atts, $content = null) {
if (!$content) return '';
$email = antispambot($content);
return '<a href="' . esc_attr('mailto:' . $email) . '">' . esc_html($email) . '</a>';
}
add_shortcode('email', 'mytheme_email_shortcode');
/**
* Mail Sender Customization
*/
add_filter('wp_mail_from', function() {
return 'info@yourdomain.com';
});
add_filter('wp_mail_from_name', function() {
return get_bloginfo('name');
});
/**
* Remove WP Version
*/
add_filter('the_generator', '__return_empty_string');
/**
* Enqueue Scripts & Styles
*/
function mytheme_enqueue_assets() {
// CSS
wp_enqueue_style(
'mytheme-style',
get_stylesheet_uri(),
[],
wp_get_theme()->get('Version')
);
// JS
wp_enqueue_script(
'mytheme-custom',
get_template_directory_uri() . '/js/custom.js',
['jquery'],
null,
true
);
}
add_action('wp_enqueue_scripts', 'mytheme_enqueue_assets');
/**
* Disable Admin Bar for Non-Admins
*/
add_filter('show_admin_bar', function($show) {
return current_user_can('administrator') ? $show : false;
});
/**
* Remove Default Widgets (Optional)
*/
function mytheme_unregister_widgets() {
unregister_widget('WP_Widget_Pages');
unregister_widget('WP_Widget_Calendar');
unregister_widget('WP_Widget_Archives');
unregister_widget('WP_Widget_Meta');
unregister_widget('WP_Widget_Search');
unregister_widget('WP_Widget_Tag_Cloud');
}
// add_action('widgets_init', 'mytheme_unregister_widgets', 11);