• Hi,

    I am trying to build my own options page and I’ve come across a stumbling block.

    I’ve got the code for everything except upload things. I’m not sure how it is done, I am hoping someone can help out, I know I need an extra case in for upload but dont know how to handle the upload location or anything like that.

    <?php
    $themename = "Functions Test";
    $shortname = "test";
    
    $options = array (
    
    	/* Use for Title */
    	array(	"name" => "Welcome Message",
    			"type" => "title"),
    
    	/* Use to start code */
    	array(	"type" => "open"),
    
    	/* Use for Text Field */
    	array(	"name" => "Text",
    			"id" => $shortname."_one_hour",
    			"std" => "",
    			"type" => "text"),
    
    	/* Use for Select */
    	array(	'name' => 'Select',
        		'desc' => '',
        		'id' => $my_shortname . '_alt_stylesheet',
        		'std' => 'default.css',
        		'type' => 'select',
        		'options' => 'Yes, No'),
    
      	/* Use for Upload */
      	array(  'name' => 'Image',
      		    'desc' => 'Upload a logo image to use.',
        		'id' => $my_shortname . '_logo',
        		'std' => '',
        		'type' => 'upload'),
    
    	/* Use for textarea */
    	array(	"name" => "Message",
    			"desc" => "Text to display as welcome message.",
                "id" => $shortname."_welcome_message",
                "type" => "textarea"),
    
    	/* Use for checkbox */
    	array(  "name" => "Disable Welcome Message?",
    			"desc" => "Check this box if you would like to DISABLE the welcome message.",
                "id" => $shortname."_welcome_disable",
                "type" => "checkbox",
                "std" => "false"),*/
    
    	/* Use to end code */
    	array(	"type" => "close")
    
    );
    
    /* Save the options if you click save and delete if delete is chosen, simples. */
    
    function mytheme_add_admin() {
    
        global $themename, $shortname, $options;
    
        if ( $_GET['page'] == basename(__FILE__) ) {
    
            if ( 'save' == $_REQUEST['action'] ) {
    
           	 	foreach ($options as $value) {
              		update_option( $value['id'], $_REQUEST[ $value['id'] ] ); }
    
    			foreach ($options as $value) {
    	        	if( isset( $_REQUEST[ $value['id'] ] ) ) { update_option( $value['id'], $_REQUEST[ $value['id'] ]  ); } else { delete_option( $value['id'] ); } }
                    header("Location: themes.php?page=functions.php&saved=true");
                    die;
    
            	} else if( 'reset' == $_REQUEST['action'] ) {
    
            	foreach ($options as $value) {
                	delete_option( $value['id'] ); }
    
                header("Location: themes.php?page=functions.php&reset=true");
                die;
    
            }
        }
    
        add_theme_page($themename." Options", "".$themename." Options", 'edit_themes', basename(__FILE__), 'mytheme_admin');
    
    }
    
    function mytheme_admin() {
    
        global $themename, $shortname, $options;
    
    	/* Messages to display saved and reset */
    
        if ( $_REQUEST['saved'] ) echo '<div id="message" class="updated fade"><p><strong>'.$themename.' settings saved.</strong></p></div>';
        if ( $_REQUEST['reset'] ) echo '<div id="message" class="updated fade"><p><strong>'.$themename.' settings reset.</strong></p></div>'; ?>
    
    <div id="icon-tools" class="icon32"><br /></div>
    <div class="wrap">
    
    <h2><?php echo $themename; ?> settings</h2>
    
    <form method="post">
    
    <?php foreach ($options as $value) { 
    
    	switch ( $value['type'] ) {
    
    		case "open":
    		?>
            <table width="100%" border="0" padding:10px;">
    
    		<?php break;
    
    		case "close":
    		?>
    
            </table><br />
    
    		<?php break;
    
    		case "title":
    		?>
    		<table width="100%" border="0" style="background-color:#e3e3e3; padding:5px 10px; margin-bottom: 10px;"><tr>
            	<td colspan="2"><h3 style="font-family:'Georgia','Times New Roman',Times,serif;"><?php echo $value['name']; ?></h3></td>
            </tr>
    
    		<?php break;
    
    		case 'text':
    		?>
    
            <tr>
                <td width="20%" rowspan="2" valign="middle"><strong><?php echo $value['name']; ?>:</strong></td>
                <td width="80%"><input style="width:200px;" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" type="<?php echo $value['type']; ?>" value="<?php if ( get_settings( $value['id'] ) != "") { echo get_settings( $value['id'] ); } else { echo $value['std']; } ?>" /></td>
            </tr>
    
            <tr>
                <td><small><?php echo $value['desc']; ?></small></td>
            </tr><tr><td colspan="2" style="margin-bottom:5px;border-bottom:1px dotted #000000;">&nbsp;</td></tr><tr><td colspan="2">&nbsp;</td></tr>
    
    		<?php
    		break;
    
    		case 'textarea':
    		?>
    
            <tr>
                <td width="20%" rowspan="2" valign="middle"><strong><?php echo $value['name']; ?></strong></td>
                <td width="80%"><textarea name="<?php echo $value['id']; ?>" style="width:400px; height:200px;" type="<?php echo $value['type']; ?>" cols="" rows=""><?php if ( get_settings( $value['id'] ) != "") { echo get_settings( $value['id'] ); } else { echo $value['std']; } ?></textarea></td>
    
            </tr>
    
            <tr>
                <td><small><?php echo $value['desc']; ?></small></td>
            </tr><tr><td colspan="2" style="margin-bottom:5px;border-bottom:1px dotted #000000;">&nbsp;</td></tr><tr><td colspan="2">&nbsp;</td></tr>
    
    		<?php
    		break;
    
    		case 'select':
    		?>
            <tr>
                <td width="20%" rowspan="2" valign="middle"><strong><?php echo $value['name']; ?></strong></td>
                <td width="80%"><select style="width:240px;" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>"><?php foreach ($value['options'] as $option) { ?><option<?php if ( get_settings( $value['id'] ) == $option) { echo ' selected="selected"'; } elseif ($option == $value['std']) { echo ' selected="selected"'; } ?>><?php echo $option; ?></option><?php } ?></select></td>
           </tr>
    
           <tr>
                <td><small><?php echo $value['desc']; ?></small></td>
           </tr><tr><td colspan="2" style="margin-bottom:5px;border-bottom:1px dotted #000000;">&nbsp;</td></tr><tr><td colspan="2">&nbsp;</td></tr>
    
    		<?php
            break;
    
    		case "checkbox":
    		?>
                <tr>
                <td width="20%" rowspan="2" valign="middle"><strong><?php echo $value['name']; ?></strong></td>
                    <td width="80%"><? if(get_settings($value['id'])){ $checked = "checked=\"checked\""; }else{ $checked = ""; } ?>
                            <input type="checkbox" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" value="true" <?php echo $checked; ?> />
                            </td>
                </tr>
    
                <tr>
                    <td><small><?php echo $value['desc']; ?></small></td>
               </tr><tr><td colspan="2" style="margin-bottom:5px;border-bottom:1px dotted #000000;">&nbsp;</td></tr><tr><td colspan="2">&nbsp;</td></tr>
    
            <?php break; } }?>
    
    /* The submit button */
    <p class="submit">
    <input name="save" type="submit" value="Save changes" />
    <input type="hidden" name="action" value="save" />
    </p>
    </form>
    
    /* The reset button */
    <form method="post">
    <p class="submit">
    <input name="reset" type="submit" value="Reset" />
    <input type="hidden" name="action" value="reset" />
    </p>
    </form>
    
    <?php
    }
    
    /* Add widgets */
    add_action('admin_menu', 'mytheme_add_admin'); ?>
    <?php
    if ( function_exists('register_sidebar') )
    	register_sidebar(array(
            'before_widget' => '<li>',
            'after_widget' => '</li>',
            'before_title' => '',
            'after_title' => '',
        ));
    
    ?>

    Thanks for your help,
    Steve

Viewing 4 replies - 1 through 4 (of 4 total)
  • I know this is an older post but does anyone have a solution for this? I am trying to accomplish the same thing, sort of like adding a custom header image to the options page.

    Thanks!

    After a couple of days of searching I found a very old post (2007) on the Custom Header WordPress API.

    Anyone know if this is still the best way to achieve the proper results? Also, will this work to add multiple images to different areas of a theme?

    I figured it out. It requires copying and editing the custom-header.php and theme.php files. If you change about 10 different variables you can actually create an entirely new image-upload option.

    This works for if you want to have multiple images that can be changed in the options menu.

    Copy the files, edit them, save them together in a new file, include it in the functions.php and your good to go!

    I am going to write up a lengthy article on what I did.

    (For my first time playing with PHP I think I did pretty good!!)

    Hi Bradley,

    I would be very interested in reading your article! 🙂

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Help with options page’ is closed to new replies.