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

Laurie Gray
Laurie Gray
23,119 Points

Please help me solve simple php challenge :)

<?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');

}

add_action( 'admin_menu', 'add_options_page' );

?>

plugin.php <?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');

}

add_action( 'admin_menu', 'add_options_page' );

?>

edited to format first copy of the code.

Laurie Gray
Laurie Gray
23,119 Points

I have tried this code and it doesn't pass - it says pass the name of the function as your second argument. Sorry :( must be me!

8 Answers

Laurie Gray
Laurie Gray
23,119 Points
<?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'
      );

}
add_action( 'admin_menu' , 'my_plugin_menu');

function my_plugin_options_page(){
  echo 'Welcome to my plugin settings page.';
}

?>

Edited to format code. Please review edits for future reference and refer to the Markdown Cheatsheet linked on this page.

I have not taken this course or quiz, so I cannot give you the exact passing code. One observation is that your add_action is outside the method or function. My guess is that it should be inside the function. To do that, move it above the closing }.

Laurie Gray
Laurie Gray
23,119 Points

Mmmm also doesn't work.

Kevin Korte
Kevin Korte
28,149 Points

One think I noticed is your add_action should call the function name as a parameter, not the array inside the function as you currently have it.

The add_action should be outside of the function. Basically, when the WP initilzies the admin_menu action, it's going to go through and look for any add_action functions that pass that in as a parameter, and it's going to run whatever paramter as a function name you attach as the second parameter

*EDIT to adjust verbage

so the challenge is to pass the name of the function as the second argument. Those are terms of art. A function always has () in PHP and has some code inside like this:

<?php
function helloWorld() {
  return 'Hello World!';
}

echo helloWorld();

//output: Hello World!

An argument is something that gets passed to the function when it is called. In the following example, we are going to pass a name to the function when it is called:

<?php
function helloName($name) {
  return 'Hello, ' . $name;
}

echo helloName('Laurie');

//Output: Hello, Laurie
?>

Since it says to pass the name of the function as the second argument, your call for the function would look something like this:

<?php

add_action( 'admin_menu', my_plugin_menu() );

I hope this helps. You may want to take some of the PHP basic courses to learn the terminology.

EDITED to fix typo

Kevin Korte
Kevin Korte
28,149 Points

Yeah, verbiage isn't correct. I adjusted my response to match the verbage the WP Codex uses.

Anyway, the add_actions in this example would be add_action( 'admin_menu', 'my_plugin_menu').

All of that got me through the quiz linked to this question, so I'm not sure if that answer the question or not. More reading here: https://codex.wordpress.org/Function_Reference/add_action

So my 0 knowledge of WordPress answer was close. :)

Laurie Gray
Laurie Gray
23,119 Points

Ok guys, youll notice that my code is correct yet the quiz doesnt pass though right?

Laurie Gray
Laurie Gray
23,119 Points

That is the correct code - the wrong function was being called

So you got it fixed?

Laurie Gray
Laurie Gray
23,119 Points

Yes its solved thanks guys

Please mark what you consider to be the best answer.

Edited to move comment to correct answer.