0

I am new to OOP with WordPress and could use some help with debugging this issue. I have been searching for an answer and been trying to debug myself, but could use a hand. I am trying to create admin menu items and their pages, but I am getting the following error:

Whoops\Exception\ErrorException thrown with message "call_user_func_array() expects parameter 1 to be a valid callback, function 'create_admin_page' not found or invalid function name"

can anyone help point me in the right direction? Thanks in advance!

Here is my class.

namespace MyNamespace;


class MyClass {


    public function __construct() {
        add_action('admin_menu', array($this, 'create_menu_item'));
        //add_action('admin_bar_menu', 'remove_wp_logo', 999);
        //add_filter( 'admin_bar_menu', 'change_howdy', 25 );

        ddd($this->create_menu_item());


    }

//Get all pages

    public function get_all_pages() {
        $allpages = get_pages( array( 'post_type' => 'page', 'posts_per_page' => '-1') );
        return $allpages;

    }

    //Get Parent Page ID
    protected function get_location_id() {
        $parents = get_page_by_title('Locations');        
        return  $parents->ID;
    }

    //Get all locations excluding grandchildren
    protected function get_top_level_pages() {
        $children = get_page_children( $this->get_location_id(), $this->get_all_pages() );
        $locations = array();
        foreach ($children as $key => $value) {
            if ($this->get_location_id() == $value->post_parent) {
                $locations[$key] = $this->{$key} = $value;
            }
        }
        return $locations;
    }
    //Create top level menu item
    public function create_menu_item() {

        $locations = $this->get_top_level_pages();
        foreach ($locations as $key => $value) {
            add_menu_page( 
                $value->post_title, 
                $value->post_title, 
                'manage_options', 
                $value->post_name.'-admin-page.php', 
                'create_admin_page', //here is where I am calling the next function
                'dashicons-location-alt', 
                6  
            );

        }

    }

//Create Admin Page -----should be it's own class? 
    public function create_admin_page() {
        $children = get_page_children( $this->get_location_id(), $this->get_all_pages() );
        ?>
        <div class="wrap">
            <h2>Welcome Child Pages</h2>
        </div>
            <ul>
            <?php 
                if(!empty($children)){
                    echo '<ul>';
                    foreach($children as $child){
                        echo '<li><a href="'.get_permalink($child->ID).'">'.$child->post_title.'</a></li>';
                    }
                    echo '</ul>';
                }    
            ?>
            </ul>
        <?php

    }
0

On this line:

add_action('admin_menu', array($this, 'create_menu_item'));

You are correctly passing the create_menu_item method of the current class to callback argument of the add_action() function. You just need to apply the same principle for the callbacks in the add_menu_page() function:

add_menu_page( 
    $value->post_title, 
    $value->post_title, 
    'manage_options', 
    $value->post_name.'-admin-page.php', 
    array( $this, 'create_admin_page' ), // <-- Here
    'dashicons-location-alt', 
    6  
);

This will apply to any WordPress function that has a 'callback' as a parameter. You can read more about callbacks in PHP here.

| improve this answer | |
  • That worked perfectly! I have been reading about callables and callbacks, but it looks like I still have some studying to do. Thank you! – NateShumate Nov 2 '17 at 4:10

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.