Pages

Monday, June 24, 2013

Redirect to home page if query string not match to value - wordpress


Please add below code to "functions.php" for generating the query string:

function add_query_vars($aVars) {
    $aVars[] = "mode";
    return $aVars;
}

add_filter('query_vars', 'add_query_vars');

function add_rewrite_rules($aRules) {
    $aNewRules = array('news/([^/]+)/?$' => 'index.php?pagename=news&mode=$matches[1]');
    $aRules = $aNewRules + $aRules;
    return $aRules;
}

add_filter('rewrite_rules_array', 'add_rewrite_rules');

function wp_redirect_to_homepage( $query ) {    
 if ( $query->is_main_query() && ( $query->query_vars['pagename'] == 'news') ){         
        if( ( array_key_exists( 'mode', $query->query_vars ) && (trim($query->query_vars['mode']) != 'follow' && trim($query->query_vars['mode']) != 'like'))) {
            wp_redirect( get_permalink($query->queried_object_id) );
            exit;
        }   
    }
}
add_action( 'parse_query', 'wp_redirect_to_homepage' );


Also you can see below URL:
http://stackoverflow.com/questions/14669336/redirect-if-wordpress-query-string-is-empty 
Its very good example for rewrite query string.

Wednesday, June 12, 2013

How to redirect non-admin users to home page (wordpress)? [resolved]


Please write below code in functions.php

function drop_login_redirect( $redirect_to, $request, $user  ) {
return ( is_array( $user->roles ) && in_array( 'administrator', $user->roles ) ) ? admin_url() : site_url();
}
add_filter( 'login_redirect', 'drop_login_redirect', 10, 3 );

Wordpress disable admin panel for all user [resolved]


Please write below code into functions.php

function drop_hide_admin_bar_settings() {
?>
<style type="text/css">
.show-admin-bar {
display: none;
}
</style>
<?php
}

function drop_disable_admin_bar() {
   if (!current_user_can('administrator')) {
add_filter( 'show_admin_bar', '__return_false' );
add_action( 'admin_print_scripts-profile.php', 'drop_hide_admin_bar_settings' );
   }
}

add_action( 'init', 'drop_disable_admin_bar' , 9 );

Tuesday, June 11, 2013

Allow WordPress login using Email Address


Please add below code to admin panel

function login_with_email_address($username) {
$user = get_user_by_email($username);
if(!empty($user->user_login))
$username = $user->user_login;
return $username;
}
add_action('wp_authenticate','login_with_email_address');

Create options pages in wordpress admin panel


Please write below code in functions.php


<?php
add_action('admin_menu', 'yt_menu');
function yt_menu() {
  add_menu_page('YT Options', 'yt Uploader', 'manage_options', 'yt-identifier', 'yt_options_page', '', 30);
}

function yt_register_settings() {
register_setting( 'yt_theme_options', 'yt_options');
}
add_action( 'admin_init', 'yt_register_settings' );

/*
Above, we have simply created a new function for registering the new option (YT Options) using the register setting wordpress function. The most important part of the above code is the “add_menu_page” bit, which is the code that actually adds a new menu item to the admin panel named “YT Options”.

We have successfully created a new menu item, specifically for changing the text on YT Options.  Now we need to create some content for this new page. Add the following code directly below what code you have already added:
*/

function yt_options_page() {
global $yt_options, $yt_categories, $yt_layouts;

if (!current_user_can('manage_options'))  {
wp_die( __('You do not have sufficient permissions to access this page.') );
}

echo '<div class="wrap"><h2>Change your yt uploader settings</h2></div>';
?>

<form id="yt_auth_options" method="post" action="options.php">
<?php $settings = get_option( 'yt_options', $yt_options ); ?>
<?php settings_fields( 'yt_theme_options' ); ?>
<table class="form-table">
<tbody
<tr valign="top">
<td colspan="2">
<div id="auth_options">
<div>
Username : <label title="username"><input type="text" name="yt_options[username]" value="<?php echo stripslashes($settings['username']); ?>"></label>
<br>Password : <label title="password"><input type="password" name="yt_options[password]" value="<?php echo stripslashes($settings['password']); ?>"></label>
</div>
</div><br>
</td>
</tr>
</tbody>
</table>
<input type="submit" value="Save settings" class="button-primary"> <input type="hidden" value="1" name="yt_uploader_settings"></form>

<?php
}
?>

The code above first of all gives our new page a title that is in-keeping with the style of our admin area. Next up, we define what message to display when our text has been updated. We then create our form for actually entering and editing the options. We then validate the value of this text and return it to be stored, using options.php.

Wordpress login/registration using ajax - [resolved]


Please write below code in functions.php
-----------------------------------------------------------------------------------------------------------

