wordpress add_action

说明

在指定的action上挂载(挂钩机制)一个函数。

对于这些动作,您可以在Plugin API/Action Reference上查看。当在Wordpress内部调用do_action()时,这些动作将被触发。

用法

<?php add_action( $tag, $function_to_add, $priority, $accepted_args ); ?>

参数

$tag
(string) (参数必需) 动作的名字。 (对于这些动作,您可以在Plugin API/Action Reference上查看。)

Default: 没有默认值
$function_to_add
(callback) (参数必需) 动作触发时,被调用函数的名字。 注释:在PHP文档中 ‘callback’ 类型的语法解释是有效的。

Default: 没有默认值
$priority
(int) (参数可选) 函数的优先级。改变这个值可以让您的函数在其他函数之前执行,或者是之后执行。默认值为10,例如把这个值设置为5将更早的被执行,设置为12将会晚一些执行。也就是小的值被优先执行。

Default: 10
$accepted_args
(int) (参数可选) 函数参数的个数。在 WordPress 1.5.1+, 当匹配的 do_action() 或apply_filters()运行,钩子函数可以传入格外的参数。例如动作comment_id_not_found可以传入评论的ID。

Default: 1

示例

1、当文章发表的时候发邮件给朋友

function email_friends($post_ID)  {
   $friends = 'bob@example.org, susie@example.org';
   mail($friends, "sally's blog updated" , 'I just put something on my blog: http://blog.example.com');
   return $post_ID;
}

add_action('publish_post', 'email_friends');

带参数

The hooked function takes one argument from the action. Specifically, the ‘echo_comment_id’ function, takes the argument $comment_ID. It then echos the value of the received argument.

接口函数可以从action获取一个参数,具体的,函数 “echo_comment_id” 可以获取 “$comment_ID”, 然后输出接受到的参数:

function echo_comment_id($comment_ID) {
   echo "I just received $comment_ID";
}
add_action('comment_id_not_found','echo_comment_id', 10, 1);

使用匿名函数(Anonymous functions)

$post_type_args = array(
            'post_type' => 'download',
            'plural_name' => 'Downloads',
            'singular_name' => 'Download',
            'rewrite_slug'  => 'download',
            'capability'    => 'post',
            'exclude_from_search'   => TRUE,
            'has_archive'   => FALSE,
            'hierarchical'  => FALSE,
            'supports'  => array( 'title', 'editor', 'thumbnail', 'page-attributes' ),
            'show_in_nav_menus' => FALSE,
            'taxonomy' => array(
		array(
			'name'          => 'download_category',
			'hierarchical'  => true,
			'label'         => 'Download category',
			'labels'        => 'Download Categories',
			'slug'          => 'download_category'
		)
	));

add_action( 'init',  function() use( $post_type_args ){
                        register_post_type($post_type_args['post_type'], array(
                           'labels' => array(
                               'name'           => $post_type_args['plural_name'],
                               'singular_name'  => $post_type_args['singular_name'],
                               'menu_name'      => $post_type_args['plural_name']
                           ),
                           'public'             => true,
                           'publicly_queryable' => true,
                           'show_ui'            => true,
                           'show_in_menu'       => true,
                           'show_in_nav_menus'  => $post_type_args['show_in_nav_menus'],
                           'query_var'          => true,
                           'rewrite'            => array('slug' => isset($post_type_args['rewrite_slug'])?$post_type_args['rewrite_slug']:$post_type_args['post_type']),
                           'capability_type'    => $post_type_args['capability'],
                           'exclude_from_search'=> $post_type_args['exclude_from_search'],
                           'has_archive'        => $post_type_args['has_archive'],
                           'hierarchical'       => $post_type_args['hierarchical'],
                           'supports'           => $post_type_args['supports']
                        ));

                        if ($post_type_args['taxonomy'] != '') {
                            $this->_add_taxonomy($post_type_args['post_type'],$post_type_args['taxonomy']);
                        }
                    }
                );

如此注册 custom post type 获取没有什么优势之处,但是我们可以把它换成这样

class wpdevil_CPT { 

    public function __construct() {
        //add_action('admin_head', array(&$this, 'cpt_icons'));
    }
    
