Wednesday 23 September 2015

login with ajax in wordpress




Remember me

// add jquery and ajax call






 <?php

/*Start Login*/

if(!empty($_POST['username']) && !empty($_POST['password']) && $_POST['action'] == "login"){
    $info = array();
    $info['user_login'] = $_POST['username'];
    $info['user_password'] = $_POST['password'];
    $info['remember'] = $_POST['remember'];
    $user = get_userdatabylogin($_POST['username']);
    $CheckMeta = get_user_meta($user->ID, 'has_to_be_activated', true);
    $user_signon = wp_signon( $info, false );
    if ( is_wp_error($user_signon) ){
       echo  '

Wrong username or password

'; }else if ($CheckMeta) { echo '

User is not activated. please go and activate

'; }else { echo '

Login successful, redirecting...

'; } } /*End Login*/ ?>

forgot password with ajax in wordpress

step 1. add link for open bootstrap modal

 Forgot Password?



 

// add jquery and ajax call


 <?php
//finally add php code.

/*Forgot Password Start*/
        if(!empty($_POST['user_login']) && isset( $_POST['action'] ) &&  $_POST['action'] == 'reset') 
        {
         global $wpdb;
         $error = '';
         $success = '';
            $email = trim($_POST['user_login']);
            
            if( ! is_email( $email )) {
                $error = 'Invalid username or e-mail address.';
            } else if( ! email_exists( $email ) ) {
                $error = 'There is no user registered with that email address.';
            } else {
                
                $random_password = wp_generate_password( 12, false );
                $user = get_user_by( 'email', $email );
                
                $update_user = wp_update_user( array (
                        'ID' => $user->ID, 
                        'user_pass' => $random_password
                    )
                );
                
                if( $update_user ) {
                    $to = $email;
                    $subject = 'Your new password';
                    $sender = get_option('name');
                    
                    $message = 'Your new password is: '.$random_password;
                    
                    $headers[] = 'MIME-Version: 1.0' . "\r\n";
                    $headers[] = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
                    $headers[] = "X-Mailer: PHP \r\n";
                    $headers[] = 'From: '.$sender.' < '.$email.'>' . "\r\n";
                    
                    $mail = wp_mail( $to, $subject, $message, $headers );
                    if( $mail )
                        $success = 'Check your email address for you new password.';
                        
                } else {
                    $error = 'Oops something went wrong updaing your account.';
                }
                
            }
            
            if( ! empty( $error ) )
                echo '

'. $error .'

'; if( ! empty( $success ) ) echo '

'. $success .'

'; } /*Forgot Password End*/ ?>

how to use number_format in php

<?php
$Price = 8945.234564;
$Price = number_format($UsedPrintPrice, 2);
print $Price;
?>

//: Output
8945.23

Rounding to the nearest nickel in php

<?php
$Price = 98.06;
$Price = round(20 * $Price,0)/20;
print $Price;
?>
//: Output
98.05

Saturday 19 September 2015

str_replace in php

Replace characters from a variable using str_replace.
<?php
$Article = "This is my first paragraph.";
echo str_replace('paragraph' ,'Article', $Article);
?>
//: Output
This is my first Article.