I am creating a theme options page but with object oriented approach. Every new instance of this object is a separate subpage. Rendering and all the options work fine (still in progress, but in a nutshell it works fine), except the AJAX save.
I have placed in my __construct
method this
public function __construct( $subpage_name, $html_array ) {
$this->subpage_name = $subpage_name;
$this->html_array = $html_array;
add_action( 'admin_menu', array( $this, 'create_submenu_pages' ) );
add_action( 'wp_ajax_settings_save', array( $this, 'all_settings_save' ) );
}
And I have a method
public function all_settings_save() {
if ( isset( $_POST['data'], $_POST['settings_nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['settings_nonce'] ), 'settings_nonce_action' ) && '' !== $_POST['data'] ) { // Input var okay.
$settings = json_decode( sanitize_text_field( wp_unslash( $_POST['data'] ) ) ); // Input var okay.
update_option( str_replace( ' ', '_', strtolower( $this->subpage_name ) ), $settings );
}
wp_die();
}
When I click on the save button, my ajax works because I can see the data in the inspector (Network tab and then admin-ajax.php), for instance it shows that the form data is:
action:settings_save
data:{
"settings": {
"header_title": "Testing",
"logo": "",
"retina_logo": "",
"retina_logo_width": "23",
"retina_logo_height": "",
"transparent_logo": "",
"transparent_retina_logo": "",
"transparent_retina_logo_width": "",
"transparent_retina_logo_height": "",
"background_image": ""
}
}
settings_nonce:b7778588e3
So this should be stored in my database under, if it's 'Header Settings' page then it's header_settings
option in my wp_options
table. but when I reload the page nothing is inserted, because I have no row called header_settings
present in the wp_options
table.
I thought that the validation checks are failing, so I've removed them, but I still don't get a thing.
What am I doing wrong?
WP_DEBUG
set totrue
), run the code, checkwp-content/debug.log
, report back if you get anythingdefine('WP_DEBUG', true);
anddefine('WP_DEBUG_LOG', true);
but I don't have debug.log or error.log. I need to check the settings of my servererror_log( $message )
- maybe one at the start of the functionerror_log( 'Running handler...' )
, one inside theif
, and one just beforewp_die
- you'll then know if a) it's firing at all and b) if the code is running as you expected__construct()
method? Should I define a separate method and call it in the construct or something like that?