    public function Add_CPT($post_type_args) {
        add_action( 'init', 
               function() use( $post_type_args ){
                        register_post_type($post_type_args['post_type'], array(
                           'labels' => array(
                               'name'           => $post_type_args['plural_name'],
                               'singular_name'  => $post_type_args['singular_name'],
                               'menu_name'      => $post_type_args['plural_name']
                           ),
                           'public'             => true,
                           'publicly_queryable' => true,
                           'show_ui'            => true,
                           'show_in_menu'       => true,
                           'show_in_nav_menus'  => $post_type_args['show_in_nav_menus'],
                           'query_var'          => true,
                           'rewrite'            => array('slug' => isset($post_type_args['rewrite_slug'])?$post_type_args['rewrite_slug']:$post_type_args['post_type']),
                           'capability_type'    => $post_type_args['capability'],
                           'exclude_from_search'=> $post_type_args['exclude_from_search'],
                           'has_archive'        => $post_type_args['has_archive'],
                           'hierarchical'       => $post_type_args['hierarchical'],
                           'supports'           => $post_type_args['supports']
                        ));

                        if ($post_type_args['taxonomy'] != '') {
                            $this->_add_taxonomy($post_type_args['post_type'],$post_type_args['taxonomy']);
                        }
                    }
                );
    }
    
    public function _add_taxonomy($post_type, $taxonomy) {
        foreach ($taxonomy as $t) {
            register_taxonomy($t['name'], $post_type, array(
                'hierarchical' => $t['hierarchical'],
                'labels' => array( 'name' => $t['label'], 'singular_name' => $t['label'], 'menu_name' => $t['label']),
                'public' => true,
                'show_in_nav_menus' => true,
                'show_ui' => true,
                'show_tagcloud' => true,
                'show_admin_column' => true,
                'hierarchical' => true,
                'rewrite' => true,
                'query_var' => true
            )); 
        }
    }

    public function cpt_icons() {
        $post_types = get_post_types(array( 'public'   => true,  '_builtin' => false), 'names' ); 
        //print_r($post_types);
        echo '<style type="text/css" media="screen">'."\n";
        foreach ( $post_types as $post_type ) {
           $icon = WPDEVIL_IMG_URI."/icons/" . $post_type . ".png";
           echo "\t#menu-posts-$post_type .wp-menu-image { background: url($icon) no-repeat 6px -17px !important; }#menu-posts-$post_type:hover .wp-menu-image {  background-position:6px 7px!important; }\n";
        }
        echo '</style>'."\n";
    }
}
$_cpt = new wpdevil_CPT(); 

$_cpt->Add_CPT(array(
            'post_type' => SLIDER_POST_TYPE_NAME,
            'plural_name' => 'Sliders',
            'singular_name' => 'Slider',
            'rewrite_slug'  => 'slider',
            'capability'    => 'post',
            'exclude_from_search'   => TRUE,
            'has_archive'   => FALSE,
            'hierarchical'  => FALSE,
            'supports'  => array( 'title', 'thumbnail', 'page-attributes' ),
            'show_in_nav_menus' => FALSE
           )); 
$_cpt->Add_CPT(array(
            'post_type' => 'download',
            'plural_name' => 'Downloads',
            'singular_name' => 'Download',
            'rewrite_slug'  => 'download',
            'capability'    => 'post',
            'exclude_from_search'   => TRUE,
            'has_archive'   => FALSE,
            'hierarchical'  => FALSE,
            'supports'  => array( 'title', 'editor', 'thumbnail', 'page-attributes' ),
            'show_in_nav_menus' => FALSE,
            'taxonomy' => array(
		array(
			'name'          => 'download_category',
			'hierarchical'  => true,
			'label'         => 'Download category',
			'labels'        => 'Download Categories',
			'slug'          => 'download_category'
		)
	))); 
//organization
$_cpt->Add_CPT(array(
            'post_type'     => 'organization',
            'plural_name'   => 'Organizations',
            'singular_name' => 'Organization',
            'rewrite_slug'  => 'organization',
            'capability'    => 'post',
            'exclude_from_search'   => TRUE,
            'has_archive'   => FALSE,
            'hierarchical'  => FALSE,
            'supports'      => array( 'title', 'editor', 'thumbnail', 'page-attributes' ),
            'show_in_nav_menus' => FALSE
           )); 

一个简单的面向对象的方法注册 custom post types.