<?php
function ajax_login_init(){

    wp_register_script('ajax-login-script', get_template_directory_uri() . '/js/ajax-login-script.js', array('jquery') );
    wp_enqueue_script('ajax-login-script');

    wp_localize_script( 'ajax-login-script', 'ajax_login_object', array(
        'ajaxurl' => admin_url( 'admin-ajax.php' ),
        'redirecturl' => home_url(),
        'loadingmessage' => __('Sending user info, please wait...')
    ));

    // Enable the user with no privileges to run ajax_login() in AJAX
    add_action( 'wp_ajax_nopriv_ajaxlogin', 'ajax_login' );
}

// Execute the action only if the user isn't logged in
if (!is_user_logged_in()) {
    add_action('init', 'ajax_login_init');
}

function ajax_login(){

    // First check the nonce, if it fails the function will break
    check_ajax_referer( 'ajax-login-nonce', 'security' );

    // Nonce is checked, get the POST data and sign user on
    $info = array();
    $info['user_login'] = $_POST['username'];
    $info['user_password'] = $_POST['password'];
    $info['remember'] = true;

    $user_signon = wp_signon( $info, false );
    if ( is_wp_error($user_signon) ){
        echo json_encode(array('loggedin'=>false, 'message'=>__('Wrong username or password.')));
    } else {
        echo json_encode(array('loggedin'=>true, 'message'=>__('Login successful, redirecting...')));
    }

    die();
}

function ajax_registration_init(){

    wp_localize_script( 'ajax-login-script', 'ajax_registration_object', array(
        'ajaxurl' => admin_url( 'admin-ajax.php' ),
        'redirecturl' => home_url(),
        'loadingmessage' => __('Sending user info, please wait...')
    ));

    // Enable the user with no privileges to run ajax_login() in AJAX
    add_action( 'wp_ajax_nopriv_ajaxregistration', 'ajax_registration' );
}

// Execute the action only if the user isn't logged in
if (!is_user_logged_in()) {
    add_action('init', 'ajax_registration_init');
}

function ajax_registration(){

    // First check the nonce, if it fails the function will break
    check_ajax_referer( 'ajax-registration-nonce', 'regsecurity' );

    // Nonce is checked, get the POST data and sign user on
    $info = array();
   
    $regname = $_POST['regname'];
    $regemail = $_POST['regemail'];
$regusername = explode("@",$regemail);
$regusername = $regusername[0];
$regpassword = $_POST['regpassword'];

$myerror = '';

if($regemail == ''){
$myerror .= 'Please enter email address';
}
else if($regpassword == ''){
$myerror .= ($myerror != '')?'<br>':'';
$myerror .= 'Please enter password';
}
if($myerror != ''){
echo json_encode(array('signupresponse'=>false, 'message'=>__("$myerror")));
}
else {
if ( email_exists($regemail) == false ) {
$user_id = wp_create_user( $regusername, $regpassword, $regemail );
update_user_option( $user_id, 'firstname', $regname);
echo json_encode(array('signupresponse'=>true, 'message'=>__('Register successful!\n You can login now')));
} else {
echo json_encode(array('signupresponse'=>false, 'message'=>__('Email already exists.')));
}
}

    die();
}
?>
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
Now create "ajax-login-script.js" file 
-----------------------------------------------------------------------------------------------------------
jQuery(document).ready(function($) {

    // Show the login dialog box on click
    $('a#show_login').on('click', function(e){
        $('body').prepend('<div class="login_overlay"></div>');
        $('form#login').fadeIn(500);
        $('div.login_overlay, form#login a.close').on('click', function(){
            $('div.login_overlay').remove();
            $('form#login').hide();
        });
        e.preventDefault();
    });

    // Perform AJAX login on form submit
    $('form#login').on('submit', function(e){
        $('form#login p.status').show().text(ajax_login_object.loadingmessage);
        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: ajax_login_object.ajaxurl,
            data: { 
                'action': 'ajaxlogin', //calls wp_ajax_nopriv_ajaxlogin
                'username': $('form#login #username').val(), 
                'password': $('form#login #password').val(), 
                'security': $('form#login #security').val() },
            success: function(data){
                $('form#login p.status').text(data.message);
                if (data.loggedin == true){
                    document.location.href = ajax_login_object.redirecturl;
                }
            }
        });
        e.preventDefault();
    });

// Show the login dialog box on click
    $('a#show_registration').on('click', function(e){
        $('body').prepend('<div class="login_overlay"></div>');
        $('form#registration').fadeIn(500);
        $('div.login_overlay, form#registration a.close').on('click', function(){
            $('div.login_overlay').remove();
            $('form#registration').hide();
        });
        e.preventDefault();
    });

    // Perform AJAX registration on form submit
    $('form#registration').on('submit', function(e){
        $('form#registration p.status').show().text(ajax_registration_object.loadingmessage);
        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: ajax_registration_object.ajaxurl,
            data: { 
                'action': 'ajaxregistration', //calls wp_ajax_nopriv_ajaxregistration
                'regname': $('form#registration #regname').val(), 
                'regemail': $('form#registration #regemail').val(), 
                'regpassword': $('form#registration #regpassword').val(), 
                'regsecurity': $('form#registration #regsecurity').val() },
            success: function(data){
                $('form#registration p.status').text(data.message);
                if (data.signupresponse == true){
                    document.location.href = ajax_registration_object.redirecturl;
                }
            }
        });
        e.preventDefault();
    });
});
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
Please add below code after body tag in header.php
-----------------------------------------------------------------------------------------------------------
<form id="login" action="login" method="post">
        <h1>Site Login</h1>
        <p class="status"></p>
        <label for="username">Username</label>
        <input id="username" type="text" name="username">
        <label for="password">Password</label>
        <input id="password" type="password" name="password">
        <a class="lost" href="<?php echo wp_lostpassword_url(); ?>">Lost your password?</a>
        <input class="submit_button" type="submit" value="Login" name="submit">
        <a class="close" href="">(close)</a>
        <?php wp_nonce_field( 'ajax-login-nonce', 'security' ); ?>
