Ver Mensaje Individual
  #1 (permalink)  
Antiguo 12/10/2012, 08:27
Avatar de rogertm
rogertm
Mod->Cuba
 
Fecha de Ingreso: julio-2005
Ubicación: /home/Cuba/Habana/rogertm/
Mensajes: 2.922
Antigüedad: 18 años, 9 meses
Puntos: 638
Problemas de validación y/o inserción en la BD. Setting API

Estoy haciendo un setting para un theme, algo parecido al que tiene Twentyeleven, pero una de las funciones no inserta los datos en la base de datos.

Lo que tengo es lo siguiente, una función que me permite escoger si en el header del theme aparecerá una imagen de cabecera o no y otra que habilitará unos links a redes sociales, twitter, facebook, etc...

La de la cabecera funciona bien, pero la otra no, aquí parte del código:
Código PHP:
Ver original
  1. <?php
  2. /**
  3.  * Register setting options
  4.  */
  5. add_action( 'admin_init', 'theme_register_setting_options_init' );
  6. function theme_register_setting_options_init(){
  7.     // Based on Twentyeleven WordPress Theme
  8.     register_setting( 'theme_options', 'theme_theme_options', 'theme_theme_options_validate' );
  9.  
  10.     // Register our settings field group
  11.     add_settings_section( 'general', '', '__return_false', 'theme-options' );
  12.  
  13.     // Register our individual settings fields
  14.     add_settings_field( 'theme_header_stuff',    __( 'Header Options', 't_em' ),            'theme_settings_field_header_stuff',            'theme-options',    'general' );
  15.     add_settings_field( 'theme_social_stuff',    __( 'Social Network Options', 't_em' ),    'theme_settings_field_socialnetwork_stuff',    'theme-options',    'general' );
  16. }
  17.  
  18. /**
  19.  * Return an array of header options for Twenty'em
  20.  */
  21. function theme_header_options(){
  22.     $header_options = array (
  23.         'no-header-image' => array (
  24.             'value' => 'no-header-image',
  25.             'label' => __( 'No header image', 't_em' ),
  26.             'extend' => '',
  27.         ),
  28.         'header-image' => array (
  29.             'value' => 'header-image',
  30.             'label' => __( 'Header image', 't_em' ),
  31.             'extend' => theme_header_image_extend(),
  32.         ),
  33.     );
  34.  
  35.     return apply_filters( 'theme_header_options', $header_options );
  36. }
  37.  
  38. /**
  39.  * Return an array of social network options for Twenty'em
  40.  */
  41. function theme_socialnetwork_options(){
  42.     $socialnetwork_options = array (
  43.         'twitter-stuff' => array (
  44.             'value' => '',
  45.             'name' => 'twitter-stuff',
  46.             'label' => __( 'Twitter URL', 't_em' ),
  47.         ),
  48.         'facebook-stuff' => array (
  49.             'value' => '',
  50.             'name' => 'facebook-stuff',
  51.             'label' => __( 'Facebook URL', 't_em' ),
  52.         ),
  53.     );
  54.  
  55.     return apply_filters( 'theme_socialnetwork_options', $socialnetwork_options );
  56. }
  57. ?>
Además, tengo una función que me registra opciones por defecto, otra para obtener las opciones de la BD, otra por cada setting para mostrarlos en el administrador del theme que se las muestro. Esta me imprime en pantalla tres campos de texto donde poner las URLs de las redes sociales:
Código PHP:
Ver original
  1. <?php
  2. /**
  3.  * Render the Socialnetwork setting field
  4.  */
  5. function theme_settings_field_socialnetwork_stuff(){
  6.     $options = theme_get_theme_options();
  7.     foreach ( theme_socialnetwork_options() as $social ) :
  8. ?>
  9.     <div class="layout text-option social">
  10.         <label>
  11.             <span><?php echo $social['label'];?></span>
  12.             <input type="text" name="theme_theme_options['<?php echo $social['name']; ?>']" value="<?php echo esc_attr( $options[$social['name']] ) ?>" />
  13.         </label>
  14.     </div>
  15. <?php
  16.     endforeach;
  17. }
  18. ?>
Y por último una función para validar los datos.
Código PHP:
Ver original
  1. <?php
  2. /**
  3.  * Sanitize and validate input. Accepts an array, return a sanitized array.
  4.  */
  5. function theme_theme_options_validate( $input ){
  6.     $output = $defaults = theme_get_default_theme_options();
  7.  
  8.     // Header stuff must be in our array of header stuff options
  9.     if ( isset( $input['header-stuff'] ) && array_key_exists( $input['header-stuff'], theme_header_options() ) ) :
  10.         $output['header-stuff'] = $input['header-stuff'];
  11.     endif;
  12.  
  13.     // Archive stuff must be in our array of archive stuff options
  14.     if ( isset( $input['archive-stuff'] ) && array_key_exists( $input['archive-stuff'], theme_archive_options() ) ) :
  15.         $output['archive-stuff'] = $input['archive-stuff'];
  16.     endif;
  17.  
  18.     return apply_filters( 'theme_theme_options_validate', $output, $input, $defaults );
  19. }
  20. ?>

El código completo del archivo es mucho más largo, pero me he centrado en solo lo más importante, además tengo un par de cosas más que no me insertan los datos en la BD, pero si resuelvo uno, ya seguro resuelvo lo demás.

Algo más, si elimino el tercer parámetro de la función register_setting(), que es un callback a la función de validación, entonces me guarda los datos, pero por ejemplo, en los campos donde se insertan las URLs de las redes sociales, me manda el siguiente error:
Cita:
Notice: Undefined index: twitter-stuff in /path/to/file/theme-options.php on line 398
que es
Código PHP:
Ver original
  1. <input type="text" name="theme_theme_options['<?php echo $social['name']; ?>']" value="<?php echo esc_attr( $options[$social['name']] ) ?>" />

Para hacer todo esto me estoy guiando por la Settings API y el archivo theme-options.php que viene en Twentyeleven

Gracias mil de antemano a todos los que me puedan dar una ayuda...
__________________
Friki y Blogger por Cuenta Propia:213
Twenty'em: Theming is Prose