user1469270
user1469270

Reputation:

Create options menu in WordPress

I've got some code that creates an 'Options' menu in WordPress, from here. I'm using the documentated example, but I'm not sure how to make it functional. Ideally, I'd like to use the values the user has entered within the admin form elsewhere in the theme, like:

<?php echo_the_twitter_link(); ?>

Here is the current code:

<?php
/*
Plugin Name: Admin Menu
Plugin URI: x
Description: x
Author: x
Author URI: x
*/

// create custom plugin settings menu
add_action('admin_menu', 'baw_create_menu');

function baw_create_menu() {

    //create new top-level menu
    add_menu_page('BAW Plugin Settings', 'BAW Settings', 'administrator', __FILE__, 'baw_settings_page');

    //call register settings function
    add_action( 'admin_init', 'register_mysettings' );
}


function register_mysettings() {
    //register our settings
    register_setting( 'baw-settings-group', 'new_option_name' );
    register_setting( 'baw-settings-group', 'some_other_option' );
    register_setting( 'baw-settings-group', 'option_etc' );
}

function baw_settings_page() {
?>
<div class="wrap">
<h2>Social Media Links</h2>

<form method="post" action="options.php">
    <?php settings_fields( 'baw-settings-group' ); ?>
    <table class="form-table">
        <tr valign="top">
        <th scope="row">New Option Name</th>
        <td><input type="text" name="new_option_name" value="<?php echo get_option('new_option_name'); ?>" /></td>
        </tr>

        <tr valign="top">
        <th scope="row">Some Other Option</th>
        <td><input type="text" name="some_other_option" value="<?php echo get_option('some_other_option'); ?>" /></td>
        </tr>

        <tr valign="top">
        <th scope="row">Options, Etc.</th>
        <td><input type="text" name="option_etc" value="<?php echo get_option('option_etc'); ?>" /></td>
        </tr>
    </table>

    <p class="submit">
    <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
    </p>

</form>
</div>
<?php } ?>

Upvotes: 4

Views: 241

Answers (1)

danyo
danyo

Reputation: 5846

To use WordPress options anywhere in your theme, simply call the option name like this:

echo get_option('some_other_option');

I hope this helps and is what you need?

Upvotes: 1

Related Questions