How to use WPDB class for DB access

require_once ‘../../../wp-load.php’;

function insertDB($name,$contact,$email,$evtName) {
global $wpdb;
$wpdb->custom_table = “custom_table_name”;

//$wpdb->show_errors();

$ok = $wpdb->insert($wpdb->custom_table, array(
‘name’ => $name,
‘contact’ => $contact,
‘email’ => $email,
‘event_name’ => $evtName
));

if ($ok) {
echo json_encode(array( ‘ok’ => true , ‘status’ => ‘success in DB insert’ ));
}else {
//$wpdb->print_error();
echo json_encode(array( ‘ok’ => false , ‘status’ => ‘failed on DB insert’ ));
}

}

insertDB($name,$contact,$email,$evtName);

How to use WP phpmailer on separate php file.


/* Send Mail */

require_once '../../../wp-includes/class-phpmailer.php';

//Gmail settings
define('GUSER', 'sender@gmail.com'); // Gmail username
define('GPWD', 'xxxxx'); // Gmail password

//Host settings
define('SMTPUSER', 'xerxis@domain.com'); // sec. smtp username
define('SMTPPWD', '12314'); // sec. password
define('SMTPSERVER', 'mail.domain.com'); // sec. smtp server

function smtpmailer($to, $from, $from_name, $subject, $body, $is_gmail = true) {
global $error;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
if ($is_gmail) {
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = GUSER;
$mail->Password = GPWD;
} else {
$mail->Host = SMTPSERVER;
$mail->Username = SMTPUSER;
$mail->Password = SMTPPWD;
}
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
}

$msg = 'Hello World';
$subj = 'test mail message';
$to = 'kaixersoft@gmail.com';
$from = 'from@mail.com';
$name = 'test email';

//try using gmail
if (smtpmailer($to, $from, $name, $subj, $msg)) {
echo json_encode(array('status' => 'message send via Gmail'));
//try host settings
} else {
if (!smtpmailer($to, $from, $name, $subj, $msg, false)) {
if (!empty($error)) echo $error;
} else {
echo json_encode(array('status' => 'message send via Host Settings'));
}
}

How to use WP-Includes (3.2.1++)

Load jQuery using Google CDN:

function init_includes() {
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js');
wp_enqueue_script( 'jquery' );
}
add_action('wp_enqueue_scripts', 'init_includes');

see codex for other built-in includes:
http://codex.wordpress.org/Function_Reference/wp_enqueue_script#Load_a_default_WordPress_script_from_a_non-default_location

How to capture form input into objects

Features :
1. auto check for selected and non-selected checkbox
2. auto capture selected radio


function nvp2obj(selector) {
var inputs = $(selector);
var o = {};
inputs.each(function(i) {
if (this.name) {
if (this.type == 'checkbox' && $(this).attr('checked')) o[this.name] = $(this).val();
if (this.type == 'radio' && $(this).attr('checked')) o[this.name] = $(this).val();
if (this.type == 'text' || this.type == 'textarea') o[this.name] = $(this).val();
}
});
return o;
}

How to use :
$('#btnLetsTalk').click(function(e) {

var inputs = nvp2obj('#frmLetsTalk :input');

console.log(inputs);

e.preventDefault();

});

How to Fix App Authorization issues

/*
-- [ Using Old FB SDK ] ------------------------------------------------------
FB.init({ appId: '202143216518919',
status: true,
cookie: true,
xfbml: true});

//App Settings
Encrypted Access Token = Disabled

*/
FB.getLoginStatus(function(response) {
if (!response.session) {
FB.ui({
method: "permissions.request",
"perms": permission
},
function(response) {
if (response) {
console.log(response);
}
}
);
}
});

/*
-- [ Using New FB SDK ] ------------------------------------------------------
FB.init({ appId: '202143216518919',
status: true,
cookie: true,
xfbml: true,
oauth : true //Enable OAuth 2.0
});

//App Settings
Encrypted Access Token = true

*/
FB.login(function(response) {
console.log(response);
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
} else {
console.log('User cancelled login or did not fully authorize.');
}
}, {scope:permission});

/*
-- [ Using New FB SDK / disabled OAuth ] ------------------------------------------------------
FB.init({ appId: '202143216518919',
status: true,
cookie: true,
xfbml: true,
oauth : false //Enable OAuth 2.0
});

//App Settings
Encrypted Access Token = true

*/
FB.getLoginStatus(function(response) {

console.log(response);
if (response.status === "notConnected") {
console.log('not yet authorized');
FB.ui({
method: "permissions.request",
"perms": permission
},function(response) {
if (response) {
var accessToken = response.authResponse.accessToken;
var uid = response.authResponse.accessToken
console.log(response);
}
});
} else {
console.log('connected to FB');
}

});