^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 5年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云1核2g仅需102元/年,今日10点开抢

wordpress函数add_filter()用法示例

add_filter( string $tag,  callable $function_to_add,  int $priority = 10,  int $accepted_args = 1 )

将函数连接到特定过滤器动作。


描述

过滤器是 WordPress 发布的一种 hook ,在不同类别的文本被添加到书库据或发送到浏览器窗口前对这些文本进行修改。插件可用过滤器 API 规定在修改特定类型的文本时,执行一个还是多个 PHP 函数。

WordPress 提供了过滤器钩子,允许插件在运行时修改各种类型的内部数据。

插件可以通过将回调绑定到筛选器钩子来修改数据。当稍后应用筛选器时,每个绑定回调按优先级顺序运行,并有机会通过返回一个新值来修改一个值。

下面的示例演示回调函数如何绑定到筛选器钩子。

变量 $example 返回给回调函数,可能是修改后的数据:

function example_callback( $example ) {
    // Maybe modify $example in some way.
    return $example;
}
add_filter( 'example_filter', 'example_callback' );

绑定回调函数可以接受从没有到作为参数传递参数的总数,在相应的 apply_filters() 调用。

换句话说,如果一个 apply_filters() 有四个可选参数,它可以接受任何个数的参数,参数可以设置为一个或者四个。重要的是,$accepted_args 的值必须反映参数绑定回调 actuallyopted 接受参数的个数。如果没有设置被认为与接受 1 参数相同的回调参数。例如:

// Filter call.
$value = apply_filters( 'hook', $value, $arg2, $arg3 );

// Accepting zero/one arguments.
function example_callback() {
    ...
    return 'some value';
}
add_filter( 'hook', 'example_callback' ); // Where $priority is default 10, $accepted_args is default 1.

// Accepting two arguments (three possible).
function example_callback( $value, $arg2 ) {
    ...
    return $maybe_modified_value;
}
add_filter( 'hook', 'example_callback', 10, 2 ); // Where $priority is 10, $accepted_args is 2.

注意:无论回调函数是否有效,函数将返回 true。由你来负责。这样做是为了优化目的,所以一切都尽可能快。


参数

$tag
(string)
(Required)
连接到 $function_to_add 的过滤器名称
$function_to_add
(callable)
(Required)
应用过滤器时被调用函数的名称。
$priority
(int)
(Optional)
用于规定函数被执行的顺序,函数与特定动作关联。较小的数字匹配较早的执行,同等优先级的函数按加入 action 的顺序被执行。默认:10。
$accepted_args
(int)
(Optional)
函数所接受参数的数量。在 WordPress 1.5.1 及之后版本中,连接的函数可吸收其它在调用 do_action() 或 apply_filters() 时设置的参数。默认:1。

返回值

(true)


源代码

File: wp-includes/plugin.php

function add_filter( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) {
	global $wp_filter;
	if ( ! isset( $wp_filter[ $tag ] ) ) {
		$wp_filter[ $tag ] = new WP_Hook();
	}
	$wp_filter[ $tag ]->add_filter( $tag, $function_to_add, $priority, $accepted_args );
	return true;
}

更新日志

Version 描述
0.71 Introduced.

More Information

  • Hooked 的功能可以采取额外的参数设置,当匹配 do_action() 或 apply_filters() 调用运行。例如,在 comment_id_not_found 行动将通过评论 ID 每个回调。
  • 虽然你可以通过 $accepted_args 设置参数的数量,你只能操纵 $value 。其他参数仅用于提供上下文,它们的值不能通过筛选器函数更改。
  • 还可以将类方法传递为回调函数。

静态类方法:

add_filter( 'media_upload_newtab', array( 'My_Class', 'media_upload_callback' ) );

实例方法:

add_filter( 'media_upload_newtab', array( $this, 'media_upload_callback' ) );

还可以将匿名函数传递为回调函数。例如:

add_filter( 'the_title', function( $title ) { return '<strong>' . $title . '</strong>'; } );

相关函数

