Skip to main content

PHP Helpers

The rwp Facade

The ReactWP mu-plugin exposes a small static facade for common runtime operations. Project code can call these helpers from a theme or project plugin after mu-plugins have loaded.

ACF Fields

$hero = rwp::field('hero', 42);

The signature mirrors ACF's get_field() arguments:

rwp::field($field, $id = false, $format = true, $escape = false)

ReactWP also has a replacement layer used when ACF values need project-wide substitutions. Use the underlying utility intentionally:

ReactWP\Utils\Field::replace('{home_url}', home_url('/'));
$value = rwp::field('content');
ReactWP\Utils\Field::remove('{home_url}');

Replacements are applied recursively to strings in arrays outside normal non-AJAX admin screens.

The replacement utility also exposes:

MethodPurpose
Field::replace($search, $replace)add one replacement or matching replacement arrays
Field::remove($search)remove one or several registered searches
Field::clear()remove every replacement for the current request
Field::has_replacement()report whether replacements are registered
Field::apply_replacement($value)apply current replacements to a string or nested array explicitly

Queries

rwp::cpt() returns a WP_Query:

$projects = rwp::cpt('project', [
'posts_per_page' => 12,
'post_status' => 'publish',
]);

It is a convenience wrapper, not a different query engine. Reset post data after a loop that calls the_post().

Set project-wide query defaults before calling the facade:

ReactWP\Utils\CustomPostType::default('posts_per_page', 12);

rwp::menu() wraps wp_nav_menu() and defaults to echo => false:

$html = rwp::menu('primary', [
'container' => false,
'menu_class' => 'site-menu',
]);

The optional mobile_bars argument appends a basic hamburger element to the generated items wrapper, then removes that custom argument before WordPress receives the configuration.

React templates usually consume normalized navigation data instead. Use this helper when PHP-rendered markup is the intended output.

ReactWP\Utils\Menu::default($parameter, $value) changes a default passed to later rwp::menu() calls during the request.

Theme Paths and URLs

Resolve an active child-theme file path:

$path = rwp::source([
'path' => '/assets/js/entrypoints.json',
'url' => false,
]);

Resolve its public URL:

$url = rwp::source([
'path' => '/assets/images/logo.svg',
'url' => true,
]);

Options are:

  • base: optional directory prefix
  • path: relative path
  • url: false for a filesystem path, true for a URL
  • theme: stylesheet for the active theme or template for the parent theme

Use home_url() when you need the public path of a permalink. Use wp_parse_url(get_permalink($id), PHP_URL_PATH) when you specifically need only the pathname from a post permalink.

ReactWP\Utils\Source::default($parameter, $value) changes a source resolver default for later calls during the request.

Runtime Payload and Cache

$bootstrap = rwp::bootstrap();
$cache_version = rwp::client_cache_version();
$new_version = rwp::bust_client_cache();
rwp::invalidate_render_cache(['post-type:project']);

rwp::bootstrap() builds the same integrated-theme payload injected into #reactwp-bootstrap. Do not call it repeatedly inside a loop; it resolves route, menus, settings, and media maps.

rwp::invalidate_render_cache() invalidates static and cached SSR fragments whose dependency tags match. The default argument is render:all.

Preview Tokens

$token = rwp::preview_token($post_id, 600);

The optional second argument is the lifetime in seconds. Preview support loads the headless runtime only when needed.

Sanitizers

rwp::sanitize($type, ['value' => $value]) delegates to the matching WordPress sanitizer.

Supported types map directly to these WordPress APIs:

TypeWordPress behaviorAdditional arguments
emailsanitize_email()none
file_namesanitize_file_name()none
hex_colorsanitize_hex_color()none
hex_color_no_hashsanitize_hex_color_no_hash()none
html_classsanitize_html_class()optional fallback
keysanitize_key()none
metasanitize_meta()required key, object_type; optional object_subtype
mime_typesanitize_mime_type()none
optionsanitize_option()required option
sql_orderbysanitize_sql_orderby()none
termsanitize_term()required taxonomy; optional context
term_fieldsanitize_term_field()required field, term_id, taxonomy; optional context
text_fieldsanitize_text_field()none
textarea_fieldsanitize_textarea_field()none
slug or titlesanitize_title()optional fallback, context
title_for_querysanitize_title_for_query()none
title_with_dashessanitize_title_with_dashes()optional raw_title, context
usersanitize_user()optional strict
urlsanitize_url()optional protocols
htmlwp_kses()optional allowed_html, allowed_protocols
post_contentwp_kses_post()none

Every type requires value. A missing required argument or unknown type returns null. sql_orderby validates a SQL ORDER BY fragment; it does not make arbitrary SQL safe.

$message = rwp::sanitize('textarea_field', [
'value' => wp_unslash($_POST['message'] ?? '')
]);

Escaping

rwp::escape() maps an output context to WordPress escaping functions:

echo rwp::escape('html', $title);
echo rwp::escape('attr', $value);
echo rwp::escape('url', $url);

Available types include html, js, url, url_raw, xml, attr, textarea, html__, html_x, attr__, and attr_x.

Sanitization and escaping are different steps. Sanitize when data enters the system; escape when it is rendered in a specific context.