Skip to content

Instantly share code, notes, and snippets.

@vishalbasnet23
vishalbasnet23 / functions.php
Created December 19, 2014 10:55
Custom Image Source with BFI outside loop
<?php
/**
* BFI Thumb For Custom Image Src
*/
function code_custom_img( $image_src, $image_width, $image_height ) {
$custom_image_src = bfi_thumb( $image_src, array( 'width' => $image_width, 'height' => $image_height) );
return $custom_image_src;
}
@vishalbasnet23
vishalbasnet23 / functions.php
Last active February 9, 2019 07:54
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();
@vishalbasnet23
vishalbasnet23 / functions.php
Last active August 29, 2015 14:15
Mail chimp 1.3 PHP API on ajax call
<?php
require get_template_directory() . '/inc/mailchipapi.php';
add_action('wp_ajax_add_to_mailchimp_list', 'add_to_mailchimp_list', 0);
add_action('wp_ajax_nopriv_add_to_mailchimp_list', 'add_to_mailchimp_list');
function add_to_mailchimp_list() {
$apiKey = 'YOUR_API_KEY';
$listId = 'YOUR_LIST_ID'; // End Customer
$double_optin=false;
@vishalbasnet23
vishalbasnet23 / functions.php
Created February 18, 2015 10:46
Call Ajax on Successful Facebook Share.
<?php
add_action('wp_ajax_facebook_share', 'facebook_share', 0);
add_action('wp_ajax_nopriv_facebook_share', 'facebook_share');
function facebook_share() {
$your_data = $_POST['your_data'];
echo $your_data;
die;
}
@vishalbasnet23
vishalbasnet23 / functions.php
Created February 18, 2015 10:53
Ajax Call On Successful Twitter Share
<?php
add_action('wp_ajax_twitter_share', 'twitter_share', 0);
add_action('wp_ajax_nopriv_twitter_share', 'twitter_share');
function twitter_share() {
$your_data = $_POST['your_data'];
echo $your_data;
die;
}
@vishalbasnet23
vishalbasnet23 / functions.php
Created February 19, 2015 06:18
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;
@vishalbasnet23
vishalbasnet23 / functions.php
Created February 25, 2015 06:24
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();
@vishalbasnet23
vishalbasnet23 / functions.php
Created February 25, 2015 07:05
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
@vishalbasnet23
vishalbasnet23 / functions.php
Created March 16, 2015 08:29
Change Return Path of wp_mail()
<?php
/**
* Return Path Change
*/
class email_return_path {
function __construct() {
add_action( 'phpmailer_init', array( $this, 'fix' ) );
}
@vishalbasnet23
vishalbasnet23 / functions.php
Created March 16, 2015 08:31
Login redirect WP
<?php
function my_login_redirect( $redirect_to, $request, $user ) {
//is there a user to check?
global $user;
if ( isset( $user->roles ) && is_array( $user->roles ) ) {
//check for admins
if ( in_array( 'administrator', $user->roles ) ) {
// redirect them to the default place
return $redirect_to;
} else {