Used By

  • wp-includes/widgets/class-wp-widget-media-video.php:WP_Widget_Media_Video::render_media()
  • wp-includes/widgets/class-wp-widget-media.php:WP_Widget_Media::_register()
  • wp-includes/class-wp-customize-manager.php:WP_Customize_Manager::save_changeset_post()
  • wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php:WP_REST_Attachments_Controller::prepare_items_query()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php:WP_REST_Posts_Controller::prepare_item_for_response()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php:WP_REST_Posts_Controller::get_item_permissions_check()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php:WP_REST_Posts_Controller::get_items()
  • wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php:WP_REST_Comments_Controller::check_read_post_permission()
  • wp-includes/class-wp-locale-switcher.php:WP_Locale_Switcher::init()
  • wp-includes/class-wp-taxonomy.php:WP_Taxonomy::add_hooks()
  • wp-includes/class-wp-customize-nav-menus.php:WP_Customize_Nav_Menus::insert_auto_draft_post()
  • wp-includes/customize/class-wp-customize-custom-css-setting.php:WP_Customize_Custom_CSS_Setting::preview()
  • wp-includes/class-wp-metadata-lazyloader.php:WP_Metadata_Lazyloader::queue_objects()
  • wp-includes/class-wp-customize-widgets.php:WP_Customize_Widgets::render_widget_partial()
  • wp-includes/class-wp-customize-widgets.php:WP_Customize_Widgets::selective_refresh_init()
  • wp-includes/rest-api.php:rest_api_default_filters()
  • wp-admin/includes/class-wp-screen.php:WP_Screen::render_view_mode()
  • wp-includes/customize/class-wp-customize-nav-menu-setting.php:WP_Customize_Nav_Menu_Setting::update()
  • wp-includes/customize/class-wp-customize-nav-menu-setting.php:WP_Customize_Nav_Menu_Setting::preview()
  • wp-includes/customize/class-wp-customize-nav-menu-item-setting.php:WP_Customize_Nav_Menu_Item_Setting::update()
  • wp-includes/customize/class-wp-customize-nav-menu-item-setting.php:WP_Customize_Nav_Menu_Item_Setting::preview()
  • wp-includes/customize/class-wp-customize-nav-menus-panel.php:WP_Customize_Nav_Menus_Panel::render_screen_options()
  • wp-includes/class-wp-customize-nav-menus.php:WP_Customize_Nav_Menus::customize_preview_init()
  • wp-includes/class-wp-customize-nav-menus.php:WP_Customize_Nav_Menus::customize_register()
  • wp-includes/class-wp-customize-nav-menus.php:WP_Customize_Nav_Menus::__construct()
  • wp-admin/includes/class-wp-site-icon.php:WP_Site_Icon::get_post_metadata()
  • wp-admin/includes/class-wp-site-icon.php:WP_Site_Icon::__construct()
  • wp-admin/includes/ajax-actions.php:wp_ajax_crop_image()
  • wp-admin/includes/class-wp-press-this.php:WP_Press_This::html()
  • wp-admin/includes/post.php:wp_edit_attachments_query_vars()
  • wp-admin/includes/class-wp-list-table-compat.php:_WP_List_Table_Compat::__construct()
  • wp-admin/includes/class-wp-automatic-updater.php:WP_Automatic_Updater::update()
  • wp-admin/includes/class-language-pack-upgrader.php:Language_Pack_Upgrader::bulk_upgrade()
  • wp-admin/includes/class-plugin-upgrader.php:Plugin_Upgrader::bulk_upgrade()
  • wp-admin/includes/class-theme-upgrader.php:Theme_Upgrader::check_parent_theme_filter()
  • wp-admin/includes/class-theme-upgrader.php:Theme_Upgrader::install()
  • wp-admin/includes/class-theme-upgrader.php:Theme_Upgrader::upgrade()
  • wp-admin/includes/class-theme-upgrader.php:Theme_Upgrader::bulk_upgrade()
  • wp-admin/includes/class-plugin-upgrader.php:Plugin_Upgrader::install()
  • wp-admin/includes/class-plugin-upgrader.php:Plugin_Upgrader::upgrade()
  • wp-admin/includes/class-wp-screen.php:WP_Screen::render_per_page_options()
  • wp-admin/includes/export.php:export_wp()
  • wp-admin/includes/deprecated.php:wp_update_core()
  • wp-admin/includes/deprecated.php:wp_update_plugin()
  • wp-admin/includes/deprecated.php:wp_update_theme()
  • wp-admin/includes/class-wp-list-table.php:WP_List_Table::__construct()
  • wp-admin/includes/dashboard.php:wp_dashboard_setup()
  • wp-includes/option.php:register_setting()
  • wp-admin/includes/media.php:media_upload_type_form()
  • wp-admin/includes/media.php:media_upload_gallery_form()
  • wp-admin/includes/media.php:media_upload_library_form()
  • wp-admin/includes/ajax-actions.php:wp_ajax_query_attachments()
  • wp-admin/includes/bookmark.php:wp_link_manager_disabled_message()
  • wp-admin/includes/class-wp-media-list-table.php:WP_Media_List_Table::display_rows()
  • wp-admin/includes/class-wp-importer.php:WP_Importer::get_page()
  • wp-admin/includes/class-wp-comments-list-table.php:WP_Comments_List_Table::__construct()
  • wp-admin/includes/nav-menu.php:wp_nav_menu_setup()
  • wp-admin/includes/widgets.php:wp_list_widget_controls()
  • wp-admin/includes/class-wp-posts-list-table.php:WP_Posts_List_Table::display_rows()
  • wp-admin/update-core.php:do_core_upgrade()
  • wp-includes/class-wp-customize-manager.php:WP_Customize_Manager::customize_preview_init()
  • wp-includes/class-wp-customize-manager.php:WP_Customize_Manager::start_previewing_theme()
  • wp-includes/class-wp-customize-manager.php:WP_Customize_Manager::__construct()
  • wp-includes/kses.php:kses_init_filters()
  • wp-includes/class-wp-embed.php:WP_Embed::__construct()
  • wp-includes/class-oembed.php:WP_oEmbed::__construct()
  • wp-includes/plugin.php:add_action()
  • wp-includes/class-wp-customize-setting.php:WP_Customize_Setting::__construct()
  • wp-includes/class-wp-customize-setting.php:WP_Customize_Setting::preview()
  • wp-includes/user.php:wp_signon()
  • wp-includes/revision.php:_set_preview()
  • wp-includes/revision.php:_show_post_preview()
  • wp-includes/ms-default-constants.php:ms_upload_constants()
  • wp-includes/class-wp-customize-widgets.php:WP_Customize_Widgets::start_capturing_option_updates()
  • wp-includes/class-wp-customize-widgets.php:WP_Customize_Widgets::capture_filter_pre_update_option()
  • wp-includes/class-wp-customize-widgets.php:WP_Customize_Widgets::__construct()
  • wp-includes/class-wp-customize-widgets.php:WP_Customize_Widgets::override_sidebars_widgets_for_theme_switch()
  • wp-includes/class-wp-customize-widgets.php:WP_Customize_Widgets::customize_register()
  • wp-includes/comment.php:check_comment_flood_db()
  • wp-includes/meta.php:register_meta()
  • wp-includes/class-wp-editor.php:_WP_Editors::editor()

