Before Mapping the Domain and switching the DNS records, we could test the development site in shared hosting using below Options!
All posts by Rampit
jquery trim last ‘N’ characters
Below the example to trim last two characters.
<script>
var content_string = 'RAMP IT';
var content_string_name = content_string.slice(0, -2); //=> slice(0, -N) - update N based on your need.
alert(content_string_name);
</script>
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");
}
}
Add CSS into wordrpess admin
Add below code into function.php within the active theme folder.
<?php
add_action('admin_head', 'your_custom_func');
function your_custom_func() {
echo '<style>
/* Add your custom style here */
</style>';
}
?>
Above code will add the style into admin header.
admin_head — Admin header hook. Its hooked by add_action.
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 } ?>
wp_list_categories not listing the Category while post not mapped
Need to add below code, to display the empty category links. This will display the all category links.
$args = array('hide_empty' => FALSE);
wp_list_categories($args);
Default value for “hide_empty” is “TRUE”.
Shortcode not displaying in correct location
To display the Wordpres Shortcode return value in correct location, we need to use the “ob_start()” and “ob_get_clean()” as like below.
<?php
function your_shortcode_fun( $atts ){
ob_start();
?> <HTML> <here> ... <?php
return ob_get_clean();
}
add_shortcode( 'your_shortcode_name', 'your_shortcode_fun' );
?>
It will place the output in correct location.
WordPress Security
Follow the below steps to secure your Blog
- 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>
- Disable Directory access through Browsing
# Disable directory browsing Options All -Indexes
- 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]
- 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 - 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);
- 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');