WordPress POST action is null - wordpress

I'm trying to make a simple login form for my WordPress theme. I thought I followed WP documentation correctly, but I'm seeing that my forms action is not being detected.
I can see in my error log 'got here 1' is printing. Looking at process_form.php below, this condition should indicate that my user is not logged in and that action is empty.
I believed that this line from my form processing code:
add_action( 'admin_post_nopriv_streamLogForm', 'showroom_login_user' );
and this line from my actual form
<input type="hidden" name="action" value="streamLogForm">
would interact to bind the form action to this admin_post hook.
The showroom_login_user() function works and my GET variables actually seem to be working (at first I thought they were not), but still not sure why I'm reaching this condition.
login.php
<head>
<script type="text/javascript">
function validateLoginForm() {
var email = document.forms["streamLogForm"]["user_email"].value;
var password = document.forms["streamLogForm"]["user_password"].value;
if ( email == null || email == "" ) {
alert("User must be entered");
return false;
} else if ( password == null || password == "" ) {
alert("Password must be entered");
return false;
}
return true;
}
</script>
</head>
<div class="">
<form name="streamLogForm" id="streamLoginForm" onsubmit="return validateLoginForm()" action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" method="get" >
<input type="hidden" name="action" value="streamLogForm">
<input type="hidden" name="location" value="<?php echo $URI ?>" />
<input type="hidden" name="action_type" value="<?php echo 'login'?>" />
<div class="field">
<label for="streamUserEmail">Email:</label>
<input type="text" id="streamUserEmail" name="user_email">
</div>
<div class="field">
<label for="streamUserPassword">Password: </label>
<input type="password" id="streamUserEmail" name="user_password">
</div>
<button> Login </button>
</form>
</div>
process_form.php
<?php
if ( ! wp_validate_auth_cookie() ) {
if ( empty( $action ) ) {
error_log('got here 1', 0);
do_action( 'admin_post_nopriv' );
} else {
error_log('got here 2', 0);
do_action( "admin_post_nopriv_{$action}" );
}
} else {
if ( empty( $action ) ){
error_log('got here 3', 0);
do_action( 'admin_post' );
} else {
error_log('got here 4', 0);
do_action( "admin_post_{$action}" );
}
}
function showroom_login_user()
{
error_log("GOT HERE 6!", 0);
$location = $_GET["action_type"];
$action_type = $_GET["action_type"];
$user_email = $_GET["user_email"];
$user_password = $_GET["user_password"];
echo 'testing......' . $user_email;
}
add_action( 'admin_post_nopriv_streamLogForm', 'showroom_login_user' );
add_action( 'admin_post_streamLogForm', 'showroom_login_user' );
?>

Update your form tag like this
<form name="streamLogForm" id="streamLoginForm" onsubmit="return validateLoginForm()" action="<?php echo admin_url('admin-ajax.php'); ?>" method="get" >
and place this snippet on your functions.php
function showroom_login_user() {
$creds = array();
$creds['user_login'] = $_GET["user_email"];
$creds['user_password'] = $_GET["user_password"];
$creds['remember'] = true;
$autologin_user = wp_signon( $creds, false );
if ( !is_wp_error($autologin_user) ) {
// "Logged In.!";
} else {
// "Log In Failed.!";
}
// do your redirect here
wp_safe_redirect( "URL of the page to be redirected" );
}
add_action ( 'wp_ajax_quick_streamLogForm', 'showroom_login_user' );
add_action ( 'wp_ajax_nopriv_streamLogForm', 'showroom_login_user' );

Related

Set Form action in wordpress

I am creating form for wordpress website. Currently i am getting value on same page successfully with blank action.
Now i need to get form value on data.php page.
Form
<form id="post-container" action="<?php echo home_url( '/' ); ?>data.php" method="POST">
<input name="aa" value="post_id"> ........ </input>
<input name="ab" value="post_id"> ........ </input>
<input type="submit" name="submit" value="submit">
</form>
data.php
<?php
if (isset ( $_POST['aa'] )){
$ID = $_POST['aa'] ;
echo get_post_meta( $ID, 'brand', true );
}
if (isset ( $_POST['ab'] )){
$ID = $_POST['ab'] ;
echo get_post_meta( $ID, 'brand', true );
}
?>
After submit
url = http://localhost/mobile/data.php which is correct.
but it display index.php which is wrong and also not print any value.
How to submit form to data.php and print value?
Wordpress have protection from POST, GET requests..
I have some links about it
wordpress-jquery-contact-form-without-a-plugin
create-a-submit-form-in-wordpress
add_query_arg
how-to-submit-a-form-in-wordpress

