Home About Skills Products Work
Projects Services Experience
Learn
Tutorials Courses Blogs Resources
Contact
Blog · WordPress

Writing Custom Functions in WordPress: A Beginner's Guide to functions.php

Writing Custom Functions in WordPress: A Beginner's Guide to functions.php

The functions.php file is where WordPress theme customization actually happens — but it's also the easiest place to make a mistake that takes down the entire site. Here's how to write custom functions safely and correctly.

The Golden Rule: Never Edit a Parent Theme Directly

If you're using a third-party theme, put your custom code in a child theme's functions.php, or better, in a small site-specific plugin. A theme update will silently wipe out anything you added directly to the parent theme.

A Safe functions.php Structure

<?php
// Always start with the opening tag, never close it at the end of the file
// (a trailing whitespace after ?> is a classic cause of "headers already sent" errors)

function mytheme_enqueue_styles() {
    wp_enqueue_style('mytheme-style', get_stylesheet_uri());
}
add_action('wp_enqueue_scripts', 'mytheme_enqueue_styles');

Hooks: Actions vs Filters

This is the single most important WordPress concept to understand. An action lets you run code AT a specific point (nothing is returned):

add_action('wp_footer', function () {
    echo '<!-- Custom footer script -->';
});

A filter lets you modify a value as it passes through (you must return something):

add_filter('the_title', function ($title) {
    return $title . ' — ' . get_bloginfo('name');
});

Common, Genuinely Useful Snippets

// Add a custom excerpt length
add_filter('excerpt_length', fn() => 20);

// Add a custom image size
add_action('after_setup_theme', function () {
    add_image_size('card-thumb', 400, 250, true);
});

// Disable Gutenberg block editor for a specific post type
add_filter('use_block_editor_for_post_type', function ($use, $post_type) {
    return $post_type === 'testimonial' ? false : $use;
}, 10, 2);

Defensive Habits That Prevent Fatal Errors

  • Always check function_exists() before redeclaring anything that might already exist.
  • Prefix every function name (mytheme_, myplugin_) to avoid collisions with plugins.
  • Never run untested code directly on a live production site — a single fatal error in functions.php can white-screen the entire site instantly.

Once actions and filters click, most of what looks like "WordPress magic" in plugins turns out to be exactly this pattern, used a few hundred times.

How to Register a Custom Post Type in WordPress (Step-by-Step)

The complete register_post_type() walkthrough, including the rewrite-rules gotcha that causes a confusing 404 every time.

Getting Started with Advanced Custom Fields (ACF) in WordPress

Getting Started with Advanced Custom Fields (ACF) in WordPress

Field groups, repeaters, displaying ACF data in templates, and registering fields in code so they ship with your theme.

WordPress for Developers: Hooks, Actions, and Filters Explained

WordPress for Developers: Hooks, Actions, and Filters Explained

The mental model behind every WordPress plugin and theme — and how to create your own hooks so others can extend your code too.

Esc