2017-06-28 13 views
-1

私はカスタムポスト型のwordpress関数とadd_actionフックをリサイクルしようとしています。しかし、私は関数 "cpt_function_name"の引数が不足しています。このコードの最後に、インスタンス化するときにクラスに渡す必要がある引数を渡そうとします。助けて?PHPクラスの関数の引数を外部に渡す

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

add_actionコールへのparamsを追加するため、その特定のアクションがadmin_init(https://codex.wordpress.org/Plugin_API/Action_Reference/admin_init)の場合とされない、それをサポートしなければなりません。

つまり、オブジェクト内の変数を設定して、パブリック関数から呼び出す必要があります。

この

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であなたの_construct機能を置き換えます

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

私はそれを引用符で '配列($この、 'cpt_function_name')'、であると仮定します。 –

+0

はい。申し訳ありません:)答えを更新しました –

関連する問題