How to get the "really simple captcha" to work in a simple contact form?

I tried to create a simple contact form according to this tutorial
To avoid spam attacks I tried to combine that contact form with the really simple captcha plugin.
With the following code I'm able to use the shortcode [contact] in any of my pages and the form is displaying BUT there is a problem when checking the captcha.
This is the plugin's code:
function wptuts_get_the_ip() {
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
return $_SERVER["HTTP_X_FORWARDED_FOR"];
}
elseif (isset($_SERVER["HTTP_CLIENT_IP"])) {
return $_SERVER["HTTP_CLIENT_IP"];
}
else {
return $_SERVER["REMOTE_ADDR"];
}
}
// short code function
function wptuts_contact_form_sc( $atts ) {
add_shortcode( 'contact', 'contact_form_shortcode' );
extract(shortcode_atts(array(
"email" => get_bloginfo('admin_email'),
"subject" => '',
"label_name" => 'Your Name',
"label_email" => 'Your E-mail Address',
"label_subject" => 'Your Answer',
"label_captcha" => 'Captcha',
"label_submit" => 'Submit',
"error_empty" => 'Please fill in all the required fields.',
"error_noemail" => 'Please enter a valid e-mail address.',
"success" => 'Thanks for your e-mail! We\'ll get back to you as soon as we can.'
), $atts));
$captcha_instance = new ReallySimpleCaptcha(); //imports really simple captcha plugin
$word = $captcha_instance->generate_random_word();
$prefix = mt_rand();
$image= $captcha_instance->generate_image( $prefix, $word );
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$error = false;
$required_fields = array("your_name", "email", "captcha", "subject");
foreach ($_POST as $field => $value) {
if (get_magic_quotes_gpc()) {
$value = stripslashes($value);
}
$form_data[$field] = strip_tags($value);
}
foreach ($required_fields as $required_field) {
$value = trim($form_data[$required_field]);
if(empty($value)) {
$error = true;
$result = $error_empty;
}
}
if(!is_email($form_data['email'])) {
$error = true;
$result = $error_noemail;
}
if ($error == false) {
$captcha_text=$form_data['captcha'];
$correct = $captcha_instance->check( $prefix, $captcha_text );
if($correct==TRUE){
$email_subject = "[" . get_bloginfo('name') . "] " . $form_data['subject'];
$email_message = $form_data['captcha'] . "\n\nIP: " . wptuts_get_the_ip();
$headers = "From: ".$form_data['your_name']." <".$form_data['email'].">\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\n";
$headers .= "Content-Transfer-Encoding: 8bit\n";
wp_mail($email, $email_subject, $email_message, $headers);
$result = $success;
$sent = true;
}
}
}
if($result != "") {
$info = '<div class="info">'.$result.'</div>';
}
$email_form = '<form class="contact-form" method="post" action="'.get_permalink().'">
<div>
<label for="cf_name">'.$label_name.':</label>
<input type="text" name="your_name" id="cf_name" size="50" maxlength="50" value="'.$form_data['your_name'].'" />
</div>
<div>
<label for="cf_email">'.$label_email.':</label>
<input type="text" name="email" id="cf_email" size="50" maxlength="50" value="'.$form_data['email'].'" />
</div>
<div>
<label for="cf_subject">'.$label_subject.':</label>
<input type="text" name="subject" id="cf_subject" size="50" maxlength="50" value="'.$subject.$form_data['subject'].'" />
</div>
<div>
<label for="cf_message">'.$label_captcha.':</label>
<input type="text" name="captcha" id="cf_message" size="50" maxlength="50" value=""/>
</div>
<div id="captcha"><span id="captcha_text">Captcha:</span><img id="captcha_image" src="'.plugins_url("really-simple-captcha/tmp/" . $image) . '" alt="" /></div>
<div>
<input type="submit" value="'.$label_submit.'" name="send" id="cf_send" />
</div>
</form>';
if($sent == true) {
return $info;
} else {
return $info.$email_form;
}
}
add_shortcode('contact', 'wptuts_contact_form_sc');
// add plugins style to frontend
function prize_game_contact_form() {
wp_register_style('prize_game_contact_form', plugins_url('css/style.css',__FILE__));
wp_enqueue_style('prize_game_contact_form');
}
add_action('wp_enqueue_scripts', 'prize_game_contact_form');
I didn't add $captcha_instance->remove( $prefix ); so far because it works not as it should. It seems that every time I click on the submit button a new word is created and the function $captcha_instance->check() checks if the new word equals the captcha text in the input field but those words doesn't match of course because it's already a new word. I don't understand why a new word is created when clicking on the submit button. Where is the error?
I believe the problem is that you are generating a new $prefix each time the function runs. So there is no way to run the check() or the remove() because the $prefix used on the form is then a new value.
In your form, create a hidden input field with the value of the $prefix being used. So that value is posted with the form. When the function is run the 2nd time after the form post, use the posted $prefix value to do the check() and remove(). That should do it.
Here is a detailed article on how to integrate Really Simple Captcha plugin into your plugin or theme together with the hidden prefix field I just mentioned: http://www.lost-in-code.com/platforms/wordpress/wordpress-plugins/wordpress-using-really-simple-captcha/
Feel free to let me know if you need any assistance.
Are you using Wordpress? Do not try to reinvent the wheel.
Download a free plugin like Contact Form 7 which includes an Ajax powered (doesn't require page refresh) Askimet Spam Filter, plus CAPTCHA system.
CAPTCHA works only half as well as it did 3-5 years back.
My advise to you is this:
Do not look for simple, but look for what works.
if you're not using WordPress then you might check this simple form & captcha idea - if you can grab out the captcha part that is, captcha has become incredibly complex and over engineered when all it needs to do is differentiate simply between a human and non-human for most purposes.
You must create the variables $word, $prefix and $image only after you check the correctness of the user answer, and you must pass the $prefix value through a hidden input field. I haven't checked it, but this code should work now:
function wptuts_get_the_ip() {
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
return $_SERVER["HTTP_X_FORWARDED_FOR"];
}
elseif (isset($_SERVER["HTTP_CLIENT_IP"])) {
return $_SERVER["HTTP_CLIENT_IP"];
}
else {
return $_SERVER["REMOTE_ADDR"];
}
}
// short code function
function wptuts_contact_form_sc( $atts ) {
add_shortcode( 'contact', 'contact_form_shortcode' );
extract(shortcode_atts(array(
"email" => get_bloginfo('admin_email'),
"subject" => '',
"label_name" => 'Your Name',
"label_email" => 'Your E-mail Address',
"label_subject" => 'Your Answer',
"label_captcha" => 'Captcha',
"label_submit" => 'Submit',
"error_empty" => 'Please fill in all the required fields.',
"error_noemail" => 'Please enter a valid e-mail address.',
"success" => 'Thanks for your e-mail! We\'ll get back to you as soon as we can.'
), $atts));
$captcha_instance = new ReallySimpleCaptcha(); //imports really simple captcha plugin
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$error = false;
$required_fields = array("your_name", "email", "captcha", "subject");
foreach ($_POST as $field => $value) {
if (get_magic_quotes_gpc()) {
$value = stripslashes($value);
}
$form_data[$field] = strip_tags($value);
}
foreach ($required_fields as $required_field) {
$value = trim($form_data[$required_field]);
if(empty($value)) {
$error = true;
$result = $error_empty;
}
}
if(!is_email($form_data['email'])) {
$error = true;
$result = $error_noemail;
}
if ($error == false) {
$captcha_text=$form_data['captcha'];
$captcha_prefix=$form_data['prefix'];
$correct = $captcha_instance->check( $captcha_prefix, $captcha_text );
if($correct==TRUE){
$email_subject = "[" . get_bloginfo('name') . "] " . $form_data['subject'];
$email_message = $form_data['captcha'] . "\n\nIP: " . wptuts_get_the_ip();
$headers = "From: ".$form_data['your_name']." <".$form_data['email'].">\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\n";
$headers .= "Content-Transfer-Encoding: 8bit\n";
wp_mail($email, $email_subject, $email_message, $headers);
$result = $success;
$sent = true;
}
}
}
$word = $captcha_instance->generate_random_word();
$prefix = mt_rand();
$image= $captcha_instance->generate_image( $prefix, $word );
if($result != "") {
$info = '<div class="info">'.$result.'</div>';
}
$email_form = '<form class="contact-form" method="post" action="'.get_permalink().'">
<div>
<label for="cf_name">'.$label_name.':</label>
<input type="text" name="your_name" id="cf_name" size="50" maxlength="50" value="'.$form_data['your_name'].'" />
</div>
<div>
<label for="cf_email">'.$label_email.':</label>
<input type="text" name="email" id="cf_email" size="50" maxlength="50" value="'.$form_data['email'].'" />
</div>
<div>
<label for="cf_subject">'.$label_subject.':</label>
<input type="text" name="subject" id="cf_subject" size="50" maxlength="50" value="'.$subject.$form_data['subject'].'" />
</div>
<div>
<label for="cf_message">'.$label_captcha.':</label>
<input type="text" name="captcha" id="cf_message" size="50" maxlength="50" value=""/>
<input type="hidden" name="prefix" id="prefix" value="'.$prefix.'"/>
</div>
<div id="captcha"><span id="captcha_text">Captcha:</span><img id="captcha_image" src="'.plugins_url("really-simple-captcha/tmp/" . $image) . '" alt="" /></div>
<div>
<input type="submit" value="'.$label_submit.'" name="send" id="cf_send" />
</div>
</form>';
if($sent == true) {
return $info;
} else {
return $info.$email_form;
}
}
add_shortcode('contact', 'wptuts_contact_form_sc');
// add plugins style to frontend
function prize_game_contact_form() {
wp_register_style('prize_game_contact_form', plugins_url('css/style.css',__FILE__));
wp_enqueue_style('prize_game_contact_form');
}
add_action('wp_enqueue_scripts', 'prize_game_contact_form');

