Quantcast
Channel: Default Route » programming
Viewing all articles
Browse latest Browse all 10

Custom theme options page in wordpress

$
0
0

Creating a custom theme options page is very simple if you follow these steps. In this lesson we will create a very basic options page.

Step 1: Init the options page.

In your functions.php file add this line to the top:

require_once ( get_template_directory() . '/theme-options.php' );

Step 2: Create theme-options.php

// The following line calls the function theme_options_init where we will register the options. It will also take care of storing the values

add_action( ‘admin_init’, ‘theme_options_init’ );

// The following line connects the admin_menu event to load up the HTML for the form

add_action( ‘admin_menu’, ‘theme_options_add_page’ );

// adds the theme options page to the Theme admin menu

function theme_options_add_page() {

add_theme_page( __( ‘Theme Options’ ), __( ‘Theme Options’ ), ‘edit_theme_options’, ‘theme_options’, ‘theme_options_html’ );

}

// This is the init which is used by POST

function theme_options_init(){

// this little bit of magic works similar to scaffolding in other popular frameworks. Inputs that have names ‘my_options[key]‘ will be automatically stored by wordpress. You can also use a filter function in the syntax. See wordpress codex for more info

register_setting( ‘my_options’, ‘my_options’ );

}

function theme_options_html() {

if ( ! isset( $_REQUEST['updated'] ) )

$_REQUEST['updated'] = false;

?>

<div class=”wrap”>

<?php screen_icon(); echo “<h2>” . get_current_theme() . __( ‘ Theme Options’ ) . “</h2>”; ?>

<?php if ( false !== $_REQUEST['updated'] ) : ?>

<div class=”updated fade”><p><strong><?php _e( ‘Options saved’ ); ?></strong></p></div>

<?php endif; ?>

<form method=”post” action=”options.php”>

<?php settings_fields( ‘my_options’ ); ?>

<?php $options = get_option( ‘my_options’ ); ?>

<input type=”text” name=”my_options[setting1]” id=”my_options[setting1]” value=”<?php echo $options['setting1'];?>” />

<input type=”submit” name=”save” value=”Save” />

</form>

</div>

<?php

}

?>

That’s it! Basic text inputs are very easy to do. Contact me if you need to do more complex upload such as images.


Viewing all articles
Browse latest Browse all 10

Latest Images

Trending Articles





Latest Images