Skip to content

Commit

Permalink
Added two new fields
Browse files Browse the repository at this point in the history
File field uses thickbox media uploader and simply copies the url back
to the input field.
Password field outputs a simple password field.  Security on field is
minimal, password is output back to the field value after saving.
  • Loading branch information
jeremyclark13 committed Mar 21, 2013
1 parent e38e7c8 commit 200ba1c
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 0 deletions.
61 changes: 61 additions & 0 deletions class.settings-api.php
Expand Up @@ -32,9 +32,21 @@ class WeDevs_Settings_API {
private static $_instance;

public function __construct() {
add_action('admin_enqueue_scripts', array(&$this, 'admin_enqueue_scripts'));

}

/**
* Enqueue scripts and styles
*/
function admin_enqueue_scripts() {
wp_enqueue_style('thickbox');

wp_enqueue_script('jquery');
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
}

/**
* Set settings sections
*
Expand Down Expand Up @@ -261,7 +273,56 @@ function callback_wysiwyg( $args ) {

echo sprintf( '<br><span class="description"> %s</span>', $args['desc'] );
}

/**
* Displays a file upload field for a settings field
*
* @param array $args settings field args
*/
function callback_file( $args ) {

$value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
$size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
$id = $args['section'] . '[' . $args['id'] . ']';
$js_id = $args['section'] . '\\\\[' . $args['id'] . '\\\\]';
$html = sprintf( '<input type="text" class="%1$s-text" id="%2$s[%3$s]" name="%2$s[%3$s]" value="%4$s"/>', $size, $args['section'], $args['id'], $value );
$html .= '<input type="button" class="button wpsf-browse" id="'. $id .'_button" value="Browse" />
<script type="text/javascript">
jQuery(document).ready(function($){
$("#'. $js_id .'_button").click(function() {
tb_show("", "media-upload.php?post_id=0&amp;type=image&amp;TB_iframe=true");
window.original_send_to_editor = window.send_to_editor;
window.send_to_editor = function(html) {
var imgurl = $("img",html).attr("src");
$("#'. $js_id .'").val(imgurl);
tb_remove();
window.send_to_editor = window.original_send_to_editor;
};
return false;
});
});
</script>';
$html .= sprintf( '<span class="description"> %s</span>', $args['desc'] );

echo $html;
}

/**
* Displays a password field for a settings field
*
* @param array $args settings field args
*/
function callback_password( $args ) {

$value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
$size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';

$html = sprintf( '<input type="password" class="%1$s-text" id="%2$s[%3$s]" name="%2$s[%3$s]" value="%4$s"/>', $size, $args['section'], $args['id'], $value );
$html .= sprintf( '<span class="description"> %s</span>', $args['desc'] );

echo $html;
}

/**
* Get the value of a settings field
*
Expand Down
42 changes: 42 additions & 0 deletions procedural-example.php
Expand Up @@ -82,6 +82,20 @@ function wedevs_admin_init() {
'yes' => 'Yes',
'no' => 'No'
)
),
array(
'name' => 'password',
'label' => __( 'Password', 'wedevs' ),
'desc' => __( 'Password description', 'wedevs' ),
'type' => 'password',
'default' => ''
),
array(
'name' => 'file',
'label' => __( 'File', 'wedevs' ),
'desc' => __( 'File description', 'wedevs' ),
'type' => 'file',
'default' => ''
)
),
'wedevs_advanced' => array(
Expand Down Expand Up @@ -137,6 +151,20 @@ function wedevs_admin_init() {
'yes' => 'Yes',
'no' => 'No'
)
),
array(
'name' => 'password',
'label' => __( 'Password', 'wedevs' ),
'desc' => __( 'Password description', 'wedevs' ),
'type' => 'password',
'default' => ''
),
array(
'name' => 'file',
'label' => __( 'File', 'wedevs' ),
'desc' => __( 'File description', 'wedevs' ),
'type' => 'file',
'default' => ''
)
),
'wedevs_others' => array(
Expand Down Expand Up @@ -190,6 +218,20 @@ function wedevs_admin_init() {
'yes' => 'Yes',
'no' => 'No'
)
),
array(
'name' => 'password',
'label' => __( 'Password', 'wedevs' ),
'desc' => __( 'Password description', 'wedevs' ),
'type' => 'password',
'default' => ''
),
array(
'name' => 'file',
'label' => __( 'File', 'wedevs' ),
'desc' => __( 'File description', 'wedevs' ),
'type' => 'file',
'default' => ''
)
)
);
Expand Down
42 changes: 42 additions & 0 deletions settings-api.php
Expand Up @@ -118,6 +118,20 @@ function get_settings_fields() {
'yes' => 'Yes',
'no' => 'No'
)
),
array(
'name' => 'password',
'label' => __( 'Password', 'wedevs' ),
'desc' => __( 'Password description', 'wedevs' ),
'type' => 'password',
'default' => ''
),
array(
'name' => 'file',
'label' => __( 'File', 'wedevs' ),
'desc' => __( 'File description', 'wedevs' ),
'type' => 'file',
'default' => ''
)
),
'wedevs_advanced' => array(
Expand Down Expand Up @@ -173,6 +187,20 @@ function get_settings_fields() {
'yes' => 'Yes',
'no' => 'No'
)
),
array(
'name' => 'password',
'label' => __( 'Password', 'wedevs' ),
'desc' => __( 'Password description', 'wedevs' ),
'type' => 'password',
'default' => ''
),
array(
'name' => 'file',
'label' => __( 'File', 'wedevs' ),
'desc' => __( 'File description', 'wedevs' ),
'type' => 'file',
'default' => ''
)
),
'wedevs_others' => array(
Expand Down Expand Up @@ -226,6 +254,20 @@ function get_settings_fields() {
'yes' => 'Yes',
'no' => 'No'
)
),
array(
'name' => 'password',
'label' => __( 'Password', 'wedevs' ),
'desc' => __( 'Password description', 'wedevs' ),
'type' => 'password',
'default' => ''
),
array(
'name' => 'file',
'label' => __( 'File', 'wedevs' ),
'desc' => __( 'File description', 'wedevs' ),
'type' => 'file',
'default' => ''
)
)
);
Expand Down

0 comments on commit 200ba1c

Please sign in to comment.