0

I have a PHP Class I created that handles creation of the custom post type, custom taxonomies, and meta boxes and custom metadata fields.

My next step is to add the ability to create subpages under the CPT's menu in the Admin backend. In order to do that, I have the following code that defines the method:

public function add_submenu_page( $title, $capability = 'administrator' ) {

        // Set variables
        $post_type = self::uglify( $this->post_type_name );
        $menu_title = self::beautify( $title );
        $menu_capability = $capability;
        $parent_slug = 'edit.php?post_type=' . $post_type;
        $menu_slug = self::uglify( $post_type ) . '_' . 'menu_' . self::uglify( $title );

        add_action( 'admin_menu', function( $parent_slug, $menu_title, $menu_capability, $menu_slug )  {
                add_submenu_page(
                    $parent_slug,
                    $menu_title,
                    $menu_title,
                    $menu_capability,
                    $menu_slug,
                    function() {
                        echo 'The Page Actually Worked!';
                    }
                );
        } );
}

And I call it using the following:

$cpt->add_submenu_page(
    'Options'
);

I did a test error_log() echo of the command and it came out how it was supposed to, but when I go ahead and actually implement the code in the method, I get a 500 Error when I attempt to access my local development host and the debug.log from WordPress has the following:

PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function JL_CustomPostType::{closure}(), 1 passed in D:\zSite\wpdev\wp-includes\class-wp-hook.php on line 286 and exactly 4 expected in D:\WordPress\Plugins\jl-cpt-casestudies\class-jl-custom-post-type.php:407

Am I missing something or does add_action() really not like anonymous functions?

0

the action admin_menu is called with one empty argument, then anonymous function can only be with 0 or 1 useless argument. and then if you want to pass variables to this function, you can try that :

public function add_submenu_page( $title, $capability = 'administrator' ) {

    $post_type = self::uglify( $this->post_type_name );


    $GLOBALS["MyPlugin_menu"] = [
        "post_type" => $post_type,
        "menu_title" => self::beautify( $title ),
        "menu_capability" => $capability,
        "parent_slug" => 'edit.php?post_type=' . $post_type,
        "menu_slug" => self::uglify( $post_type ) . '_' . 'menu_' . self::uglify( $title ),
    ];


    add_action("admin_menu", function() {

        add_submenu_page(
              $GLOBALS["MyPlugin_menu"]["parent_slug"]
            , $GLOBALS["MyPlugin_menu"]["menu_title"]
            , $GLOBALS["MyPlugin_menu"]["menu_title"]
            , $GLOBALS["MyPlugin_menu"]["menu_capability"]
            , $GLOBALS["MyPlugin_menu"]["menu_slug"]
            , function() {
                echo 'The Page Actually Worked!';
            }
        );


    });


}
  • I actually found a similar solution, but instead of using a global, it uses the use() function. – Cedon Dec 24 '17 at 23:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.