All posts by Rampit

test

jquery get CSS style property value

Use the below code to get the particular CSS style property value. Within the below example, its get and displaying the “background-color” value as “blue”.

           <div style="background-color:blue;"></div>
<script>
$( "div" ).click(function() {
                       var color = $( this ).css( "background-color" );
alert(color);  // Use this value
              });
</script>

Create Thumb image in PHP

function makeThumbnails($updir, $img, $id)
{
$thumbnail_width = 134;
$thumbnail_height = 189;
$thumb_beforeword = "thumb";
$arr_image_details = getimagesize("$updir" . $id . '_' . "$img"); // pass id to thumb name
$original_width = $arr_image_details[0];
$original_height = $arr_image_details[1];
if ($original_width > $original_height) {
$new_width = $thumbnail_width;
$new_height = intval($original_height * $new_width / $original_width);
} else {
$new_height = $thumbnail_height;
$new_width = intval($original_width * $new_height / $original_height);
}
$dest_x = intval(($thumbnail_width - $new_width) / 2);
$dest_y = intval(($thumbnail_height - $new_height) / 2);
if ($arr_image_details[2] == 1) {
$imgt = "ImageGIF";
$imgcreatefrom = "ImageCreateFromGIF";
}
if ($arr_image_details[2] == 2) {
$imgt = "ImageJPEG";
$imgcreatefrom = "ImageCreateFromJPEG";
}
if ($arr_image_details[2] == 3) {
$imgt = "ImagePNG";
$imgcreatefrom = "ImageCreateFromPNG";
}
if ($imgt) {
$old_image = $imgcreatefrom("$updir" . $id . '_' . "$img");
$new_image = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
imagecopyresized($new_image, $old_image, $dest_x, $dest_y, 0, 0, $new_width, $new_height, $original_width, $original_height);
$imgt($new_image, "$updir" . $id . '_' . "$thumb_beforeword" . "$img");
}
}

WooCommerce products per page and List all products

Add below code in function,php to list only particular count product in product list page. Below code will list 12 products per page.

add_filter( 'loop_shop_per_page', create_function( '$cols', 'return 12;' ) );

Use below code to list “All products” in one page.  So, URL should have “URL?showall=1

if( isset( $_GET['showall'] ) ){
add_filter( 'loop_shop_per_page', create_function( '$cols', 'return -1;' ) );
} else {
add_filter( 'loop_shop_per_page', create_function( '$cols', 'return 12;' ) );
}

Speed Up web page using Prerender and prefetch

We could Load Web Page Faster using Prerender and prefetch support.

rel=prerender will load the next page in the Browser background.

It will work in background, while you are surfing the page you are on. The browser will download and keep the next page ready, but it will displayed when the link to Next page is Clicked.

Also, it will not affect the current page loading process.


‘prerender’
for Chrome and will load the top level resource like HTML.

‘prefetch’
for Firefox and will execute Javascript code.

rel=”prerender prefetch” will support all HTML5 browsers like IE 11, Opera 12.10
So, below code used to load the WordPress Posts quickly while use navigating into other posts and Home page.

<?php if (is_archive() && ($paged > 1) && ($paged < $wp_query->max_num_pages)) { ?>
<link rel="prefetch" href="<?php echo get_next_posts_page_link(); ?>">
<link rel="prerender" href="<?php echo get_next_posts_page_link(); ?>">
<?php } elseif (is_singular()) { ?>
<link rel="prefetch" href="<?php bloginfo('home'); ?>">
<link rel="prerender" href="<?php bloginfo('home'); ?>">
<?php } ?>

WordPress Security

Follow the below steps to secure your Blog

  1. Block wp-config file access.
    Add below code into “.htaccess” file which is in your root folder. Place this code outsideof the  # BEGIN WordPress and # END WordPress tags.
    <files wp-config.php>
    order allow,deny
    deny from all
    </files>
  2. Disable Directory access through Browsing
    # Disable directory browsing
    Options All -Indexes

  3. Securing wp-includes and wp-admin includes.
    Includes folder is common for all wordrpess site. So, we need to secure this folders. Add below code in “.htaccess” file.
    # Block the include-only files.
    RewriteEngine On
    RewriteBase /
    RewriteRule ^wp-admin/includes/ - [F,L]
    RewriteRule !^wp-includes/ - [S=3]
    RewriteRule ^wp-includes/[^/]+\.php$ - [F,L]
    RewriteRule ^wp-includes/js/tinymce/langs/.+\.php - [F,L]
    RewriteRule ^wp-includes/theme-compat/ - [F,L]
  4. Change and make sure the file permission
    Below is the default file permission for Files and Folder. Please check it and make sure its not changed into any other permission.Need to change the common file permission as like below. We could do it through FTP.
    wp-config.php(root) file permission set to 440 or 400
    – All directories set to 755 or 750
    – All files set to 644 or 640
  5. Disable File Editing
    In Wordrpess admin we have the option to edit the theme files, using below code we could disable that option.
    define('DISALLOW_FILE_EDIT', true);
  6. Remove the WordPress version
    Add below code into the wp-config.php file to remove the WordPress version from user-end
    remove_action('wp_head', 'wp_generator');