-
-
Save DavidPeralvarez/bf0cafd26f31b461ec56d57e3b637a87 to your computer and use it in GitHub Desktop.
Añadir sólo campos en una página y sección existente
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: API de Ajustes 2 | |
Description: Aprendiendo a usar la Settings API | |
Text Domain: api-ajustes | |
Domain Path: /languages | |
*/ | |
add_action( 'admin_init', 'dp_ada_init' ); | |
function dp_ada_init(){ | |
// Register the setting | |
register_setting( 'discussion', 'dp_ada_settings', 'dp_ada_sanitize_validate_settings' ); | |
// Add fields to our section | |
$settings = get_option('dp_ada_settings'); | |
add_settings_field( 'dp_ada_field_one', esc_html__('Edad', 'api-ajustes'), 'dp_ada_fields_callback', 'discussion', 'default', array( | |
'name' => 'dp_ada_settings[one]', | |
'value' => $settings['one'] | |
) ); | |
add_settings_field( 'dp_ada_field_two', esc_html__('Email', 'api-ajustes'), 'dp_ada_fields_callback', 'discussion', 'default', array( | |
'name' => 'dp_ada_settings[two]', | |
'value' => $settings['two'] | |
) ); | |
} | |
// Sanitize and validate settings | |
function dp_ada_sanitize_validate_settings( $input ){ | |
$output = get_option( 'dp_ada_settings' ); | |
// Sanear edad | |
$output['one'] = absint( $input['one'] ); | |
// Validar email | |
if( is_email( $input['two'] ) ): | |
$output['two'] = $input['two']; | |
else: | |
add_settings_error( 'dp_ada_settings', 'dp_ada_error_email', esc_html__( 'Debes introducir un email correcto.', 'api-ajustes' ) ); | |
endif; | |
return $output; | |
} | |
// Show section description | |
function dp_ada_section_callback(){ | |
esc_html_e( 'Texto de ayuda o descripción de la sección.', 'api-ajustes' ); | |
} | |
// Render the field | |
function dp_ada_fields_callback( $args ){ | |
echo '<input type="text" name="'.esc_attr( $args['name'] ).'" value="'.esc_attr( $args['value'] ).'">'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment