-
-
Save DavidPeralvarez/1f53262c349cfe4b0f8a24e034fe4d5b to your computer and use it in GitHub Desktop.
Añadir secciones en una página 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( 'general', 'dp_ada_settings', 'dp_ada_sanize_validate_settings' ); | |
// Create new section | |
add_settings_section( 'dp_ada_section', esc_html__('Título de la sección', 'api-ajustes'), 'dp_ada_section_callback', 'general' ); | |
// Add field 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', 'general', 'dp_ada_section', 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', 'general', 'dp_ada_section', array( | |
'name' => 'dp_ada_settings[two]', | |
'value' => $settings['two'] | |
) ); | |
} | |
// Sanitize and validate settings | |
function dp_ada_sanize_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', 'error-email', esc_html__( 'El email introducido no es 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