<?php do_action( 'wordpress_social_login' ); ?>
    </form>

<form id="registration" action="registration" method="post">
        <h1>Site Registration</h1>
        <p class="status"></p>
        <label for="regname">Name</label>
        <input id="regname" type="text" name="regname">
        <label for="regemail">Email</label>
        <input id="regemail" type="text" name="regemail">
        <label for="regpassword">Password</label>
        <input id="regpassword" type="password" name="regpassword">
        <input class="submit_button" type="submit" value="Registration" name="submit">
        <a class="close" href="">(close)</a>
        <?php wp_nonce_field( 'ajax-registration-nonce', 'regsecurity' ); ?>
    </form>

after this add below code for "login/logout" link
<?php if (is_user_logged_in()) { ?>
<a class="login_button" href="<?php echo wp_logout_url( home_url() ); ?>">Logout</a>
<?php } else { ?>
<a class="login_button" id="show_login" href="">Login</a> &nbsp;&nbsp;<a class="registration_button" id="show_registration" href="">SignUp</a>
<?php } ?>
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
Please add below CSS code into your style.css 
-----------------------------------------------------------------------------------------------------------
form#login, form#registration{
    display: none;
    background-color: #FFFFFF;
    border-radius: 8px;
    font-family: Arial, Helvetica, sans-serif;
    box-shadow: 0 0 6px rgba(0, 0, 0, 0.2);
    position: fixed;
    top: 200px;
    padding: 40px 25px 25px 25px;
    width: 350px;
    z-index: 999;
    left: 50%;
    margin-left: -200px;
    color: #878787;
    font-size: 11px;
}

form#login h1, form#registration h1{
    color: #333333;
    font-family: 'Georgia', 'Times New Roman', Times, serif;
    font-size: 27px;
    font-weight: 100;
    text-align: center;
    line-height: 1;
    margin: 0 0 30px 0;
}

form#login input#username,
form#login input#password,
form#registration input#regname,
form#registration input#regemail,
form#registration input#regpassword{
    border: 1px solid #EDEDED;
    border-radius: 3px 3px 3px 3px;
    box-shadow: 0 0 3px rgba(0, 0, 0, 0.1) inset;
    color: #333333;
    font-size: 15px;
    padding: 10px 10px 10px 13px;
    width: 325px;
    margin: 7px 0 30px 0;
    background-color: #F9F9F9;
    font-family: 'Georgia', 'Times New Roman', Times, serif;
}

form#login input#username:focus,
form#login input#username:focus,
form#registration input#regname:focus,
form#registration input#regemail:focus,
form#registration input#regpassword:focus{
    background-color: #FFF;
}


form#login input.submit_button, form#registration input.submit_button{
    font-size: 13px;
    color: #FFF;
    border: 1px solid #b34336;
    background-color: #e25c4c;
    border-radius: 3px;
    text-shadow: 0 1px 0 #ba3f31;
    padding: 9px 31px 9px 31px;
    background: -moz-linear-gradient(top, #ea6656, #df5949);
    border-top: 1px solid #bb483a;
    border-bottom: 1px solid #a63b2e;
    float: right;
    box-shadow: 0 1px 0 #E87A6E inset;
}

form#login a, form#registration a{
    text-decoration: none;
}

form#login a.close, form#registration a.close{
    color: #DCDCDC;
    position: absolute;
    right: 15px;
    top: 15px;
}

form#login a.lost, form#registration a.lost{
    color: #B4B2B2;
    float: left;
    margin: 10px 0 0 0;
}

form#login p.status, form#registration p.status{
    text-align: center;
    margin: -25px 0 20px 0;
    display: none;
}

a.login_button, a.registration_button{
    font-family: Arial, Helvetica, sans-serif;
    padding: 5px 7px 5px 7px;
    background-color: #FFF;
    border-radius: 3px;
    border: 1px solid #DCDCDC;
    color: #333;
    text-decoration: none;
    font-size: 11px;
}

.login_overlay{
    height: 100%;
    width: 100%;
top: 0px;
    background-color: #F6F6F6;
    opacity: 0.9;
    position: fixed;
    z-index: 998;
}
-----------------------------------------------------------------------------------------------------------