wpseo_register_var_replacement

Register a new variable replacement.

Description

wpseo_register_var_replacement( (string) $var, (mixed) $replace_function, (string) $type = 'advanced', (string) $help_text = '' ); 

This function is for use by other plugins/themes to easily add their own additional variables to replace. This function should be called from a function on the wpseo_register_extra_replacements action hook. The use of this function is preferred over the older wpseo_replacements, filter as a way to add new replacements. The wpseo_replacements, filter should still be used to adjust standard WPSEO replacement values. The function can not be used to replace standard WPSEO replacement value functions and will thrown a warning if you accidently try. To avoid conflicts with variables registered by WPSEO and other themes/plugins, try and make the name of your variable unique. Variable names also can not start with %%cf_ or %%ct_ as these are reserved for the standard WPSEO variable variables cf_, ct_ and 'ct_desc_'. The replacement function will be passed the undelimited name (i.e. stripped of the %%) of the variable to replace in case you need it.

Example code:

Parameters (4)

0. $var — Optional. (string)
The name of the variable to replace, i.e. %%var%% - the surrounding %% are optional, name can only contain [A-Za-z0-9_-].
1. $replace_function (mixed)
Function or method to call to retrieve the replacement value for the variable Uses the same format as add_filter/add_action function parameter and should *return* the replacement value. DON'T echo it.
2. $type — Optional. (string) => 'advanced'
Type of variable: basic or advanced,, defaults to advanced,.
3. $help_text — Optional. (string) => ''
Help text to be added to the help tab for this variable.

Usage

  1. if ( !function_exists( 'wpseo_register_var_replacement' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'yoast-seo/inc/wpseo-functions.php'; 
  3.  
  4. // The name of the variable to replace, i.e. '%%var%%' 
  5. // - the surrounding %% are optional, name can only contain [A-Za-z0-9_-]. 
  6. $var = ''; 
  7. $replace_function = null; 
  8.  
  9. // Type of variable: 'basic' or 'advanced', defaults to 'advanced'. 
  10. $type = 'advanced'; 
  11.  
  12. // Help text to be added to the help tab for this variable. 
  13. $help_text = ''; 
  14.  
  15. // NOTICE! Understand what this does before running. 
  16. $result = wpseo_register_var_replacement($var, $replace_function, $type, $help_text); 
  17.  

Defined (1)

The function is defined in the following location(s).

/inc/wpseo-functions.php  
  1. function wpseo_register_var_replacement( $var, $replace_function, $type = 'advanced', $help_text = '' ) { 
  2. return WPSEO_Replace_Vars::register_replacement( $var, $replace_function, $type, $help_text );