Global Data functions
3 min read
Global Data holds site-wide values (phone number, address, opening hours) that you manage in WPM Fields › Global Data and print anywhere. Each entry is a variable with a key, a type (text, textarea, number, range, email, URL or password), a value and optional display settings. Variables are also available in templates as {{ glb.key }} (see Variables and hooks).
All functions take the variable key (lower-cased with sanitize_key(); 'PHONE' equals 'phone').
wpmcf_get_global()
wpmcf_get_global( string $key, array $args = [] ): mixedReturns the raw, unescaped value as a string. $args['default'] is returned when the key does not exist or the value is empty ('').
$phone = wpmcf_get_global( 'phone', [ 'default' => 'n/a' ] );
echo esc_html( $phone );- Verified: whole values are strings (
'0','42','1.5'). {{ ... }}expressions in the value are resolved (printing functions do the same) ('Call {{ glb.phone }}!'returns'Call 555!').- Password variables are returned in plain text (decrypted). It is a raw getter for your own code; never print it (see Password helpers and security).
- The
wpmcf/global/valuefilter changes the value (arguments: value, key,$args).
wpmcf_the_global()
wpmcf_the_global( string $key, array $args = [] ): voidEchoes the value escaped, wrapped in <span class="wpmcf-global-value ..."> with the variable's display settings (wrapper class, wrapper ID, prefix, suffix).
| Values | Meaning |
|---|---|---|
|
|
|
|
| default: wrapped |
wpmcf_the_global( 'phone' );
// <span class="wpmcf-global-value ph">Tel: +1 <b>555</b> & "q"</span>
wpmcf_the_global( 'phone', [ 'wrap' => false ] );
wpmcf_the_global( 'site_url', [ 'format' => 'url', 'wrap' => false ] );Verified: a textarea value <b>bold</b><script>x</script> prints fully escaped with auto; with post_html it keeps <b>bold</b> and removes the script. A value of 0 is printed (it counts as non-empty). Password variables print nothing unless wpmcf/global/the_password returns true; then the auto format prints the (decrypted) value.
wpmcf_has_global()
wpmcf_has_global( string $key, array $args = [] ): boolTrue when the variable exists and its value is not empty. '0' counts as a value; '' does not.
wpmcf_update_global()
wpmcf_update_global( string $key, mixed $value ): boolSets the value of an existing variable, stored the way the Global Data screen stores it (password variables are encrypted at rest).
- Returns
falsefor an unknown key (it does not create variables), an array or object value, or a failed write. - Accepts strings, numbers,
true(stored'1'),false(stored empty) andnull(stored empty). - The value goes through
sanitize_textarea_field(): HTML tags are removed ('+90 <i>212</i>'becomes'+90 212'). - Fires
wpmcf/global_data/savedwith the whole variable list, like a save on the screen (page caches are cleared). - No capability check. Check permissions before passing user input.
if ( current_user_can( 'manage_options' ) ) {
wpmcf_update_global( 'phone', sanitize_text_field( wp_unslash( $_POST['phone'] ?? '' ) ) );
}The shortcode
[wpmcf_global key="phone"] prints the same escaped output. See Shortcodes.
Shortcode and function differences
The shortcode's wrap is stricter than the function's: in the shortcode any value that is not true, 1, yes or on (for example wrap="garbage") removes the wrapper, while the PHP function keeps it for any value other than the "off" list above.
Behavior changed in the current version
Older builds printed {{ }} literally and ignored default. Now wpmcf_the_global() and [wpmcf_global] resolve {{ }} expressions like wpmcf_get_global() (verified: Call {{ glb.phone }}! prints Call 555!), and default is used by all three for a missing and an empty variable (printed escaped: N/A <b> prints N/A <b>; wpmcf_has_global() stays false for an empty variable).
Still stuck? Contact support