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

Code Challenge: Create a global array named "options" and include it in the options page function.

I'm stuck on this challenge. I create a global option array using global $options; but am stuck on including it in the options page function.

4 Answers

Luke Wenke
Luke Wenke
32,294 Points
<?php
$options = array();

function my_plugin_options_page() {
    if( !current_user_can( 'manage_options' ) ) {
        wp_die( 'You do not have sufficient permissions to access this page.' );
    }

    global $options;

    if( isset( $_POST['my_plugin_hidden_field'] ) ) {           
        $my_plugin_username = esc_html( $_POST['my_plugin_username'] );

    }

    require('includes/page-wrapper.php');
}


?>
lyonel scapino
lyonel scapino
14,191 Points

why there? why not working at the beginning? (put the global variable at the beginning)

Luke Wenke
Luke Wenke
32,294 Points

$options = array(); should be declared at the start

Bradley Maravalli
Bradley Maravalli
8,927 Points

Thanks Luke! That did the trick.

<?php

  $options = array();

    function my_plugin_options_page() {

        global $options;    

        if( !current_user_can( 'manage_options' ) ) {
            wp_die( 'You do not have sufficient permissions to access this page.' );
        }

        if( isset( $_POST['my_plugin_hidden_field'] ) ) {           
            $my_plugin_username = esc_html( $_POST['my_plugin_username'] );

        }

        require('includes/page-wrapper.php');
    }

?>
Luke Wenke
Luke Wenke
32,294 Points

I think initially I put "global $options" in the wrong spot but here you can see where the right spot is:

http://teamtreehouse.com/library/crud-with-the-wordpress-options-table-part-2

Luke would you mind sharing your code with me? I still haven't gotten this down.