在PHP类中的功能外部通过参数

  • 本文关键字:外部 参数 功能 PHP php oop
  • 更新时间 :
  • 英文 :


我正在尝试回收自定义帖子type 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' );

用于将params添加到add_action呼叫中,该特定操作必须支持它,admin_init并非如此(https://codex.wordpress.org/plugin_api/actor_api/action_reference/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 with:

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

最新更新