使用方法

例如:让我们添加 WordPress 默认主题 twentyseventeen 额外的部分的头版。默认情况下,twentyseventeen 主题有 4 节的首页。这个例子将使他们有 6 个。

add_filter( 'twentyseventeen_front_page_sections', 'prefix_custom_front_page_sections' );
function prefix_custom_front_page_sections( $num_sections ){
	return 6;
}

示例:让显示自定义长度的文章摘录。

if( ! function_exists( 'prefix_custom_excerpt_length' ) ){
	function prefix_custom_excerpt_length( $length )
	{
		return 40;
	}
}
add_filter( 'excerpt_length', 'prefix_custom_excerpt_length', 999 );

默认情况下,WordPress 的摘要显示 57 字符。使用上面的代码你可以设置自定义的长度。

Example

过滤器 img_caption_shortcode 将会在 media.php 页面调用:

// Allow plugins/themes to override the default caption template.
$output = apply_filters( 'img_caption_shortcode', '', $attr, $content );
if ( $output != '' )
	return $output;

将用三个参数调用目标筛选器函数:

  • ” <= 这通常是过滤器将被修改的值。
  • $attr
  • $content

为了过滤功能,实际收到全部的参数列表调用 add_filter() 必须修改指定的参数列表上有 3 个参数。

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="wp-caption ' . esc_attr($align) . '" style="width: '
	. (10 + (int) $width) . 'px">' . do_shortcode( $content ) . '<figcaption ' . $capid 
	. 'class="wp-caption-text">' . $caption . '</figcaption></figure>';
}

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