Useful WordPress Utility Functions for Theme and Plugin Development

A companion to the WooCommerce functions post published last month, this article collects the WordPress utility functions that appear most often across different projects. All snippets below go in functions.php or a dedicated plugin file.

Problem: WordPress and ACF development involves the same repeated utility patterns — safely rendering links, checking post types, truncating strings, formatting dates — that are verbose to write inline every time.

Solution: The reference below collects the most useful reusable helper snippets for theme and plugin development, grouped by category: ACF output, post type detection, string utilities, URL helpers, and date formatting.

0. Render an ACF link safely

<?php
function the_custom_acf_link( $title, $url, $target, $class = '' ) {
    if ( ! $url ) {
        return;
    }
    $target     = $target ? '_blank' : '_self';
    $class_attr = $class ? ' class="' . esc_attr( preg_replace( '/[^ \w-]+/', '', $class ) ) . '"' : '';
    printf(
        '<a href="%s"%s target="%s">%s</a>',
        esc_url( $url ),
        $class_attr,
        esc_attr( $target ),
        esc_html( $title )
    );
}

1. Render an ACF image safely

<?php
function the_custom_acf_image( $image, $class = '' ) {
    if ( empty( $image['url'] ) ) {
        return;
    }
    $class_attr = $class ? ' class="' . esc_attr( preg_replace( '/[^ \w-]+/', '', $class ) ) . '"' : '';
    printf(
        '<img%s src="%s" alt="%s">',
        $class_attr,
        esc_url( $image['url'] ),
        esc_attr( $image['alt'] ?? '' )
    );
}

2. Check whether a taxonomy term has children

<?php
function term_has_children( $term_id, $taxonomy = 'category' ) {
    $children = get_terms( [
        'taxonomy'   => $taxonomy,
        'parent'     => $term_id,
        'hide_empty' => false,
        'number'     => 1,
        'fields'     => 'ids',
    ] );
    return ! empty( $children );
}

3. Prevent a specific plugin from showing update notifications

<?php
add_filter( 'site_transient_update_plugins', 'disable_specific_plugin_update' );

function disable_specific_plugin_update( $transient ) {
    $plugin = 'plugin-folder/plugin.php'; // relative path from wp-content/plugins/
    if ( isset( $transient->response[ $plugin ] ) ) {
        unset( $transient->response[ $plugin ] );
    }
    return $transient;
}

4. Allow JSON file uploads in the Media Library

<?php
add_filter( 'upload_mimes', 'allow_json_uploads' );

function allow_json_uploads( $mime_types ) {
    $mime_types['json'] = 'application/json';
    return $mime_types;
}

5. Track post view counts without a plugin

<?php
function increment_post_view_count( $post_id ) {
    $meta_key = 'post_views_count';
    $count    = (int) get_post_meta( $post_id, $meta_key, true );
    update_post_meta( $post_id, $meta_key, $count + 1 );
}

// Disable prefetching so the count is not incremented by browser speculation
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10 );

add_action( 'wp_head', function () {
    if ( is_singular( [ 'post', 'news', 'event' ] ) ) {
        increment_post_view_count( get_the_ID() );
    }
} );

NOTE: The view-count function above increments on every page load, including bot traffic and repeated visits. For accurate unique-visitor counts, check for a cookie or session before incrementing, or use a purpose-built analytics solution.