WordPress: Getting URL Variables with a Rewritten Page (Login Page)

As part of a larger project, I am attempting all login activity to occur at the following address:
www.url.com/login
So, a link that should lead to a form that would allow a user to reset their password would be found at the following:
www.url.com/login?action=lostpassword
However, the wp-login.php is not reading this correctly, and when I go to the above address, I get the normal log-in form.
This is how I have it set up:
add_action( 'init', 'add_virtual_page_template' );
function add_virtual_page_template()
{
global $wp, $wp_rewrite;
$wp->add_query_var( 'template' );
add_rewrite_endpoint( 'login', EP_PERMALINK | EP_PAGES );
add_rewrite_rule( 'login/?', 'index.php?template=login', 'top' );
add_rewrite_rule( 'login/?', 'index.php?template=login', 'top' );
$wp_rewrite->flush_rules();
}
add_action( 'template_redirect', 'add_virtual_page_redirect' );
function add_virtual_page_redirect()
{
global $wp;
$queryvar = get_query_var('template');
if ($queryvar && $queryvar == 'login')
{
include(site_url('wp-login.php'));
exit();
}
if ($queryvar == 'mylogin')
{
include( get_stylesheet_directory() . '/page-login.php' );
exit();
}
}
What am I missing?
Here's how I dealt with this when building out the WordPress for Web Apps toolkit (https://github.com/cferdinandi/web-app-starter-kit):
// LOGIN FORM SHORTCODE
function wpwebapp_login() {
// Get current page URL
$url_current = #( $_SERVER["HTTPS"] != 'on' ) ? 'http://'.$_SERVER["SERVER_NAME"] : 'https://'.$_SERVER["SERVER_NAME"];
$url_current .= ( $_SERVER["SERVER_PORT"] !== 80 ) ? ":".$_SERVER["SERVER_PORT"] : "";
$url_current .= $_SERVER["REQUEST_URI"];
$url_clean = array_shift( explode('?', $url_current) );
$login_failed = $url_clean . '?login=failed';
$signup_success = $url_clean . '?signup=success';
$reset_success = $url_clean . '?password-reset=success';
// Variables
$login_status = '';
// If login failed
if ( $url_current == $login_failed ) {
$login_status = '<div class="alert alert-red">Invalid username or password. Please try again.</div>';
}
// If password reset
if ( $url_current == $signup_success ) {
$login_status = '<div class="alert alert-green"><strong>Success!</strong> We just sent you an email with your password.</div>';
}
// If password reset
if ( $url_current == $reset_success ) {
$login_status = '<div class="alert alert-green">Your password was successfully reset. We just emailed you a new one.</div>';
}
// The login form
$form =
$login_status .
'<form name="login" id="wp_login_form" action="' . get_option('home') . '/wp-login.php" method="post">
<div>
<label for="username">Username</label>
<input type="text" name="log" id="log" value="" tabindex="1" autofocus>
</div>
<div>
<label for="password">Password</label>
<input type="password" name="pwd" id="pwd" value="" tabindex="2">
</div>
<div>
<label>
<input name="rememberme" type="checkbox" id="rememberme" value="forever" tabindex="90" checked>
Remember Me
</label>
</div>
<div>
<button type="submit" name="wp-submit" id="wp-submit" tabindex="100" class="btn btn-blue">Log In</button><br>
Forgot your password?
<input type="hidden" name="action" value="login">
<input type="hidden" name="redirect_to" value="' . get_option('home') . '">
<input type="hidden" name="testcookie" value="1">
</div>
</form>';
// Display the form
return $form;
}
add_shortcode( 'loginform', 'wpwebapp_login' );
// FAILED LOGIN REDIRECT
add_action('login_redirect', 'redirect_login', 10, 3);
function redirect_login($redirect_to, $url, $user) {
// URL Variables
$referrer = $_SERVER['HTTP_REFERER'];
$url_clean = array_shift( explode('?', $referrer) );
$login_failed = $url_clean . '?login=failed';
// If the post submission is a valid page that's not the backend login screen
if(!empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin')) {
// If the password is empty...
if($user->errors['empty_password']){
wp_redirect($login_failed);
}
// If the username is empty...
else if($user->errors['empty_username']){
wp_redirect($login_failed);
}
// If the username is invalid...
else if($user->errors['invalid_username']){
wp_redirect($login_failed);
}
// If the password is incorrect...
else if($user->errors['incorrect_password']){
wp_redirect($login_failed);
}
// Catch all for all other issues
else {
wp_redirect(get_option('home'));
}
exit;
}
// Prevents page from hanging when redirected from backend
if ( !empty($referrer) && ( strstr($referrer,'wp-login') || strstr($referrer,'wp-admin')) ) {
wp_redirect(get_option('home'));
exit;
}
}
// BLOCK BACKEND ACCESS FOR NON-ADMINS
add_action( 'init', 'blockusers_init' );
function blockusers_init() {
// If accessing the admin panel and not an admin
if ( is_admin() && !current_user_can('level_10') ) {
// Redirect to the homepage
wp_redirect( home_url() );
exit;
}
}
With this setup, you block users from ever getting redirected to the backend (the unfortunate side effect: if you're not logged in, you need to login from the front-end first). You can add custom error messages based on the situation, etc. And you can add this to a page just by using the [loginform] shortcode.
The WordPress for Web Apps toolkit has functions like this for password resets, signup forms, and more, so you might want to check it out. I went through this same learning process last year on a project I was working on.

Automatic Login after Registration - Wordpress

I am working on a site where I have a frontend registration form and it works as it should but now I am having difficulty with automatic login and redirect upon successful registration. I've tried a few plugins and codes in my themes functions file but none seem to work. So how can I automatically log a person in after a successful registration? Any help would be appreciated thanks.
<?php
if(get_option('users_can_register')) {
//Check whether user registration is enabled by the administrator
?>
<?php
if($_POST){
//We shall SQL escape all inputs
$username = $wpdb->escape($_REQUEST['username']);
if(empty($username)) {
echo "<span style='color:#FF0000'><strong>Error..</strong></span><br /><br />You have to fill in the username.";
exit();
}
$email = $wpdb->escape($_REQUEST['email']);
if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/", $email)) {
echo "<span style='color:#FF0000'><strong>Error..</strong></span><br /><br />please use a calid e-mailadress.";
exit();
}
$pass1 = $wpdb->escape($_REQUEST['pass1']);
$pass2 = $wpdb->escape($_REQUEST['pass2']);
if ($pass1 != $pass2){
echo "<span style='color:#FF0000'><strong>Error..</strong></span><br /><br />please use a passwords don't match.";
exit();
}
$random_password = $pass1;
$status = wp_create_user( $username, $random_password, $email );
if ( is_wp_error($status) )
echo "<span style='color:#FF0000'><strong>Feil..</strong></span><br /><br />Username allready exist. please try another one.";
else {
$from = get_option('admin_email');
$headers = 'From: '.$from . "\r\n";
$subject = "Registration ok!";
$msg = "Welcome, you are now registered. Here is your username and password.\Info:\Username: $username\Password: $random_password";
wp_mail( $email, $subject, $msg, $headers );
echo "<strong>You are now registered. An e-mail is now sent to you with your username and password..";
}
exit();
}
else
{
//Embed the register form and javascript here
?>
<div id="result"></div>
<div style="padding-top:5px;"><h2 style="font-size:16px;color:#06f;">Register</h2></div>
<form id="wp_signup_form" action="" method="post">
<p><label>Username<br />
<input type="text" name="username" style="width:250px; margin-bottom:3px;"></label></p>
<p><label>E-Mail<br />
<input type="text" name="email" style="width:250px; margin-bottom:3px;"></label></p>
<p><label>Password<br />
<input type="password" name="pass1" style="width:250px; margin-bottom:3px;"></label></p>
<p><label>Repeat Password<br />
<input type="password" name="pass2" style="width:250px; margin-bottom:3px;"></label></p>
<br />
<input type=checkbox name="termsnp">By registering I agree to the terms and policy<br /><br />
<input type="submit" id="submitbtn" name="submit" value="Register" class="knapp" style="padding:8px;">
</form>
<script type="text/javascript">
$("#submitbtn").click(function() {
$('#result').html('<img src="<?php bloginfo('template_url'); ?>/images/loader.gif" class="loader" />').fadeIn();
var input_data = $('#wp_signup_form').serialize();
$.ajax({
type: "POST",
url: "<?php echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>",
data: input_data,
success: function(msg){
$('.loader').remove();
$('
<div>').html(msg).appendTo('div#result').hide().fadeIn('slow');
}
});
return false;
});
</script>
<?php
// embedded
}
?>
<?php
}
?>
for auto login after register use this code
$userdata = array(
'user_login' => esc_attr($username),
'user_email' => esc_attr($email),
'user_pass' => esc_attr($password),
'first_name' => esc_attr($first_name),
'last_name' => esc_attr($last_name),
'display_name' => esc_attr($first_name.' '.$last_name),
);
$register_user = wp_insert_user($userdata);
if (!is_wp_error($register_user)) {
wp_set_current_user( $register_user, $username );
wp_set_auth_cookie( $register_user );
do_action( 'wp_login', $username );
} else{
echo $register_user->get_error_message();
}
and for redirect use this
wp_redirect( site_url('my-account') ); exit;
replace site_url('my-account') by your successful url
I did not find out the correcting coding to use so what I ended up doing was using the plugin Formidable Pro to create a registration form which logs in and redirects upon successful registration. - http://formidablepro.com/

Ajaxify WP Options - Problems with using jquery serialize and checkboxes

i've been working on a theme options panel for wordpress. but i've run into a bug when i start using more than 1 checkbox as an option.
my code uses jquery to serialize all the form data and then submit it to wp's ajax-url. then i add a callback function and WP knows to send the data to the function i have set up to save the options to the DB.
it works when i check the boxes ON, hit save and no problem. the values are saved. but now that the boxes are checked if i try to uncheck 1 or 2 of the 3 and click save... then on refresh the boxes are still checked. the values are still 'ON' in the DB. I think this is b/c jquery doesn't serialize unchecked checkboxes, so they aren't be passed to the update_option array. since they aren't in the update_option array the values for those keys stays the same as is currently in the DB. hence, no change. strangely (to me atleast) if i uncheck all 3 of my test checkboxes then it does update properly.
so, i am looking for a work around that will update the options with the correct values and remove checkbox values that have been unchecked.
<?php
add_action('admin_menu', 'test_add_theme_page');
function test_add_theme_page() {
if ( isset( $_GET['page'] ) && $_GET['page'] == basename(__FILE__) ) {
add_action('admin_head', 'test_theme_page_head');
}
add_theme_page(__('Test Admin'), __('Test Admin'), 'edit_themes', basename(__FILE__), 'test_theme_page');
}
function test_theme_page_head() {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
jQuery('form#test_form').submit(function() {
var data = jQuery(this).serialize();
alert(data);
jQuery.post(ajaxurl, data, function(response) {
if(response == 1) {
show_message(1);
t = setTimeout('fade_message()', 2000);
} else {
show_message(2);
t = setTimeout('fade_message()', 2000);
}
});
return false;
});
});
function show_message(n) {
if(n == 1) {
jQuery('#saved').html('<div id="message" class="updated fade"><p><strong><?php _e('Options saved.'); ?></strong></p></div>').show();
} else {
jQuery('#saved').html('<div id="message" class="error fade"><p><strong><?php _e('Options could not be saved.'); ?></strong></p></div>').show();
}
}
function fade_message() {
jQuery('#saved').fadeOut(1000);
clearTimeout(t);
}
</script>
<?php
}
function test_theme_page() {
?>
<div class="wrap">
<h2><?php _e('Test Admin'); ?></h2>
<div id="saved"></div>
<?php $options = get_option('test_theme');
echo "<br>";
print_r($options);
echo"<br>";
?>
<form action="/" name="test_form" id="test_form">
Text<input type="text" name="test_text" value="<?php echo $options['test_text']; ?>" /><br />
Check1<input type="checkbox" name="test_check1" <?php echo ($options['test_check1'] == 'on') ? 'checked' : ''; ?> /><br />
Check2<input type="checkbox" name="test_check2" <?php echo ($options['test_check2'] == 'on') ? 'checked' : ''; ?> /><br />
Check3<input type="checkbox" name="test_check3" <?php echo ($options['test_check3'] == 'on') ? 'checked' : ''; ?> /><br />
<input type="hidden" name="action" value="test_theme_data_save" />
<input type="hidden" name="security" value="<?php echo wp_create_nonce('test-theme-data'); ?>" />
<input type="submit" value="Submit" />
</form>
</div>
<?php
}
add_action('wp_ajax_test_theme_data_save', 'test_theme_save_ajax');
function test_theme_save_ajax() {
check_ajax_referer('test-theme-data', 'security');
$data = $_POST;
unset($data['security'], $data['action']);
if(!is_array(get_option('test_theme'))) {
$options = array();
} else {
$options = get_option('test_theme');
}
if(!empty($data)) {
$diff = array_diff($options, $data);
$diff2 = array_diff($data, $options);
$diff = array_merge($diff, $diff2);
} else {
$diff = array();
}
if(!empty($diff)) {
if(update_option('test_theme', $data)) {
die('1');
} else {
die('0');
}
} else {
die('1');
}
}
you can see the full code here
http://pastebin.com/BCHwsBi5
was apparently 1. over-complicating things and 2. not understanding that update_option returns TRUE on an update and returns FALSE if there is no change and NOT on failure.
here's the corrected test_theme_save_ajax() function:
function test_theme_save_ajax() {
check_ajax_referer('test-theme-data', 'security');
$data = $_POST;
unset($data['security'], $data['action']);
update_option('test_theme', $data);
die('1');
}

Resources