WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Help with my first plug in (5 posts)

  1. Bloke
    Member
    Posted 1 year ago #

    I am trying to write my first plug in. At this point I am trying to get the menu to load the admin pages I will use. It loads the menu but it doesn't work. I get this error when I press the navigation button.

    Warning: call_user_func_array() expects parameter 1 to be a valid callback, no array or string given in ....\wp-includes\plugin.php on line 403

    /*
    Plugin Name:
    Plugin URL:
    Version:
    Author:
    Description:
    */
    
    add_action('admin_menu', 'myLiterature_menu');
    
    function myLiterature_menu() {
    
        add_menu_page('addLiterature', 'Literature', 'manage_options', 'addLiterature',6);
        add_submenu_page('addLiterature', 'Literature','AddLiterature','manage_options', 'editLiterature',6);
    
    }
    
    function addLiterature() {
      //must check that the user has the required capability
        if (!current_user_can('manage_options'))
        {
          wp_die( __('You do not have sufficient permissions to access this page.') );
        }
    
       echo "<p>This is the page to add content</p>"; 
    
    }
    function editLiterature() {
        echo "<p>Edit page info here</p>"; 
    
    }
  2. Bloke
    Member
    Posted 1 year ago #

    I got it to run the first function. Now it won't display the sub menu button.

    `
    add_action('admin_menu', 'myLiterature_menu');
    function myLiterature_menu() {

    add_menu_page('addLiterature', 'Literature', 1, 'manage_options','addLiterature');
    add_submenu_page('addLiterature', 'Literature', 1,'manage_options','editLiterature');

    }

    function addLiterature() {

    echo "<p>This is the page to edit content</p>";

    }
    function editLiterature() {

    echo "<p>Edit page</p>";
    }'

  3. bcworkz
    Member
    Posted 1 year ago #

    You closed your code with an apostrophe instead of a backtick, so the forum's parser might have corrupted some of your code. But your parameters for the two menu page functions appear to be wrong. Check the order, number, and type of parameters carefully, it's easy to get them mixed up or forgotten. And the parameters are not exactly the same for each function, adding to the confusion. For example, the capability parameter is fourth in one and third in the other.

  4. Bloke
    Member
    Posted 1 year ago #

    Tried this and still can't get the sub menu to appear.

    add_action('admin_menu', 'myLiterature_menu');
    
    function myLiterature_menu() { 
    
        add_menu_page('addLiterature', 'Literature', 10,'manage_options', 'add-literature','addLiterature');
        add_submenu_page('addLiterature', 'EditLiterature' ,10,'manage_options','edit-literature', 'editLiterature');
    
    }
    
    function addLiterature() {
        global $title;
        ?>
        <div class="wrap">
        <h2><?php echo $title;?></h2>
        ... Functionality for adding content will go here ...
        <div id="icon-edit-pages" class="icon32"></div>
    </div><?php
    }?>
    
    <?php function editLiterature() {
    global $title;
        ?>
        <div class="wrap">
        <h2><?php echo $title;?></h2>
        ... Functionality for edit content will go here ...
    </div><?php
    }?>
  5. Bloke
    Member
    Posted 1 year ago #

    I finally got it to work after I figured the slug line and the order of things. Also it would say I didn't have permissions and I would have to sign out and back in to test it each time I changed the code.

    add_action('admin_menu', 'myLiterature_menu');
    
    function myLiterature_menu() { 
    
        add_menu_page('Literature','Literature',10,'get-literature','addLiterature');
        add_submenu_page('get-literature','Literature','Edit Literature',10,'edit-literature','editLiterature');
    
    }
    
    function addLiterature() {
        global $title;
        ?>
        <div class="wrap">
        <h2><?php echo $title;?></h2>
        Functionality for adding content will go here ...
        <div id="icon-edit-pages" class="icon32"></div>
    </div><?php
    }?>
    
    <?php function editLiterature() {
    global $title;
        ?>
        <div class="wrap">
        <h2><?php echo $title;?></h2>
        Functionality for editing content will go here ...
    </div><?php
    }?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.