Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Cómo crear múltiples campos [Enfoque 2]
<?php
/*
Plugin Name: API de Ajustes
Description: Aprendiendo a usar la Settings API
Text Domain: api-ajustes
Domain Path: /languages
*/
// Create new item in the admin menu
add_action( 'admin_menu', 'dp_ada_add_submenu' );
function dp_ada_add_submenu(){
add_options_page( esc_html__('API de Ajustes','api-ajustes') , esc_html__('API de Ajustes','api-ajustes'), 'manage_options', 'dp_ada_main_menu', 'dp_ada_main_menu_settings' );
}
// Render settings form
function dp_ada_main_menu_settings(){
?>
<div class="wrap">
<h2><?php esc_html_e( 'Página de Ajustes', 'api-ajustes' ); ?></h2>
<form action="options.php" method="post">
<?php
settings_fields( 'dp_ada_settings_group' );
do_settings_sections( 'dp_ada_main_menu' );
submit_button();
?>
</form>
</div>
<?php
}
add_action( 'admin_init', 'dp_ada_init' );
function dp_ada_init(){
// Register the setting
register_setting( 'dp_ada_settings_group', 'dp_ada_settings' );
// Create new section
add_settings_section( 'dp_ada_section', esc_html__('Título de la sección', 'api-ajustes'), 'dp_ada_section_callback', 'dp_ada_main_menu' );
// Add field to our section
add_settings_field( 'dp_ada_field_one', esc_html__('Primer campo', 'api-ajustes'), 'dp_ada_field_one_callback', 'dp_ada_main_menu', 'dp_ada_section' );
add_settings_field( 'dp_ada_field_two', esc_html__('Segundo campo', 'api-ajustes'), 'dp_ada_field_two_callback', 'dp_ada_main_menu', 'dp_ada_section' );
}
// 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_field_one_callback(){
echo '<input type="text" name="dp_ada_settings[one]" value="'.esc_attr( get_option('dp_ada_settings')['one'] ).'">';
}
function dp_ada_field_two_callback(){
$settings = get_option('dp_ada_settings');
$field2 = $settings['two'];
echo '<input type="text" name="dp_ada_settings[two]" value="'.esc_attr( $field2 ).'">';
}
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.