Overview

Post meta and theme options access with built‑in content filtering and inline tag formatting.


Description

A utility class that provides a streamlined API for accessing post meta and theme options, with methods for formatted output (headlines with format tag support, filtered content via the_content). Designed for use in template files and block render callbacks.

The options key is configurable via the method_utility_option_key filter (default: method_options).


Source

File: lib/class-method-utility.php


Properties

PropertyVisibilityTypeDescription
$loaded_metaprotectedarrayCached post meta from load_meta()
$optsprotectedarrayTheme options loaded from get_option()

Methods

__construct()

Loads theme options from the key returned by method_utility_option_key filter.


load_meta( $id )

Loads all post meta for a given post ID into $loaded_meta for subsequent access via get_loaded_meta() and related methods.

ParamTypeDescription
$idintThe post ID.

Returns: (void)


unload_meta()

Clears the $loaded_meta cache. Call this when switching between posts.

Returns: (void)


get_loaded_meta( $key, $fallback = '' )

Retrieves a single meta value from the loaded meta cache.

ParamTypeDescription
$keystringThe meta key.
$fallbackstringValue to return if the key is not found. Default ''.

Returns: (mixed) The meta value, or $fallback.


get_serialized_loaded_meta( $key )

Retrieves and unserializes a meta value from the loaded cache.

ParamTypeDescription
$keystringThe meta key.

Returns: (mixed) The unserialized value, or false.


get_loaded_headline( $key, $before, $after, $fallback = '' )

Returns a formatted headline from loaded meta, wrapped in $before/$after tags, with format tag processing. Content is HTML‑escaped before tag processing.

ParamTypeDescription
$keystringThe meta key.
$beforestringOpening HTML tag (e.g., '<h2>').
$afterstringClosing HTML tag (e.g., '</h2>').
$fallbackstringFallback content if meta is empty.

Returns: (string) Formatted HTML, or empty string.


get_loaded_content( $key, $before, $after, $fallback = '' )

Returns content from loaded meta, run through the_content filter, wrapped in $before/$after tags.

ParamTypeDescription
$keystringThe meta key.
$beforestringOpening HTML.
$afterstringClosing HTML.
$fallbackstringFallback content.

Returns: (string) Formatted HTML, or empty string.


get_option( $key, $fallback = '' )

Retrieves a value from the loaded theme options.

ParamTypeDescription
$keystringThe option key.
$fallbackstringFallback value. Default ''.

Returns: (mixed) The option value, or $fallback.


get_headline_from_option( $key, $before, $after, $fallback = '' )

Same as get_loaded_headline() but reads from theme options instead of post meta.


get_content_from_option( $key, $before, $after, $fallback = '' )

Same as get_loaded_content() but reads from theme options.


format_tags( $text )

Replaces shortcode‑style inline tags with HTML. Filterable via method_utility_format_tags.

TagOutput
[[br]]<br>
[[mbr]]Mobile‑only <br>
[[dbr]]Desktop‑only <br>
[[strong]]/[[/strong]], [[b]]/[[/b]]<strong>/</strong>
[[em]]/[[/em]]<em>/</em>
[[u]]/[[/u]]<span class="method-underlined">
[[bull]]&bull; in a span

Returns: (string) Text with tags replaced.


format_headline( $text )

Escapes HTML via esc_html() then applies format_tags(). Safe for use with user‑provided text.

Returns: (string)


filter_content( $content )

Runs content through WordPress’s the_content filter.

Returns: (string)


check_array_key( $item, $key )

Instance method equivalent of the global method_check_array_key().

Returns: (bool)


check_array( $item, $key )

Instance method equivalent of the global method_check_array().

Returns: (bool)


str_replace_assoc( array $replace, $subject )

Instance method equivalent of the global method_str_replace_assoc().

Returns: (string)


array_to_p( $array, $class = '', $separator = '', $show_separator = false )

Converts an array of strings into a <p> element with <br> breaks between items. Each item is processed through format_tags(). Optionally adds visible or screen‑reader‑only separators.

This method is primarily used to render information such as street addresses that have been added line by line via a CMB2 text field with repeatable text.

ParamTypeDescription
$arrayarrayArray of text strings (may be serialized).
$classstringCSS class for the <p> element.
$separatorstringSeparator text between items.
$show_separatorboolWhen false, separator is wrapped in .visually-hidden.

Returns: (string) HTML <p> element.


Usage

PHP
$m = new Method_Utility();
$m->load_meta( get_the_ID() );

echo $m->get_loaded_headline( '_hero_title', '<h1>', '</h1>', 'Default Title' );
echo $m->get_loaded_content( '_hero_body', '<div class="body">', '</div>' );

$phone = $m->get_option( 'contact_phone', '555-0000' );

$m->unload_meta();