Skip to content

Instantly share code, notes, and snippets.

@igmoweb
Created May 20, 2014 12:39
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 igmoweb/87f3b0870ac8028a71d1 to your computer and use it in GitHub Desktop.
Save igmoweb/87f3b0870ac8028a71d1 to your computer and use it in GitHub Desktop.
Woocommerce new setting class complete
<?php
class WC_Settings_Hola_Dolly extends WC_Settings_Page {
public function __construct() {
$this->id = 'hola-dolly';
$this->label = __( 'Hola Dolly', 'hola-dolly' );
// Añadir pestaña
add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_settings_page' ), 20 );
// Mostrar los campos
add_action( 'woocommerce_settings_' . $this->id, array( $this, 'output' ) );
// Guardar los cambios
add_action( 'woocommerce_settings_save_' . $this->id, array( $this, 'save' ) );
// Muestra nuestro campo especial
add_action( 'woocommerce_admin_field_holadolly_special_field', array( &$this, 'display_special_field' ) );
// Guarda nuestro campo especial
add_action( 'woocommerce_update_option_holadolly_special_field', array( $this, 'sanitize_special_field' ) );
}
public function display_special_field( $field ) {
$option_value = get_option( $field['id'], $field['default'] );
?>
<label for="<?php echo $field['id']; ?>" ><?php echo esc_html( $field['title'] ); ?></label><br/>
<textarea name="<?php echo $field['id']; ?>" id="<?php echo $field['id']; ?>"><?php echo esc_textarea( $option_value ); ?></textarea>
<?php
}
public function sanitize_special_field( $field ) {
$value = ! empty( $_POST[ $field['id'] ] ) ? stripslashes( $_POST[ $field['id'] ] ) : '';
update_option( $field['id'], $value );
}
public function get_settings() {
$settings = array(
array( 'title' => __( 'Opciones Generales', 'hola-dolly' ), 'type' => 'title', 'id' => 'holadolly_general_options', 'desc' => __( 'Nuestras opciones generales.', 'hola-dolly' ) ),
array(
'title' => __( 'Un número', 'hola-dolly' ),
'id' => 'holadolly_general_options_number',
'default' => 0,
'type' => 'number',
'desc_tip' => __( 'Pon un número por aquí', 'hola-dolly' )
),
array( 'type' => 'sectionend', 'id' => 'holadolly_general_options' ),
array( 'title' => __( 'Opciones Especiales', 'hola-dolly' ), 'type' => 'title', 'id' => 'holadolly_special_options', 'desc' => __( 'Nuestras opciones especiales.', 'hola-dolly' ) ),
// Aquí va nuestro campo especial encerrado en una nueva sección
array(
'title' => __( 'Campo especial', 'wookings' ),
'id' => 'holadolly_general_options_special',
'default' => '',
'type' => 'holadolly_special_field'
),
array( 'type' => 'sectionend', 'id' => 'holadolly_special_options' ),
);
return apply_filters( 'holadolly_woocommerce_holadolly_settings', $settings );
}
}
new WC_Settings_Hola_Dolly();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment