Useful Theme Functions

custom_pagination($query = null, $range = 2) Displays a custom, numbered pagination for posts or custom queries. It shows the current page, a range of neighbor pages, and links to the first and last pages with ellipses (...) where pages are skipped. Parameters: $query (WP_Query Object, optional) - The WP_Query object to paginate. Defaults to the global $wp_query.
$range (Int, optional) - The number of page links to show on either side of the current page. Default is 2. Returns: Void - Echos the HTML for the pagination navigation directly to the output.
Usage Example:
// For the main query on a blog page:
custom_pagination();

// For a custom query where $my_query is defined:
custom_pagination($my_query, 3);
is_mobile() Detects if the user agent string indicates a request is coming from a common mobile phone or small-screen device (e.g., Android, iPhone, Blackberry, etc.). Returns: Boolean - true if the user agent matches common mobile patterns, otherwise false.
Usage Example:if (is_mobile()) { echo '<p>Viewing on a mobile device.</p>'; }
is_curl_installed() Checks if the cURL PHP extension is installed and loaded on the server. cURL is often required for making outgoing HTTP requests (e.g., to APIs). Returns: Boolean - true if cURL is installed and enabled, otherwise false.
Usage Example:if (!is_curl_installed()) { error_log('cURL is not available! API calls might fail.'); }
get_current_language_slug() Retrieves the current two-character language slug (e.g., "en", "de"). It supports Polylang and WPML, falling back to the site's default locale if no language plugin is active. Returns: String - The current language slug.
Usage Example:$lang = get_current_language_slug(); // Returns 'de' or 'en'
echo '<p>Current language: ' . strtoupper($lang) . '</p>';
string_to_slug($str) Converts a given string (like a title) into a URL-friendly, lowercase slug. It specifically handles German umlauts (ä, ö, ü, ß) by converting them to their two-letter equivalents (ae, oe, ue, sz). Parameters: $str (String) - The input string to convert. Returns: String - The converted, clean slug (e.g., 'meine-neue-post').
Usage Example:$title = "Über-Süße Fröhliche Grüße!";
$slug = string_to_slug($title); // Returns 'ueber-suesse-froehliche-gruesse'
tel_to_link($phone_number, $vorwahl = '43') Standardizes a phone number, regardless of its messy input format (spaces, dashes, parentheses), into a clean `+COUNTRYCODE` format suitable for `tel:` links. It strips all non-numeric and non-plus characters. Parameters: $phone_number (String) - The phone number to format (e.g., `+43 (0) 316-123`).
$vorwahl (String, optional) - The default country code (without '+') to use if the number is provided in a local format (e.g., starting with '0'). Default is '43'. Returns: String - The formatted phone number (e.g., '+433161234567').
Usage Example:$num = "0316 123 456 78";
$link_format = tel_to_link($num); // Returns '+4331612345678'
echo '<a href="tel:' . $link_format . '">' . $num . '</a>';
get_attachment($attachment_id) A wrapper function to easily retrieve common metadata for a WordPress media attachment by its ID, compiling them into a single associative array. Parameters: $attachment_id (Int) - The ID of the media file/attachment. Returns: Array - Contains attachment details: `url`, `title`, `caption`, `description`, `alt` text, and `filename`.
Usage Example:$image_data = get_attachment(105);
echo '<img src="' . $image_data['url'] . '" alt="' . $image_data['alt'] . '">';
get_registered_image_sizes() Fetches a list of all default WordPress image sizes and any custom image sizes registered by the theme or plugins. Useful for options/settings pages. Returns: Array - An associative array where keys are image size slugs (e.g., 'thumbnail', 'custom_size') and values are user-friendly descriptions (e.g., 'Thumbnail - 150 x 150 (crop)'). Includes 'full' size.
Usage Example (for a select field):$sizes = get_registered_image_sizes();
foreach ($sizes as $slug => $description) { echo '<option value="' . $slug . '">' . $description . '</option>'; }
get_social_media_icon($platform, $attrs = array()) Renders the SVG icon for a specified social media platform. It first checks for a theme template (`templates/svg/social-platform.php`) and falls back to a generic circle icon if a specific template is not found. Parameters: $platform (String) - The name of the social media platform (e.g., 'facebook', 'instagram').
$attrs (Array, optional) - An array of HTML attributes (e.g., `['width' => '32', 'class' => 'my-icon']`) to apply to the SVG element. Returns: String - The HTML markup for the SVG icon.
Usage Example:echo get_social_media_icon('facebook', ['width' => '30', 'class' => 'footer-icon']);
nl2span($string, $class = '') Converts newlines in a string into HTML span tags, wrapping each non-empty line of text. This is commonly used for CSS-based animations or complex text styling where each line needs to be treated as a separate element. Parameters: $string (String) - The input string containing newlines.
$class (String, optional) - An optional CSS class to apply to all wrapping <span> tags. Returns: String - The string with each non-empty line wrapped in a <span> tag.
Usage Example:$text = "Line 1\nLine 2\n\nLine 3";
echo nl2span($text, 'line-to-animate');
// Output: <span class="line-to-animate">Line 1</span><span class="line-to-animate">Line 2</span><span class="line-to-animate">Line 3</span>