Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress How to Build a WordPress Plugin Building a WordPress Plugin Settings Page Creating a Plugin Settings Page

So far I can't resolve this challenge:

Inside of the "my_plugin_menu()" function, write out the function to add a link for a plugin options page to the admin navigation. Then use the information in the comment to add the proper parameters to the newly added function.

function my_plugin_menu() {

    // Page Title: My Plugin Settings Page
    // Menu Title: My Plugin
    // Slug: Same as menu name, but with hyphens instead of spaces
    // Make sure user has ability to manage plugin options
    // my_plugin_options_page() is the function for what displays on the options page

    add_options_page(
    'My Plugin Setting Page',
    'My Plugin',
    'My_Plugin',


    );

  add_action( 'admin_menu', 'my_plugin_menu');

}

12 Answers

This is the answer I used to correctly answer this question:

function my_plugin_menu() {
    // Page Title: My Plugin Settings Page
    // Menu Title: My Plugin
    // Slug: Same as menu name, but with hyphens instead of spaces
    // Make sure user has ability to manage plugin options
    // my_plugin_options_page() is the function for what displays on the options page  
  add_options_page(
        'My Plugin Settings Page',
        'My Plugin',
        'manage_options',
        'my-plugin',
        'my_plugin_options_page'
    );  
}
function my_plugin_options_page() {
    if( !current_user_can( 'manage_options' ) ) {
        wp_die( 'You do not have suffiicient permissions to access this page.' );
    }
    echo '<p>Welcome to our plugin page!</p>';
}
?>
Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Check when you're calling the add_action function, it should not be nested inside of the function you're trying to reference.

The following works: <?php function my_plugin_menu() { // Page Title: My Plugin Settings Page // Menu Title: My Plugin // Slug: Same as menu name, but with hyphens instead of spaces // Make sure user has ability to manage plugin options // my_plugin_options_page() is the function for what displays on the options page
add_options_page( 'My Plugin Settings Page', 'My Plugin', 'manage_options', 'my-plugin', 'my_plugin_options_page' );
} function my_plugin_options_page() { if( !current_user_can( 'manage_options' ) ) { wp_die( 'You do not have suffiicient permissions to access this page.' ); } echo '<p>Welcome to our plugin page!</p>'; } add_action( 'admin_menu', 'my_plugin_menu');

?>

Danny Bakker
Danny Bakker
10,058 Points

You need to take out the if statement and the echo is wrong

Hi Ed,

There's a couple of parts to this challenge question.

In the first, you need to add the parameters given in the comment to the add_options_page function. Take a look at the add_options_page on the WordPress Codex. It takes 4 required parameters, and you have 3, one of which is incorrect. (Hint: hyphens aren't the same as underscores. :))

Second, the comment in the challenge states: "Make sure user has ability to manage plugin options." This is first done by adding the correct capability to the add_options_page parameters, and then by adding a function (the name is provided in the comments of the challenge) to check the user's current role and whether that role has the ability to mange plugin options.

Hope this helps reorient your thoughts about this challenge.

Xena, thanks for your answer. I dedicated many hours to get it right but so far I more confuse than before. I am pretty new in this php plugins avenue and I don't manage correctly yet the commands.

Thanks Zac your were right. I left a coma. I took away. But still: Wrong.

add_action( 'admin_menu', 'my_plugin_menu');

function my_plugin_menu() {

    // Page Title: My Plugin Settings Page
    // Menu Title: My Plugin
    // Slug: Same as menu name, but with hyphens instead of spaces
    // Make sure user has ability to manage plugin options
    // my_plugin_options_page() is the function for what displays on the options page

      add_options_page (
        'My Plugin Setting Page',
        'My Plugin',
                'manage_options',
        'My_Plugin',        
                'my_plugin_options_page()'
      );
}

I get "Bummer! It looks like you forgot to call add_options_page"

I see we are on the same page here. This got me through 1 and 2, but is failing 3

<?php 
  function my_plugin_menu() {

    // Page Title: My Plugin Settings Page
    // Menu Title: My Plugin
    // Slug: Same as menu name, but with hyphens instead of spaces
    // Make sure user has ability to manage plugin options
    // my_plugin_options_page() is the function for what displays on the options page
        add_options_page(
            'My Plugin Settings Page',
            'My Plugin',
            'manage_options',
            'my-plugin',
            'my_plugin_options_page',
            function my_plugin_options_page() {
                if( !current_user_can( 'manage_options' ) ) {
                    wp_die( 'You do not have suggicient permissions to access this page.' );
                }
                echo '<p>Welcome to my plugin settings page.</p>';
            }
        );
  }

  add_action( 'admin_menu', 'my_plugin_menu');
?>

Just cut out the if statement in the my_plugin_options_page function:

function my_plugin_options_page() {


    echo 'Welcome to my plugin settings page.';

}

add_action('admin_menu', 'my_plugin_menu'); 
?>

I did but no dice

<?php 
  function my_plugin_menu() {

    // Page Title: My Plugin Settings Page
    // Menu Title: My Plugin
    // Slug: Same as menu name, but with hyphens instead of spaces
    // Make sure user has ability to manage plugin options
    // my_plugin_options_page() is the function for what displays on the options page
        add_options_page(
            'My Plugin Settings Page',
            'My Plugin',
            'manage_options',
            'my-plugin',
            'my_plugin_options_page',
            function my_plugin_options_page() {
                echo '<p>Welcome to my plugin settings page.</p>';
            }
        );
  }

  add_action( 'admin_menu', 'my_plugin_menu');
?>

Still not working. I am stumped

Tim Herbert
Tim Herbert
8,634 Points

your really close just can't have the function inside your add_options_page call.

Thanks Tim Herbert