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();
   });
});

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;
}

Change scrollbar style using css

/*CSS for scrollbar*/

::-webkit-scrollbar {
width: .3em;
height: 2em
}
::-webkit-scrollbar-button {
background: #ccc
}
::-webkit-scrollbar-track-piece {
background: #eee
}
::-webkit-scrollbar-thumb{
background: #888
}

But this CSS not supported in Mozilla browser.

Hide & Refresh iframe using jQuery

/* Hide iframe using jQuery */
jQuery(‘#iframe’, window.parent.document).hide();
/* Refresh iframe using jQuery */
jQuery(‘#iframe’,window.parent.document).attr(‘src’,jQuery(‘#iframe’,window.parent.document).attr(‘src’));

Node vs. entity in drupal

Please find below given clear explanation…..

  • To me, the concept of “node” is fairly clear: It is an “instantiation” of a content-type. When you add content, you choose the type, e.g. “Article”, and the content that you add (e.g. the article text, title, images, etc.) is stored as one unit in the database, and is also often assigned its own URL path. A node is then retrieved, upon user request (i.e. the appropriate HTTP GET), by the node module and passed on to the template which places in on its appropriate place in the layout and generates and returns the webpage (the actual final markup that is served to the browser) by adding in other components of the page (e.g. menus, blocks, etc.). What I find a bit fuzzy however is the concept of entity. A user is an entity, so is a node? How about a role, or a block? How about a comment or a menu? Are those entities too?I can’t yet quite see its place in the overall scheme.
  • To me, “entity” is just a fancy synonym for “thingie”, which is about as vague (or general/abstract) as you could get. Indeed, if knew what is NOT an entity, it might become clearer.