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

Garrett Sanderson
Garrett Sanderson
12,735 Points

Need some extra explaination

Can someone help me out with understanding where the my_plugin_options is coming from or where is was created? I don't fully understand it.

<?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;
            $options['last_updated'] = time();
            update_option( 'my_plugin_options', $options );
        }


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

    }


?>

1 Answer

This function isn't coming from anywhere else, it exists here in the file it is created in. For example, if you went to the bottom of that file and created a function, you are declaring it so that is where it exists. There are times when in WordPress you will use hooks or filters to modify functions that exist elsewhere, but your code is simply adding a new function to WordPress. Does that make sense?

<?php
/* This creates a new function in my file */

function my_cool_function(){
  wp_die("You've declared a function here!");
}
?>
Garrett Sanderson
Garrett Sanderson
12,735 Points

I totally understand how function are working I am confused on this part

<?php update_option( 'my_plugin_options', $options ); ?>

I don't know where my_plugin_options is? Is that in the database and then we are assigning the $options variable to that table? I need clarification on that.

Sorry if my original question was too broad.

update_option is used to update the WordPress database:

<?php update_option( $option, $new_value, $autoload ); ?>

The first argument being passed in is the name of the option, in this case "my_plugin_option", the second part are the values for those options, in this case it is an array of values. Here is more on that specific function https://codex.wordpress.org/Function_Reference/update_option .

Garrett Sanderson
Garrett Sanderson
12,735 Points

Okay! Gotcha, that makes more sense now! Thank you so much for clarifying.