WPSEO_Replace_Vars::register_replacement()public staticYoast 1.0

Register new replacement %%variables%%. For use by other plugins/themes to register extra variables.

Метод класса: WPSEO_Replace_Vars{}

Хуков нет.

Возвращает

true|false. Whether the replacement function was succesfully registered.

Использование

$result = WPSEO_Replace_Vars::register_replacement( $var_to_replace, $replace_function, $type, $help_text );
$var_to_replace(строка) (обязательный)
The name of the variable to replace, i.e. '%%var%%'. Note: the surrounding %% are optional.
$replace_function(разное) (обязательный)
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.
$type(строка)
Type of variable: 'basic' or 'advanced'.
По умолчанию: 'advanced'
$help_text(строка)
Help text to be added to the help tab for this variable.
По умолчанию: ''

Заметки

Код WPSEO_Replace_Vars::register_replacement() Yoast 22.4

public static function register_replacement( $var_to_replace, $replace_function, $type = 'advanced', $help_text = '' ) {
	$success = false;

	if ( is_string( $var_to_replace ) && $var_to_replace !== '' ) {
		$var_to_replace = self::remove_var_delimiter( $var_to_replace );

		if ( preg_match( '`^[A-Z0-9_-]+$`i', $var_to_replace ) === false ) {
			trigger_error( esc_html__( 'A replacement variable can only contain alphanumeric characters, an underscore or a dash. Try renaming your variable.', 'wordpress-seo' ), E_USER_WARNING );
		}
		elseif ( strpos( $var_to_replace, 'cf_' ) === 0 || strpos( $var_to_replace, 'ct_' ) === 0 ) {
			trigger_error( esc_html__( 'A replacement variable can not start with "%%cf_" or "%%ct_" as these are reserved for the WPSEO standard variable variables for custom fields and custom taxonomies. Try making your variable name unique.', 'wordpress-seo' ), E_USER_WARNING );
		}
		elseif ( ! method_exists( self::class, 'retrieve_' . $var_to_replace ) ) {
			if ( $var_to_replace !== '' && ! isset( self::$external_replacements[ $var_to_replace ] ) ) {
				self::$external_replacements[ $var_to_replace ] = $replace_function;
				$replacement_variable                           = new WPSEO_Replacement_Variable( $var_to_replace, $var_to_replace, $help_text );
				self::register_help_text( $type, $replacement_variable );
				$success = true;
			}
			else {
				trigger_error( esc_html__( 'A replacement variable with the same name has already been registered. Try making your variable name unique.', 'wordpress-seo' ), E_USER_WARNING );
			}
		}
		else {
			trigger_error( esc_html__( 'You cannot overrule a WPSEO standard variable replacement by registering a variable with the same name. Use the "wpseo_replacements" filter instead to adjust the replacement value.', 'wordpress-seo' ), E_USER_WARNING );
		}
	}

	return $success;
}