add_filter()

最后更新于:2021-11-25 19:11:33

add_filter( string$hook_name, callable$callback, int$priority=10, int$accepted_args=1)

Adds a callback function to a filter hook.

参数

$hook_name

(string) (Required) The name of the filter to add the callback to.

$callback

(callable) (Required) The callback to be run when the filter is applied.

$priority

(int) (Optional) Used to specify the order in which the functions associated with a particular filter are executed. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the filter.

Default value: 10

$accepted_args

(int) (Optional) The number of arguments the function accepts.

Default value: 1

响应

(true) Always returns true.

源文件

文件: gc-includes/plugin.php

function add_filter( $hook_name, $callback, $priority = 10, $accepted_args = 1 ) {
	global $gc_filter;

	if ( ! isset( $gc_filter[ $hook_name ] ) ) {
		$gc_filter[ $hook_name ] = new GC_Hook();
	}

	$gc_filter[ $hook_name ]->add_filter( $hook_name, $callback, $priority, $accepted_args );

	return true;
}
add_filter('img_caption_shortcode', 'my_img_caption_shortcode_filter',10,3);

/**
 * Filter to replace the  shortcode text with HTML5 compliant code
 *
 * @return text HTML content describing embedded figure
 **/
function my_img_caption_shortcode_filter($val, $attr, $content = null)
{
	extract( shortcode_atts( array(
		'id'	=> '',
		'align'	=> '',
		'width'	=> '',
		'caption' => ''
	), $attr ) );
	
	if ( 1 > (int) $width || empty($caption) )
		return $val;

	$capid = '';
	if ( $id ) {
		$id = esc_attr( $id );
		$capid = 'id="figcaption_' . $id . '" ';
		$id = 'id="' . $id . '" aria-labelledby="figcaption_' . $id . '" ';
	}

	return '<figure ' . $id . 'class="gc-caption ' . esc_attr($align) . '" style="width: '
	. (10 + (int) $width) . 'px">' . do_shortcode( $content ) . '<figcaption ' . $capid 
	. 'class="gc-caption-text">' . $caption . '</figcaption></figure>';
}