I added a checkbox to the setting page like this:
function abc_render_admin(){
global $abc_options;
ob_start(); ?>
<form id="abc-form" action="" method="POST">
<div>
<?php $options = get_option('abc_options'); ?>
<p>
<input id="abc_settings[update_date]" name= "abc_settings[update_date]" type="checkbox" value="1" <?php checked('1', $abc_options['update_date']); ?>/>
<label class="description" for="abc_settings[update_date]">
<?php _e('Option 1', 'abc'); ?>
</label>
</p>
<button id="abc_submit" name="abc-submit" class="button-primary"> <?php _e('Submit', 'abc'); ?> </button>
</div>
</form>
<div id="abc_results"></div>
<?php echo ob_get_clean();
}
The checkbox display nicely on the admin page. However, I couldn't figure out the correct way to check if the checkbox is checked. I've tried this (not working):
function abc_process_ajax(){
if($abc_options['update_date'] == true){
echo 'Checked';
}
}
add_action('wp_ajax_abc_show_results', 'abc_process_ajax');
I've also tried doing print_r($abc_options); but got nothing. What is the correct way of checking a checkbox? I'm not very familiar with WordPress, any suggestions would be much appreciated. By the way, the Ajax is working fine. Here's the script:
function abc_load_scripts($hook){
global $abc_settings;
if($hook != $abc_settings)
return;
wp_enqueue_script('abc-ajax', plugin_dir_url(__FILE__) .'js/abc-ajax.js', array('jquery'));
wp_localize_script('abc-ajax', 'abc_vars', array(
'abc_nonce' => wp_create_nonce('abc-nonce')
)
);
}
add_action('admin_enqueue_scripts', 'abc_load_scripts');
abc-ajax.js
jQuery(document).ready(function($){
$('#abc-form').submit(function(){
data = {
action: 'abc_show_results',
abc_nonce: abc_vars.abc_nonce
};
$.post(ajaxurl, data, function(response){
$('#abc_results').html(response);
});
return false;
});
});
Aucun commentaire:
Enregistrer un commentaire