Hola, @gigasize777. Te recomiendo que le eches un vistazo a la documentación de la Option API de WordPress. Veo que usas get_option pero no almacenas esa opción en la base de datos, por lo tanto el valor no se guarda. Algo de pseudocódigo (sin probar) sobre el tuyo sería así:
<?php
/*
Plugin Name: Plugin Mio
Description: Mi plugin
Author: Giga
Version: 1.0
*/
add_action( 'admin_menu', 'test_plugin_setup_menu', 'test_init2' );
function test_plugin_setup_menu() {
add_menu_page( 'Test Plugin Page', 'Test Plugin', 'manage_options', 'test-plugin', 'test_init', 'test_init2' );
}
function test_init() { ?>
<div class="wrap">
<h1>Plugin</h1>
<form method="post" action="options.php">
<input type="checkbox" name="test_init" value="1" <?php checked( 1, get_option( 'test_init2' ), true ); ?> />
<?php submit_button(); ?>
</form>
</div>
<?php
}
function test_init2() {
echo '<h1>Mensaje</h1>';
}
function register_option_in_db() {
add_option( 'test_init2', $_POST['test_init'], '', 'yes' );
}
add_action( 'admin_init', 'register_option_in_db' );
?>
Pruébalo y nos cuentas, un saludo.
No me sirvio me redirige a «wp-admin/options.php»
¿Has podido mirar la documentación que te pasé?
Si lo lei pero no me funciona me redirige hacia options.php, ando enredado, le he agregado
function settings_page()
{
register_option_in_db(«test_init»);
}
pero me sigue sin funcionar
Hola de nuevo, @gigasize777. En primer lugar tienes que entender que este foro es para resolver problemas, no para que los demás te hagamos las tareas (o el trabajo). Dicho esto, he cogido el código del enlace que te pasé y lo he modificado según tus necesidades.
Como verás, no es tan difícil, simplemente hay que entender para qué funciona cada cosa:
// create custom plugin settings menu
add_action( 'admin_menu', 'my_cool_plugin_create_menu' );
function my_cool_plugin_create_menu() {
// create new top-level menu
add_menu_page( 'My Cool Plugin Settings', 'Cool Settings', 'administrator', 'cool-settings', 'my_cool_plugin_settings_page' );
// call register settings function
add_action( 'admin_init', 'register_my_cool_plugin_settings' );
}
function register_my_cool_plugin_settings() {
// register our settings
register_setting( 'my-cool-plugin-settings-group', 'new_option_name' );
}
function my_cool_plugin_settings_page() {
?>
<div class="wrap">
<h1>Your Plugin Name</h1>
<form method="post" action="options.php">
<?php settings_fields( 'my-cool-plugin-settings-group' ); ?>
<?php do_settings_sections( 'my-cool-plugin-settings-group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">New Option Name</th>
<td><input type="checkbox" name="new_option_name" value="1" <?php checked( get_option( 'new_option_name' ), 1 ); ?> /></td>
</tr>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php } ?>
Espero que te sirva, un saludo.