Method_Utility
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
| Property | Visibility | Type | Description |
|---|---|---|---|
$loaded_meta | protected | array | Cached post meta from load_meta() |
$opts | protected | array | Theme 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.
| Param | Type | Description |
|---|---|---|
$id | int | The post ID. |
Returns: (void)
unload_meta()
Clears the $loaded_meta cache. Call this when switching between posts.
Returns: (void)
get_loaded_meta( $key, $fallback = '' )
get_loaded_meta( $key, $fallback = '' )Retrieves a single meta value from the loaded meta cache.
| Param | Type | Description |
|---|---|---|
$key | string | The meta key. |
$fallback | string | Value to return if the key is not found. Default ''. |
Returns: (mixed) The meta value, or $fallback.
get_serialized_loaded_meta( $key )
get_serialized_loaded_meta( $key )Retrieves and unserializes a meta value from the loaded cache.
| Param | Type | Description |
|---|---|---|
$key | string | The meta key. |
Returns: (mixed) The unserialized value, or false.
get_loaded_headline( $key, $before, $after, $fallback = '' )
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.
| Param | Type | Description |
|---|---|---|
$key | string | The meta key. |
$before | string | Opening HTML tag (e.g., '<h2>'). |
$after | string | Closing HTML tag (e.g., '</h2>'). |
$fallback | string | Fallback content if meta is empty. |
Returns: (string) Formatted HTML, or empty string.
get_loaded_content( $key, $before, $after, $fallback = '' )
get_loaded_content( $key, $before, $after, $fallback = '' )Returns content from loaded meta, run through the_content filter, wrapped in $before/$after tags.
| Param | Type | Description |
|---|---|---|
$key | string | The meta key. |
$before | string | Opening HTML. |
$after | string | Closing HTML. |
$fallback | string | Fallback content. |
Returns: (string) Formatted HTML, or empty string.
get_option( $key, $fallback = '' )
get_option( $key, $fallback = '' )Retrieves a value from the loaded theme options.
| Param | Type | Description |
|---|---|---|
$key | string | The option key. |
$fallback | string | Fallback value. Default ''. |
Returns: (mixed) The option value, or $fallback.
get_headline_from_option( $key, $before, $after, $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 = '' )
get_content_from_option( $key, $before, $after, $fallback = '' )Same as get_loaded_content() but reads from theme options.
format_tags( $text )
format_tags( $text )Replaces shortcode‑style inline tags with HTML. Filterable via method_utility_format_tags.
| Tag | Output |
|---|---|
[[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]] | • in a span |
Returns: (string) Text with tags replaced.
format_headline( $text )
format_headline( $text )Escapes HTML via esc_html() then applies format_tags(). Safe for use with user‑provided text.
Returns: (string)
filter_content( $content )
filter_content( $content )Runs content through WordPress’s the_content filter.
Returns: (string)
check_array_key( $item, $key )
check_array_key( $item, $key )Instance method equivalent of the global method_check_array_key().
Returns: (bool)
check_array( $item, $key )
check_array( $item, $key )Instance method equivalent of the global method_check_array().
Returns: (bool)
str_replace_assoc( array $replace, $subject )
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 )
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.
| Param | Type | Description |
|---|---|---|
$array | array | Array of text strings (may be serialized). |
$class | string | CSS class for the <p> element. |
$separator | string | Separator text between items. |
$show_separator | bool | When false, separator is wrapped in .visually-hidden. |
Returns: (string) HTML <p> element.
Usage
$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();