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 CRUD with the WordPress Options Table: Part 1

Zander Curtis
Zander Curtis
10,634 Points

What's Wrong With This Code?

Not sure where I went wrong with this one Treehouse community. Anyone offer some help?

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

      $options['my_plugin_username']  = $my_plugin_username;

      $update_option( 'my_plugin_username' , $options );
    }
  }

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


?>

1 Answer

Erik McClintock
Erik McClintock
45,783 Points

Zander,

I'm not sure which task you're on in the challenge (I assume task three), but I see a few errors that will prevent your progression.

1) "global" is a keyword, and thus does not get a $ in front of it

You have:

$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 is a keyword - do not use a $
$global $options;

        // rest of code here

You want:

        $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.' );
        }

                // notice no $ on "global" keyword
                global $options;

        // rest of code here

2a) You don't have the UNIX timestamp added to the $options array under an index named 'last_updated', as you were instructed (and, in theory, had added if you are on task three) in task two

2b) You also do not want to use a $ on your call to the update_option() function (as it is a function)

2c) You need to add your array to the options table entry named 'my_plugin_options', as the instructions state

You have:

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

      $options['my_plugin_username']  = $my_plugin_username;

      // you're missing your timestamp!

      // you have a $ here - remove it!
      // you need to add the $options array to the instructed options table entry, called 'my_plugin_options', not 'my_plugin_username'
      $update_option( 'my_plugin_username' , $options );
    }

You want:

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

                        // we add the UNIX timestamp to an index named 'last_updated', as instructed
                        $options['last_updated'] = time();

                        // we do not add a $ at the front of a function call
                        // we add the $options array to the appropriate table entry, as instructed
                        update_option( 'my_plugin_options', $options );

        }

The full, correct code then is this:

$options = array();

    function my_plugin_options_page() {

    // no $ on the "global" keyword
    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'] );

                        // both values added to the $options array, as instructed
            $options['my_plugin_username'] = $my_plugin_username;
                        $options['last_updated'] = time();

                        // no $ on the function call, and we're adding the $options array to the appropriate table
                        update_option( 'my_plugin_options', $options );

        }

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

Erik