Antar Azid
Antar Azid

Reputation: 5

add_menu_page need help as it should work

Since a week ago as an amateur at making my own custom plugins I wanted to make a settings page for my plugin. After using some examples that should work on wordpress I am not having much success. A sub page works just fine but I want it to be a top level menu the only reason I can think of why this is not working is because of some access rights issue? (happend before).

So if someone could help me with this would be nice. Thanks in advance

I am not sure if I may link other sites/sources but here I go

The link below has the working example of a sub page https://wisdmlabs.com/blog/create-settings-options-page-for-wordpress-plugin/

As for the other example I am working with that should work just fine is this https://wpshout.com/wordpress-options-page/

<?php


add_action( 'admin_menu' , 'Counter_Opties_pagina_aanmaken');

function Counter_Opties_pagina_aanmaken(){
    $page_title = 'Counter';
    $menu_title = 'Counter Opties';
    $capability = 'edit_posts';
    $menu_slug  = 'Counter_pagina';
    $function   = 'Counter_pagina_inhoud';


    add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function );

}

function Counter_pagina_inhoud(){

    include 'Counter-Opties-file.php';

    }

This should generate at least the 'Counter Opties' in the sidebar of wordpress backend.

Upvotes: 0

Views: 615

Answers (1)

Daniel Vickers
Daniel Vickers

Reputation: 1084

So what I can see the issue is as follows:

  • The slug isn't alphanumeric or all lowercase

The function MUST be located in the root of the plugin in your "initiator" file which should have the same name as your plugin folder. See more here.

Notes:

  • I would use manage_options over edit_pages for the capability
  • I would use require_once instead of includes

More info on menu page function can be found here.

add_action( 'admin_menu' , 'Counter_Opties_pagina_aanmaken');

function Counter_Opties_pagina_aanmaken(){
    $page_title = 'Counter';
    $menu_title = 'Counter Opties';
    $capability = 'manage_options';
    $menu_slug  = 'counter-opties-file.php';
    $function   = 'Counter_pagina_inhoud';

    add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function );
}

function Counter_pagina_inhoud(){
    require_once plugin_dir_path( __FILE__ ) . 'counter-opties-file.php';
}

As you can see it's added it succesfully

Upvotes: 2

Related Questions