zoukankan      html  css  js  c++  java
  • wordpress中add_action和add_filter

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

    官网是这么说的:在一个特定的动作上挂钩一个函数。

    那么就有对应的执行这个特定动作的函数:

    do_action( string $tag,  $arg = '' )

    在我理解他有这麽一个好处,就是把多个不同运用的函数一起执行,进行输出。

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

    add_filter跟add_action类似,在一个特定的动作上挂钩一个方法或函数,主要的区别就是有返回值。通过官网的例子可以看出:

    // 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.

    添加静态方法:

    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>'; } );

    与此对应的有apply_filter调用钩子上的 函数或方法

    apply_filters( string $tag, mixed $value )

    add_action与add_filter 主要的区别就是一个有返回值一个没有返回值。

    apply_filter和do_action都是执行钩子上挂载的函数集。

  • 相关阅读:
    redis集群
    鉴权方案选择
    spring mvc 自定义handler不拦截静态资源
    servlet3
    压测工具 ab jmeter
    死锁产生的原因
    缓存方案:本地guavaCache, 远程redis?
    使用spring boot admin
    groovy使用小记
    python--面试题01
  • 原文地址:https://www.cnblogs.com/webph/p/6741218.html
Copyright © 2011-2022 走看看