Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Añadir sólo campos en una página y sección existente
<?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
You can’t perform that action at this time.