• Hello,

    I want to build a custom survey and display his results in the wordpress backend.

    I added a new admin menu item like this:

    add_action( 'admin_menu', 'register_survey' );
    
    function register_survey(){
        add_menu_page( 'Survey', 'Survey', 'manage_options', '/survey/survey-admin.php', '', '/wp-admin/survey/icon.png', 30 );
    }

    The menu item named Survey appears in the backend. When i click it goes to /wp-admin/survey/survey-admin.php displaying my php file contents, but the dashboard dissapears with the sidebar menus… This page is also visible to everyone and wordpress function is_admin doesn’t work anymore in this page…

    What’s the correct solution? I just want to display my custom page on wordpress backend and be able to check if the user is admin or not. Do i need to create a plugin for this?

    Thanks!

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    There are a few backend things you can do from a plugin but not from a theme’s functions.php. I don’t think this is one of them.

    The first thing your page should do is wp_validate_auth_cookie(). This ensures the user is logged in and throws a fatal error if anyone tries to directly load the page. It must be redirected from another WP page to work at all. You can then check capabilities and/or roles of the user for further access control.

    I’m not sure why the backend menu stuff disappeared, it may be the file outputs a full html page with doctype and head section etc.? The page should only output HTML required for displaying the actual page content. It should not output anything else like headers, footers, menus, body tags etc.

    BTW, is_admin() returns a boolean value indicating if a backend page is being displayed or not. It has nothing to do with the admin role or user.

Viewing 1 replies (of 1 total)
  • The topic ‘Add custom page to admin menu visible only to admins’ is closed to new replies.