Skip to content

Instantly share code, notes, and snippets.

View alokstha1's full-sized avatar

Alok Shrestha alokstha1

View GitHub Profile
@alokstha1
alokstha1 / index.html
Created July 7, 2016 15:55
Custom jQuery validation for form without submit function.
<form id="form-id">
<div class="span6 form-table">
<label for="usr">First Name <b>*</b></label>
<input name="first_name" type="text" class="form-control" id="usr" data-required="yes">
<div class="cus-validation-error" style="display:none;">
Please Enter Your First Name.
</div>
</div>
<div class="span6 form-table">
<label for="">E-mail address<b>*</b></label>
@alokstha1
alokstha1 / Sublime-stuffs.txt
Created July 7, 2016 16:03
Install PHPCS with WordPress Coding Standard with Sublime Text 3
1. cmd+shift+p
2. Type phpcs and install it
3. Preferences > Package Settings > Php Code Sniffer > User settings
@alokstha1
alokstha1 / programmatic_login.php
Created July 7, 2016 16:04
programmatic_login
/**
* Programmatically logs a user in
*
* @param string $username
* @return bool True if the login was successful; false if it wasn't
*/
function programmatic_login( $username ) {
if ( is_user_logged_in() ) {
wp_logout();
@alokstha1
alokstha1 / function.php
Created July 7, 2016 16:05
Change password frontend WP function
function change_pass_frontend() {
require_once( ABSPATH . 'wp-includes/class-phpass.php');
$old_pass = $_POST['old_pass'];
$new_pass = $_POST['new_pass'];
$user_id = $_POST['user_id'];
$user_data = get_userdata( $user_id );
$wp_hasher = new PasswordHash(8, TRUE);
$password_hashed = $user_data->user_pass;
if($wp_hasher->CheckPassword($old_pass, $password_hashed)) {
wp_set_password( $new_pass, $user_id );
@alokstha1
alokstha1 / gist:e417e733f6c49f6ffcff821a7129f27c
Last active November 12, 2018 06:35
Sublime Text 3 User Preferences.
{
"always_show_minimap_viewport": true,
"bold_folder_labels": true,
"caret_extra_bottom": 3,
"caret_extra_top": 3,
"caret_extra_width": 2,
"enable_tab_scrolling": false,
"font_face": "Ubuntu Mono",
"font_options":
[
@alokstha1
alokstha1 / functions.php
Created July 7, 2016 16:14
Add Parameter to url on unsuccessful login attempt
<?php
add_action( 'wp_login_failed', 'code_login_failed' );
function code_login_failed( $user ) {
// check what page the login attempt is coming from
$referrer = $_SERVER['HTTP_REFERER'];
// check that were not on the default login page
if ( !empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin') && $user!=null ) {
// make sure we don't already have a failed login attempt
if ( !strstr($referrer, '?login=failed' )) {
// Redirect to the login page and append a querystring of login failed
@alokstha1
alokstha1 / functions.php
Created July 7, 2016 16:15
Enable Login with Email and username WordPress.
<?php
remove_filter('authenticate', 'wp_authenticate_username_password', 20);
add_filter('authenticate', 'login_with_email', 20, 3);
function login_with_email($user, $email, $password) {
//Check for empty fields
if(filter_var($email, FILTER_VALIDATE_EMAIL) ){
if(empty($email) || empty ($password)){
//create new error object and add errors to it.
$error = new WP_Error();
@alokstha1
alokstha1 / function.php
Created July 7, 2016 16:17
Simple Post View Counter WordPress
<?php
/**
* Post view Count
*/
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
function wpb_set_post_views($postID) {
$count_key = 'wpb_post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
$count = 0;
@alokstha1
alokstha1 / functions.php
Created July 7, 2016 16:21
Simple Ajax Pagination WP
<?php
add_action('wp_ajax_infinite_scroll_home', 'infinite_scroll_home', 0);
add_action('wp_ajax_nopriv_infinite_scroll_home', 'infinite_scroll_home');
function infinite_scroll_home() {
$exclude_posts_json = $_POST['exclude_posts'];
$exclude_posts = json_decode($exclude_posts_json);
$post_offset = $_POST['post_offset'];
$infinite_scroll_args = array( 'post_type'=>'post', 'posts_per_page'=> 2,'post__not_in' => $exclude_posts, 'offset' => $post_offset);
$infinite_scroll_query = new WP_Query( $infinite_scroll_args );
while( $infinite_scroll_query->have_posts() ) : $infinite_scroll_query->the_post();
@alokstha1
alokstha1 / functions.php
Created July 7, 2016 16:31
Adding Extra Field in Term Field both on Add and Edit Form WordPress
add_filter( 'pa_color_add_form_fields', array( $this, 'pa_attr_taxonomy_columns' ) );
add_filter( 'pa_color_edit_form_fields', array( $this, 'edit_pa_attr_taxonomy_column' ), 10 );
add_action( 'created_pa_color', array( $this, 'pa_attr_save_taxonomy_fields' ), 10, 2 );
add_action( 'edit_pa_color', array( $this, 'pa_attr_save_taxonomy_fields' ), 10, 2 );
/**
* Add Fields
*
*/