Skip to content

Commit

Permalink
add support for data attributes on text, textarea, and select fields
Browse files Browse the repository at this point in the history
  • Loading branch information
raideus committed Apr 3, 2014
1 parent 45b120d commit 392f2cc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
43 changes: 26 additions & 17 deletions wpas-field.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
private $id;
private $name;
private $classes;
private $attributes;
private $label;
private $type;
private $format;
Expand Down Expand Up @@ -37,6 +38,10 @@ function __construct($field_name, $args = array()) {
$this->classes = ' ' . $this->classes;
}

if (!empty($attributes) && is_array($attributes)) {
$this->attributes = $attributes;
}

$this->label = $label;
$this->type = $type;
$this->format = $format;
Expand Down Expand Up @@ -132,24 +137,18 @@ function build_field() {

function select($multi = false) {

if ($multi) {
$multiple = ' multiple="multiple"';
} else {
$multiple = '';
}

$output = '<select id="'.$this->id.'" name="'.$this->name;
if ($multi) {
$output .= '[]';
}
$output .= '"'.$multiple.' class="';

if ($multi) {
$output .= 'wpas-multi-select';
} else {
$output .= 'wpas-select';
}
$output .= $this->classes.'">';
$output .= '"';
$output .= ($multi) ? ' multiple="multiple"' : '';
$output .= ' class="';
$output .= ($multi) ? 'wpas-multi-select' : 'wpas-select';
$output .= ' ' . $this->classes.'"';
$output .= $this->add_attributes();
$output .= '>';

foreach ($this->values as $value => $label) {
if (in_array($value,$this->exclude)) continue;
Expand Down Expand Up @@ -225,7 +224,7 @@ function text() {
$placeholder = '';
if ($this->placeholder)
$placeholder = ' placeholder="'.$this->placeholder.'"';
$output = '<input type="text" id="'.$this->id.'" class="wpas-text'.$this->classes.'" value="'.$value.'" name="'.$this->name.'"'.$placeholder.'>';
$output = '<input type="text" id="'.$this->id.'" class="wpas-text'.$this->classes.'" value="'.$value.'" name="'.$this->name.'"'.$placeholder.' '.$this->add_attributes().'>';
return $output;
}

Expand All @@ -246,12 +245,12 @@ function textarea() {
$placeholder = '';
if ($this->placeholder)
$placeholder = ' placeholder="'.$this->placeholder.'"';
$output = '<textarea id="'.$this->id.'" class="wpas-textarea'.$this->classes.'" name="'.$this->name.'"'.$placeholder.'>'.$value.'</textarea>';
$output = '<textarea id="'.$this->id.'" class="wpas-textarea'.$this->classes.'" name="'.$this->name.'"'.$placeholder.' '.$this->add_attributes().'>'.$value.'</textarea>';
return $output;
}

function submit() {
$output = '<input type="submit" class="wpas-submit'.$this->classes.'" value="'.esc_attr($this->values).'">';
$output = '<input type="submit" class="wpas-submit'.$this->classes.'" value="'.esc_attr($this->values).'" '.$this->add_attributes().'>';
return $output;
}

Expand All @@ -266,7 +265,17 @@ function hidden() {
$value = reset($value);
}
$value = esc_attr($value);
$output = '<input type="hidden" name="'.$this->name.'" value="'.$value.'">';
$output = '<input type="hidden" name="'.$this->name.'" value="'.$value.'" '.$this->add_attributes().'>';
return $output;
}

function add_attributes() {
$output = "";
if ($this->attributes) {
foreach($this->attributes as $k => $v) {
$output .= $k . '="'.$v.'" ';
}
}
return $output;
}

Expand Down
2 changes: 1 addition & 1 deletion wpas.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* @author Sean Butze
* @link https://github.com/growthspark/wp-advanced-search
* @version 1.1.2
* @version 1.2
* @license MIT
*/

Expand Down

0 comments on commit 392f2cc

Please sign in to comment.