为PHP类中的函数传递外部参数

时间:2017-06-28 13:55:56

标签: php oop

我正在尝试回收自定义帖子类型wordpress函数和add_action挂钩。但是我一直在为我的函数" cpt_function_name"错过参数。在这段代码的底部,我尝试传递我需要在实例化时传递给类的参数。帮助

class classCPT
{
  public $singular;
  public $plural;
  public $cpt_name;
  public $cpt_icon;

  public function __construct() {
    add_action( 'admin_init', array( $this, cpt_function_name ) );
  }

  public function cpt_function_name( $singular, $plural, $cpt_name, $cpt_icon ) {

    $labels = array(
        'name'                          => $plural,
        'singular_name'             => $singular,
        'add_name'                    => 'Add New',
        'add_new_item'              => 'Add New ' . $singular,
        'edit'                          => 'Edit',
        'edit_item'                   => 'Edit ' . $singular,
        'new_item'                    => 'New ' . $singular,
        'view'                          => 'View ' . $singular,
        'view_item'                   => 'View ' . $singular,
        'search_term'               => 'Search ' . $singular,
        'parent'                        => 'Parent ' . $singular,
        'not_found'                   => 'No ' . $plural . ' found',
        'not_found_in_trash'      => 'No ' . $plural . ' in Trash',
    );

    $args = array(
        'labels'                => $labels,
        'public'                        => true,
        'publicly_queryable'      => true, //part of WP query
        'exclude_from_search'     => false,
        'show_in_nav_menus'       => true,
        'show_ui'                       => true,
        'show_in_menu'              => true,
        'show_in_admin_bar'       => true,
        'menu_position'             => 20,
        'menu_icon'                   => $cpt_icon,
        'can_export'                  => true,
        'delete_with_user'        => false,
        'hierarchical'              => false,
        'has_archive'                 => true,
        'query_var'                   => true,
        'capability_type'           => 'post',
        'map_meta_cap'              => true,
      'taxonomies'            => array( '' ),
        'rewrite'                       =>array( 'slug' => $cpt_name, 'with_front' => true, 'pages' => true, 'feeds' => true, ),
        'supports'                   =>array( 'title', 'editor', 'thumbnail', ),
    );

    register_post_type( $cpt_name , $args);
  }

}

$newCPT = new classCPT( 'Safari', 'Safaris', 'safaris', 'dashicons-palmtree' );

1 个答案:

答案 0 :(得分:1)

要将params添加到add_action调用,该特定操作必须支持它,而admin_init(https://codex.wordpress.org/Plugin_API/Action_Reference/admin_init)则不是这样。

话虽这么说,你必须在对象中设置变量,然后从公共函数中调用它们。

用这个

替换你的_construct函数
public function __construct($singular, $plural, $cpt_name, $cpt_icon) {
    $this->singular = $singular;
    $this->plural= $plural;
    $this->cpt_name= $cpt_name;
    $this->cpt_icon= $cpt_icon;
    add_action( 'admin_init', array( $this, 'cpt_function_name' ) );
}

和你的cpt_function_name:

public function cpt_function_name(  ) {

$labels = array(
    'name'                          => $this->plural,
    'singular_name'             => $this->singular,
    'add_name'                    => 'Add New',
    'add_new_item'              => 'Add New ' . $this->singular,
    'edit'                          => 'Edit',
    'edit_item'                   => 'Edit ' . $this->singular,
    'new_item'                    => 'New ' . $this->singular,
    'view'                          => 'View ' . $this->singular,
    'view_item'                   => 'View ' . $this->singular,
    'search_term'               => 'Search ' . $this->singular,
    'parent'                        => 'Parent ' . $this->singular,
    'not_found'                   => 'No ' . $this->plural . ' found',
    'not_found_in_trash'      => 'No ' . $this->plural . ' in Trash',
);

$args = array(
    'labels'                => $labels,
    'public'                        => true,
    'publicly_queryable'      => true, //part of WP query
    'exclude_from_search'     => false,
    'show_in_nav_menus'       => true,
    'show_ui'                       => true,
    'show_in_menu'              => true,
    'show_in_admin_bar'       => true,
    'menu_position'             => 20,
    'menu_icon'                   => $this->cpt_icon,
    'can_export'                  => true,
    'delete_with_user'        => false,
    'hierarchical'              => false,
    'has_archive'                 => true,
    'query_var'                   => true,
    'capability_type'           => 'post',
    'map_meta_cap'              => true,
  'taxonomies'            => array( '' ),
    'rewrite'                       =>array( 'slug' => $this->cpt_name, 'with_front' => true, 'pages' => true, 'feeds' => true, ),
    'supports'                   =>array( 'title', 'editor', 'thumbnail', ),
);

register_post_type( $this->cpt_name , $args);}