Skip to content

Instantly share code, notes, and snippets.

@miziomon
Created May 11, 2012 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miziomon/2660788 to your computer and use it in GitHub Desktop.
Save miziomon/2660788 to your computer and use it in GitHub Desktop.
Helper Class to render html form element width WordPress data
<?php
/*
* Mvd_FormFields
* Helper Class to render html form element width WordPress data
* NOTE: Designed for use with WordPress
* @author Maurizio Pelizzone
* @link http://maurizio.mavida.com
* @license: GPLv2 or later
* @License URI: http://www.gnu.org/licenses/gpl-2.0.html
* @version: 0.2
*/
if ( !class_exists( 'Mvd_FormFields' ) ):
Class Mvd_FormFields {
/*
*
*/
function input($id, $value='', $class='', $type='text' ) {
if ( isset($_POST[$id]) )
$value = ( $value == '' ? $_POST[$id] : $value);
return "<input type=\"$type\" id=\"$id\" name=\"$id\" class=\"$class\" value=\"$value\">";
}
/*
*
*/
function get_options( $data, $selected = '') {
foreach($data as $item) {
$selected_attribute = "";
if ( !is_array( $selected) ) {
$selected_attribute = ( $item == $selected ? " selected " : "" );
} else {
foreach( $selected as $selected_item ){
if ( $item == $selected_item ) {
$selected_attribute = " selected ";
break;
}
}
}
//$selected_attribute
$options .= "<option " . $selected_attribute . " value='" . $item . "'>" . $item . "</option>";
}
return $options;
}
/*
*
*/
function select($id, $data, $selected = '' , $class='') {
/* carico una combo a partire da un array */
$options = "";
$options .= "<option selected value=''>" . _(""). "</option>";
$selected = ( $selected == '' ? $_POST[$id] : $selected);
$options .= $this->get_options( $data, $selected );
return "<select id='$id' name='$id' class='$class' >$options</select>";
}
/*
*
*/
function combo_terms($id, $data, $selected = '' , $class='') {
/* carico una combo a partire da un array */
$options = "";
$options .= "<option value=''></option>";
foreach ( $data as $term ) {
//$options .= $link_before . "<a href='" . get_permalink( $post->ID) . "'>" . $post->post_title . "</a>" . $link_after;
$selected_attribute = ( $term->name == $selected ? " selected " : "" );
if ($selected_attribute == "") {
$selected_attribute = ( $term->slug == $selected ? " selected " : "" );
}
$options .= "<option " . $selected_attribute . " value='" . $term->name . "'>" . $term->name . "</option>";
}
return "<select id='$id' name='$id' class='$class' >$options</select><!-- " . $selected . " -->" ;
}
/*
*
*/
function combo_terms_slug($id, $data, $selected = '' , $class='') {
/* carico una combo a partire da un array */
$options = "";
$options .= "<option value=''></option>";
foreach ( $data as $term ) {
//$options .= $link_before . "<a href='" . get_permalink( $post->ID) . "'>" . $post->post_title . "</a>" . $link_after;
$selected_attribute = ( $term->slug == $selected ? " selected " : "" );
$options .= "<option " . $selected_attribute . " value='" . $term->slug . "'>" . $term->name . "</option>";
}
return "<select id='$id' name='$id' class='$class' >$options</select><!-- " . $selected . " -->" ;
}
/*
*
*/
function combo_posts($id, $data, $selected = '' , $class='') {
global $post;
$temp_post = $post;
/* carico una combo a partire da un array */
$options = "";
$options .= "<option value=''></option>";
if ($data->have_posts()) : while ($data->have_posts()) : $data->the_post();
//$options .= $link_before . "<a href='" . get_permalink( $post->ID) . "'>" . $post->post_title . "</a>" . $link_after;
$selected_attribute = ( $post->ID == $selected ? " selected " : "" );
// applico il filtro per gestione multilingua
$title = apply_filters('the_title', $post->post_title);
$options .= "<option " . $selected_attribute . " value='" . $post->ID . "'>" . $title . "</option>";
endwhile;
endif;
wp_reset_query();
$post = $temp_post;
return "<select id='$id' name='$id' class='$class' >$options</select>";
}
} // fine classe
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment