Create theme accessible variable that can be set in WordPress

I have been asked to create a dial on a wordpress website. The customer wants to be able to update the value of the gauge.

I have two options – I can either create something that sits alongside wordpress where they can update the value, or I can have it as part of wordpress, but I am not sure how to do that or how easy it would be.

Read More

Basically I just need a screen where an authenticated user can update a value. I then need to be able to read that value from one of the theme files (specifically the static front page theme file) and display it.

I am comfortable with PHP, but not familiar with wordpress – is there any easy way to do this, or will it make more sense to just do it separately?

Related posts

Leave a Reply

5 comments

  1. It should be pretty easy actually – WordPress provides the core of what you need to interact with the database.

    Here’s some sample code of a theme I built a few years back. There’s more to it than this, obviously you have to create the table at some point. But once that is in place, the global $wpdb is your gateway.

    function update_game($player, $field, $data)
    {
        global $wpdb;
    
        $table = $wpdb->prefix . "gameplay";
        $where = "tex_id=" . ($player);
    
        $sql = "UPDATE $table SET " .$field. "='" .$data. "' WHERE $where";
    
        $wpdb->query( $sql );
    
    } //End update_game
    

    Alternatively for just a one field, you could use the “options”. e.g.

    function clear_game()
    {
        update_option('tex_deal','w');
        update_option('tex_dealer','0');
        update_option('tex_actor','0');
        update_option('tex_candeal','0');
        update_option( 'tex_flopImg' , '' );
        update_option( 'tex_turnImg' , '' );
        update_option( 'tex_riverImg' , '' );
        update_option( 'tex_pot' , '0' );
        update_option( 'tex_bigbet' , '0' );
    
    }//end clear_game
    

    update_option() is/was in /wp-includes/function.php (I presume it’s still there… it’s been a while) and you would need to call an add_option before hand – get_option() will return the current value.

  2. The article in the Codex is outdated, Creating Options Page, but has two links to articles written by two great developers:

    • Handling plugins options in WordPress 2.8 with register setting, by Ozh

      In the upcoming WordPress 2.8 there’s an interesting function set meant to help authors manage their plugin custom options. In a nutshell: whitelist your options, define how you want them to be validated, and just lean back and rely on the API to handle everything for you.

    • WordPress Settings API Tutorial, by Otto

      When writing the Simple Facebook Connect plugin, I investigated how the Settings API worked. It’s relatively new to WordPress (introduced in version 2.7), and many things I read said that it was much easier to use.
      It is much easier to use in that it makes things nice and secure almost automatically for you. No confusion about nonces or anything along those lines. However, it’s slightly more difficult to use in that there’s very little good documentation for it. Especially for the most common case: Making your own settings page.

    Note that you are not limited only to “Options Page”, there are many more. Then, it’s a matter of retriving the values in the frontend with the Options API.

  3. You could also use the Advanced Custom Fields Plugin Drop-In “Options Page”.

    It provides you with the ability to edit the options Pages without needing to code.
    Plus, after finishing developement, you can still export all the fields to php.

    Plugin Link