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?