Skip to content

Instantly share code, notes, and snippets.

@geilt
Created April 16, 2013 21:11
Show Gist options
  • Save geilt/5399680 to your computer and use it in GitHub Desktop.
Save geilt/5399680 to your computer and use it in GitHub Desktop.
Theme Options for Multiple Slider images. Customize as needed!
var original_send_to_editor = "";
var modified_send_to_editor = "";
var formfield = '';
var hrefurl = '';
jQuery(document).ready( function() {
original_send_to_editor = window.send_to_editor;
modified_send_to_editor = function(html) {
hrefurl = jQuery('img',html).attr('src');
console.log(jQuery(html));
if(!hrefurl) {
hrefurl = jQuery(html).attr('href'); // We do this to get Links like PDF's
}
hrefurl = hrefurl.substr(hrefurl.indexOf('/',8)); // Skips "https://" and extracts after first instance of "/" for relative URL, ex. "/wp-content/themes/currentheme/images/etc.jpg"
console.log(hrefurl);
jQuery('.' + formfield).val(hrefurl);
tb_remove();
window.send_to_editor = original_send_to_editor;
};
jQuery('.simpul_meta_upload').click(function() {
window.send_to_editor = modified_send_to_editor;
formfield = jQuery(this).attr('data-input');
tb_show('Add File', 'media-upload.php?TB_iframe=true');
console.log(formfield);
return false;
});
} );
<?php
add_action( 'admin_init', 'theme_options_init' );
add_action( 'admin_menu', 'theme_options_add_page' );
/**
* Init plugin options to white list our options
*/
function theme_options_init(){
register_setting( 'coates_options', 'coates_theme_options', 'theme_options_validate' );
wp_enqueue_style('thickbox');
if(!wp_script_is('simpul_meta_upload')):
wp_enqueue_script( 'simpul_meta_upload', get_template_directory_uri() . '/js/simpul.meta.upload.js', array('jquery-ui-datepicker'));
endif;
if(!wp_script_is('media-upload')):
wp_enqueue_script('media-upload');
endif;
if(!wp_script_is('thickbox')):
wp_enqueue_script('thickbox');
endif;
}
/**
* Load up the menu page
*/
function theme_options_add_page() {
add_theme_page( __( 'Theme Options', 'coatestheme' ), __( 'Theme Options', 'coatestheme' ), 'edit_theme_options', 'theme_options', 'theme_options_do_page' );
}
/**
* Create the options page
*/
function theme_options_do_page() {
global $select_options, $radio_options;
if ( ! isset( $_REQUEST['settings-updated'] ) )
$_REQUEST['settings-updated'] = false;
?>
<div class="wrap">
<?php screen_icon(); echo "<h2>" . get_current_theme() . __( ' Theme Options', 'coatestheme' ) . "</h2>"; ?>
<?php if ( false !== $_REQUEST['settings-updated'] ) : ?>
<div class="updated fade"><p><strong><?php _e( 'Options saved', 'coatestheme' ); ?></strong></p></div>
<?php endif; ?>
<form method="post" action="options.php">
<?php settings_fields( 'coates_options' ); ?>
<?php $options = get_option( 'coates_theme_options' ); ?>
<?php
$fields = array(
'image' => 'src',
'image_link' => 'text',
'title' => 'text'
);
?>
<table class="form-table">
<th colspan="2"><h2>Home Slider Options</h2></th>
<tr valign="top"><th scope="row"><?php _e( 'Number of Sliders', 'coatestheme' ); ?></th>
<td>
<input id="coates_theme_options[slider_number]" class="regular-text" type="text" name="coates_theme_options[slider_number]" value="<?php esc_attr_e( $options['slider_number'] ); ?>" />
<label class="description" for="coates_theme_options[slider_number]"><?php _e( '', 'coatestheme' ); ?></label>
</td>
</tr>
<tr>
<td>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e( 'Save Options', 'coatestheme' ); ?>" />
</p>
</td>
</tr>
<?php
$j = 0;
for($i = 1; $i <= $options['slider_number']; $i++) :
?><th><h3>Slider <?php echo $i; ?></h3></th><?php
foreach($fields as $field => $type) :
switch($type) :
case 'text':
?>
<tr valign="top"><th scope="row"><?php _e( 'Slider ' . $i . " " . getLabel($field) . ": ", 'coatestheme' ); ?></th>
<td>
<input id="coates_theme_options[<?php echo $i ?>][<?php echo $field ?>]" class="regular-text" type="text" name="coates_theme_options[<?php echo $i ?>][<?php echo $field ?>]" value="<?php esc_attr_e( $options[$i][$field] ); ?>" />
<label class="description" for="coates_theme_options[<?php echo $i ?>][<?php echo $field ?>]"><?php _e( '', 'coatestheme' ); ?></label>
</td>
</tr>
<?php
break;
case 'src':
$j++;
?>
<tr valign="top"><th scope="row"><?php _e( 'Slider ' . $i . " " . getLabel($field) . ": ", 'coatestheme' ); ?></th>
<td>
<input id="coates_theme_options[<?php echo $i ?>][<?php echo $field ?>]" class="regular-text coates_theme_options_<?php echo $i ?>_<?php echo $field ?>" type="text" name="coates_theme_options[<?php echo $i ?>][<?php echo $field ?>]" value="<?php esc_attr_e( $options[$i][$field] ); ?>" />
<label class="description" for="coates_theme_options[<?php echo $i ?>][<?php echo $field ?>]"><?php _e( '', 'coatestheme' ); ?></label>
<button data-input="coates_theme_options_<?php echo $i ?>_<?php echo $field ?>" class="simpul_meta_upload button-secondary" name="coates_theme_options_src_<?php echo $i . '_' . $j ?>" type="button">Browse</button>
</td>
</tr>
<?php
break;
case 'textarea':
?>
<tr valign="top"><th scope="row"><?php _e( 'Slider ' . $i . " " . getLabel($field) . ": ", 'coatestheme' ); ?></th>
<td>
<textarea id="coates_theme_options[<?php echo $i ?>][<?php echo $field ?>]" class="large-text" cols="50" rows="10" name="coates_theme_options[<?php echo $i ?>][<?php echo $field ?>]"><?php echo esc_textarea( $options[$i][$field] ); ?></textarea>
<label class="description" for="coates_theme_options[<?php echo $i ?>][<?php echo $field ?>]"><?php _e( '', 'coatestheme' ); ?></label>
</td>
</tr>
<?php
break;
endswitch;
endforeach;?>
<tr>
<td>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e( 'Save Options', 'coatestheme' ); ?>" />
</p>
</td>
</tr><?php
endfor;
?>
</table>
</form>
</div>
<?php
}
function getLabel($key)
{
$glued = array();
if( strpos( $key, "-" ) ) $pieces = explode( "-", $key );
elseif( strpos( $key, "_" ) ) $pieces = explode( "_", $key );
else $pieces = explode(" ", $key);
foreach($pieces as $piece):
if($piece == "id"):
$glued[] = strtoupper($piece);
else:
$glued[] = ucfirst($piece);
endif;
endforeach;
return implode(" ", $glued);
}
/**
* Sanitize and validate input. Accepts an array, return a sanitized array.
*/
function theme_options_validate( $input ) {
global $select_options, $radio_options;
// Our checkbox value is either 0 or 1
if ( ! isset( $input['option1'] ) )
$input['option1'] = null;
$input['option1'] = ( $input['option1'] == 1 ? 1 : 0 );
// Say our text option must be safe text with no HTML tags
$input['sometext'] = wp_filter_nohtml_kses( $input['sometext'] );
// Our select option must actually be in our array of select options
if ( ! array_key_exists( $input['selectinput'], $select_options ) )
$input['selectinput'] = null;
// Our radio option must actually be in our array of radio options
if ( ! isset( $input['radioinput'] ) )
$input['radioinput'] = null;
if ( ! array_key_exists( $input['radioinput'], $radio_options ) )
$input['radioinput'] = null;
// Say our textarea option must be safe text with the allowed tags for posts
$input['sometextarea'] = wp_filter_post_kses( $input['sometextarea'] );
return $input;
}
// adapted from http://planetozh.com/blog/2009/05/handling-plugins-options-in-wordpress-28-with-register_setting/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment