WordPress函数apply_filters_ref_array()调用已添加到过滤器挂钩的回调函数

WordPress函数apply_filters_ref_array()调用已添加到过滤器挂钩的回调函数

WordPress函数apply_filters_ref_array()调用已添加到过滤器挂钩的回调函数

  在 WordPress CMS内容管理系统中,apply_shortcodes()一个内置函数,apply_filters_ref_array()是 WordPress 中的一个强大函数,用于执行挂钩到特定过滤器的函数,同时通过引用传递多个参数

  推荐:WordPress网站推送通知插件OneSignal插件教程

apply_filters_ref_array()函数基本语法

描述

  调用已添加到过滤器挂钩的回调函数,并在数组中指定参数。

用法

$args = array( 'arg_1', true, 'foo', 'arg_4' );
apply_filters_ref_array( 'my_custom_filter', $args );
  • $hook_name(string) – 过滤器钩子的名称。
  • $args(array) – 提供给挂接到 的函数的参数$hook_name

  推荐:WordPress函数block_template_part()打印特定的块模板

apply_filters_ref_array()函数

  apply_filters_ref_array()调用已添加到过滤器挂钩的回调函数(源文件可参考这里

function apply_filters_ref_array( $hook_name, $args ) {
	global $wp_filter, $wp_filters, $wp_current_filter;

	if ( ! isset( $wp_filters[ $hook_name ] ) ) {
		$wp_filters[ $hook_name ] = 1;
	} else {
		++$wp_filters[ $hook_name ];
	}

	// Do 'all' actions first.
	if ( isset( $wp_filter['all'] ) ) {
		$wp_current_filter[] = $hook_name;
		$all_args            = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
		_wp_call_all_hook( $all_args );
	}

	if ( ! isset( $wp_filter[ $hook_name ] ) ) {
		if ( isset( $wp_filter['all'] ) ) {
			array_pop( $wp_current_filter );
		}

		return $args[0];
	}

	if ( ! isset( $wp_filter['all'] ) ) {
		$wp_current_filter[] = $hook_name;
	}

	$filtered = $wp_filter[ $hook_name ]->apply_filters( $args[0], $args );

	array_pop( $wp_current_filter );

	return $filtered;
}

  推荐:WordPress函数email_exists()确定给定的电子邮件是否存在

如何使用apply_filters_ref_array()

  此示例显示了该函数的最基本用法,将参数数组传递给自定义过滤器。

// Define the arguments.
$args = array( 'apple', true, 'orange', 'banana' );

// Apply the filter.
apply_filters_ref_array( 'my_fruit_filter', $args );

  在回调中访问数组值此示例演示如何在回调函数中访问数组值。

function my_fruit_callback( $args ) {
  // Access values with $args[0], $args[1] etc.
  echo "First fruit: " . $args[0]; // Outputs: First fruit: apple
}

add_filter( 'my_fruit_filter', 'my_fruit_callback' );

  在回调中修改数组值此示例演示了在回调函数中修改数组值。

function my_fruit_callback( $args ) {
  // Modify array values.
  $args[0] = 'grape';
}

add_filter( 'my_fruit_filter', 'my_fruit_callback' );

// The first argument will now be 'grape' instead of 'apple'.

  传递引用指针此示例演示如何将引用指针作为数组元素传递。

apply_filters_ref_array( 'my_fruit_filter', array( &$args ) );

function my_fruit_callback( &$args ) {
  // Access values with $args[0], $args[1] etc.
  echo "First fruit: " . $args[0]; // Outputs: First fruit: apple
}

add_action('my_fruit_filter', 'my_fruit_callback');

  将数组元素复制到参数中此示例演示如何将数组元素复制到参数变量中。在所有示例中,将“my_fruit_filter”和“my_fruit_callback”替换为您自己的过滤器和回调函数名称。

function my_fruit_callback( $arg1, $arg2, $arg3, $arg4 ) {
  // Access values with $arg1, $arg2 etc.
  echo "First fruit: " . $arg1; // Outputs: First fruit: apple
}

add_action( 'my_fruit_filter', 'my_fruit_callback', 10, 4 );

  推荐:怎么删除WordPress未使用的数据库表(150+插件主题数据库表名称)

  推荐:WordPress函数使用手册

5/5 - (1 vote)

Claude、Netflix、Midjourney、ChatGPT Plus、PS、Disney、Youtube、Office 365、多邻国Plus账号购买,ChatGPT API购买,优惠码XDBK,用户购买的时候输入优惠码可以打95折

Chatgpt-Plus注册购买共享账号
滚动至顶部