Smart Snippets for WordPress – Part 1: functions.php Snippets I Actually Use (and Why They’re Worth It)

smart snippets part 1

Sometimes, the best WordPress customizations don’t need a plugin—just a few smart lines in your functions.php file. Here are ten real-world snippets I personally use in my themes (like the Frost child theme on jonimms.com), along with why they matter.

🖼️ 1. Enable Featured Images (Post Thumbnails)

add_theme_support('post-thumbnails');

Why use it: Enables support for featured images (post thumbnails). These are critical for blogs, news feeds, and content cards.

When to use it: Immediately when setting up a custom theme that displays any kind of post imagery.

🧠 2. Automatically Add Alt Text from Image Title

add_filter('wp_get_attachment_image_attributes', function($attr, $attachment){
    if (empty($attr['alt'])) {
        $attr['alt'] = get_the_title($attachment->ID);
    }
    return $attr;
}, 10, 2);

Why use it: Ensures accessibility and SEO by auto-filling missing alt text with the image title.

When to use it: Any time you’re managing media-heavy sites or working with non-technical content editors.

🧹 3. Remove Emoji Scripts

remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');

Why use it: Speeds up your site by removing unneeded scripts. Emojis load from the OS anyway.

When to use it: Every time—unless you’re actually using WordPress emoji features.

🚫 4. Disable Author Archives

add_action('template_redirect', function() {
    if (is_author()) {
        wp_redirect(home_url());
        exit;
    }
});

Why use it: Hides author archive pages, which can expose usernames and attract bots.

When to use it: If you run a single-author site or don’t need author-based archives at all.

📝 5. Rename the “Post” Post Type to “Articles”

function rename_post_type_labels() {
    global $wp_post_types;
    $wp_post_types['post']->labels->name = 'Articles';
    $wp_post_types['post']->labels->singular_name = 'Article';
}
add_action('init', 'rename_post_type_labels');

Why use it: Makes the admin UI match your content type and tone (great for clients too).

When to use it: If you’re repurposing the “Posts” post type for something like News, Recipes, or anything non-blog.

💾 6. Allow SVG Uploads

add_filter('upload_mimes', function($mimes){
    $mimes['svg'] = 'image/svg+xml';
    return $mimes;
});

Why use it: SVGs are crisp and scalable—ideal for logos and icons.

When to use it: Anytime you need vector graphics and trust your source. (Consider sanitizing!)

🎯 7. Load Scripts Only on Specific Templates

add_action('wp_enqueue_scripts', function() {
    if (is_page_template('template-contact.php')) {
        wp_enqueue_script('contact-form', get_theme_file_uri('/js/contact.js'), [], null, true);
    }
});

Why use it: Keeps your site lean by only loading assets where needed.

When to use it: Forms, maps, modals—only load their JS when they’re used.

✏️ 8. Add Custom Editor Styles

add_action('after_setup_theme', function() {
    add_theme_support('editor-styles');
    add_editor_style('editor-styles.css');
});

Why use it: Makes the block editor look and feel more like your frontend—especially fonts and spacing.

When to use it: Always. Clients love WYSIWYG editing that actually matches the live site.

🧩 9. Customize the “Read More” Link in Excerpts

add_filter('excerpt_more', fn() => '... Read more');

Why use it: Makes teaser content more useful and inviting.

When to use it: On any blog or archive layout with excerpts turned on.

🧪 10. Add a Body Class Based on Environment

add_filter('body_class', function($classes) {
    if (defined('WP_ENV')) {
        $classes[] = 'env-' . WP_ENV;
    }
    return $classes;
});

Why use it: Helps you style or debug based on environment—staging, dev, production.

When to use it: In multisite, agency, or CI/CD workflows where environment matters.

Have a favorite functions.php snippet? Drop it in the comments or send it my way—I’m always looking for clever and practical ideas.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *