Prevent anchor tag onclick # going to top of the page

Please find below code for prevent anchor tag onclick

$(document).ready(function()
{ 
   a.click(function(e)
   {
       e.preventDefault();
   });
});
Advertisements

jQuery validation for multiple file upload

Please find below code It’s allowed only png, jpg, jpeg and pdf files and upload upto 3 files and not more than 10MB
jQuery(‘input[name=”file[]”]’).change(function(){
var tot_size=0;
var count_files = jQuery(this).get(0).files.length;
for (var i = 0; i < jQuery(this).get(0).files.length; ++i) {
var file_size=jQuery(this).get(0).files[i].size;
tot_size = tot_size+file_size;
}
for (var i = 0; i < jQuery(this).get(0).files.length; ++i) {
var file1=jQuery(this).get(0).files[i].name;
if(!/(\.png|\.jpg|\.jpeg|\.pdf)$/i.test(file1))
{
jQuery(‘input[type=”submit”]’).attr(‘disabled’,’disabled’);
jQuery(“.file_error”).text(“”);
jQuery(this).css(“border”, “1px solid #c00”);
var err_var=true;
}else{
jQuery(“.file_error”).text(“Only png, jpg, jpeg and pdf files are allowed (Upload upto 3 files and 10MB).”);
jQuery(‘input[type=”submit”]’).removeAttr(‘disabled’,’disabled’);
jQuery(this).css(“border”, “1px solid #ddd”);
var err_var=false;
}
}
if(err_var==false){
if(tot_size < 10485760 && count_files < 4){
jQuery(“.file_error”).text(“Only png, jpg, jpeg and pdf files are allowed (Upload upto 3 files and 10MB).”);
jQuery(‘input[type=”submit”]’).removeAttr(‘disabled’,’disabled’);
jQuery(this).css(“border”, “1px solid #ddd”);
}else{
jQuery(‘input[type=”submit”]’).attr(‘disabled’,’disabled’);
jQuery(“.file_error”).text(“”);
jQuery(this).css(“border”, “1px solid #c00”);
}
}
});

Smooth Scroll using jQuery

Please find below code for smooth scroll

$(document).ready(function(){
$(“a”).on(‘click’, function(event) {
if (this.hash !== “”) {
event.preventDefault();
var hash = this.hash;
$(‘html, body’).animate({
scrollTop: $(hash).offset().top
}, 1800, function(){
window.location.hash = hash;
});
}
});
});

Exclude Category from Shop Woo-commerce WordPress

/* Exclude Category from Shop woo-commerce */
add_filter( ‘get_terms’, ‘get_subcategory_terms’, 10, 3 );
function get_subcategory_terms( $terms, $taxonomies, $args ) {
$new_terms = array();
// if a product category and on the shop page
if ( in_array( ‘product_cat’, $taxonomies ) && ! is_admin() && is_shop() ) {
foreach ( $terms as $key => $term ) {
if ( ! in_array( $term->slug, array( ‘project1’, ‘susan’ ) ) ) {
$new_terms[] = $term;
}
}
$terms = $new_terms;
}
return $terms;
}

Ajax call in wordpress functions.php using jQuery

/* Paste below code in header.php or footer.php */

jQuery.ajax({
type: “POST”,
url: “<?php echo admin_url( ‘admin-ajax.php’ ); ?>”,
data:{
action: “my_action”,
“date”: date,
“month”: month,
“year”: year
},
}).success(function(result){
alert(result);
});

/* Paste below code in functions.php */

add_action( ‘wp_ajax_my_action’, ‘my_action_callback’ );
add_action( ‘wp_ajax_nopriv_my_action’, ‘my_action_callback’ );

function my_action_callback(){
global $wpdb;
$events = $wpdb->get_results(“SELECT ID, title FROM table_name”);
$data = “<ul>”;
foreach($events as $evt){
$data .= “<li>”;
$data .= ‘<a href=”#” target=”_blank”>’.$evt->title.'</a>’;
$data .= “</li>”;
}
$data .= “</ul>”;
echo $data; exit;
}

Get month name from number in PHP

Please find below code

1.
$month=”January”;
$date = date_parse($month);
$month_number = $date[‘month’];

2.
switch ($m) {
case “January”:
case “Jan”:
$m = “01”;
break;
case “Febuary”:
case “Feb”:
$m = “02”;
break;
case “March”:
case “Mar”:
$m = “03”;
break;
case “April”:
case “Apr”:
$m = “04”;
break;
case “May”:
$m = “05”;
break;
case “June”:
case “Jun”:
$m = “06”;
break;
case “July”:
case “Jul”:
$m = “07”;
break;
case “August”:
case “Aug”:
$m = “08”;
break;
case “September”:
case “Sep”:
$m = “09”;
break;
case “October”:
case “Oct”:
$m = “10”;
break;
case “November”:
case “Nov”:
$m = “11”;
break;
case “December”:
case “Dec”:
$m = “12”;
break;
default:
$m = false;
break;
}