I'm creating a plugin with an admin settings page, and I want to use the settings API.
I am able to save text fields and checkboxes, but when I add a settings field with a wp_editor
it's not saving..?
I used to do this with get_option
, but now I want to use a settings class and the register_setting()
method.
This is my code:
public function register_settings() {
register_setting( 'eman_setting', 'eman_setting', array( $this, 'eman_validate_settings' ) );
add_settings_field(
'eman_dashboard_welcome',
__( "", 'emanlang' ),
array( $this, 'eman_dashboard_welcome_callback' ),
'management-settings',
'eman_settings_section'
);
}
public function eman_dashboard_welcome_callback() {
//$content = 'Empty';
$editor_id = 'textareadashboardwelcome';
$args = array('textarea_name' => 'eman_dashboard_welcome');
if(! empty( $this->eman_setting['eman_dashboard_welcome'] ) ) {
$content = $this->eman_setting['eman_dashboard_welcome'];
} else {
$content = 'The field is empty';
}
wp_editor( $content, $editor_id, $args );
/** TESTING **/
echo '<br /><b>testing textarea output: </b>'. $content .'<br /><br />';
echo '<b>Complete settings array dump: </b><br />';
echo var_dump($this->eman_setting);
}
Note: This is only the relevant part of the code. On this page I have multiple 'add_settings_field' which are all working fine.
As you may have noticed, for testing I do a var_dump()
to check what's inside the options array.
The dump returns:
array(3) { ["eman_opt_in"]=> string(2) "on" ["eman_sample_text"]=> string(10) "sample 1.1" ["eman_sample_text2"]=> string(10) "sample 2.2" }
After saving the form, the array only contains 3 fields, so the array is not even populated with the [eman_dashboard_welcome]?
I tried many possible solutions, like adding this jQuery:
$('#submit').mousedown( function() {
tinyMCE.triggerSave();
});
But nothing works... Please help :-)
Found it..
Solution:
Textarea / wp_editor 'name' needs to call the field in the array.
In my case:
$args = array('textarea_name' => 'eman_setting[eman_dashboard_welcome]');
wp_editor( $content, $editor_id, $args );