Wordpress- How to Stay Logged in for Longer Periods

Question: How to stay logged in for longer periods in wordPress?
Answer: Using below hook/filter/code user will stay stay logged in for longer periods in wordPress.
 
// WordPress filter to Stay logged in for longer periods
add_filter( 'auth_cookie_expiration', 'keep_me_logged_in' );
function keep_me_logged_in( $expirein ) {
    return 31556926; // 1 year in seconds
}

Magento - How to show all products of a category in only one page

Question: How can we show all products of a category in only one page?
Answer: You can find the solution in Magento backend,

it's like this :
Configuration > catalog > frontend > Allow All Products per Page > yes 

Woocommerce- Change number or products per row to 3

Question: How can we change number or products per row to 3 in woocommerce?
Answer:  We can this adding below filter in functions.php

// Change number or products per row to 3
add_filter('loop_shop_columns', 'loop_columns');
if (!function_exists('loop_columns')) {
    function loop_columns() {
        return 3; // 3 products per row
    }
} 

SSH- How to export/import a MySQL database via SSH

Question: How can we export/import a MySQL database via SSH commands?

Answer: we can export/import a MySQL database using below commands.

USERNAME - the MySQL user assigned to your database.

DATABASE - the name of your MySQL database.

1. Exporting a MySQL database

mysqldump -uUSERNAME -p DATABASE > backup.sql 

2. Importing a MySQL database

mysql -uUSERNAME -p DATABASE < backup.sql 

SSH- How to zip a folder via SSH Terminal

This is how you can zip a folder via SSH on your Linux server. Works on Debian and other servers.

zip -r filename.zip foldername/

jQuery- Stop YouTube video within iFrame on external Button click

 
//First get the  iframe URL
var url = jQuery('#YourIFrameID').attr('src');

//Then assign the src to null, this then stops the video been playing
jQuery('#YourIFrameID').attr('src', '');

// Finally you reasign the URL back to your iframe, so when you hide and load it again you still have the link
jQuery('#YourIFrameID').attr('src', url);

jQuery- show div on hover - hide div on mouseout

Question: How can we show div on hover and hide div on mouseout ?

Answer: Using below example code we can do this.

HTML
hover anchor

lorem ipsum dolor sit amet......

JS
(function(){
  var del = 200;
  $('.icontent').hide().prev('a').hover(function(){
    $(this).next('.icontent').stop('fx', true).slideToggle(del);
  });
})();

Magento 1.9 - Add to cart button on upsell product page in magento

To add "add to cart" button on upsell product page-

Follow below steps : -

STEP 1. open the upsell.phtml page:-

i.e.
app/design/frontend/default/your theme/template/catalog/product/list/upsell.phtml

STEP 2. Put the code where button to show-


Enjoy:)

Ajax- submit form using ajax (With file/ image)

Below is the HTML and script to submit form using ajax (With file/ image)

HTML

 
Image 1

Script

 
jQuery("form[name='uploader1']").submit(function(e) {
        var formData = new FormData(jQuery(this)[0]);
        jQuery.ajax({
            url: "http://your-domain/processupload.php",
            type: "POST",
            data: formData,
            async: false,
            success: function (msg) {
               // alert(msg)
                jQuery( "#success1" ).html(msg);
            },
            cache: false,
            contentType: false,
            processData: false
        });

        e.preventDefault();
    });

Note: Please be sure, jQuery libary has included for the page.

jQuery- Toggle Up and Dowm

HTML:
 

Click 1

Click 2

SCRIPT:
 
jQuery(document).ready(function() {
    jQuery('#faq-list h2').click(function() {
       jQuery(this).next('.answer').slideToggle(500);
       jQuery(this).toggleClass('close');
       
    });
}); // end ready
Important: Please make sure, jQuery library is included in the page.

Wordpress- Ajax Pagination (How to implement pagination on a custom WP_Query Ajax ?)

Question: How can we implement pagination on a custom WP_Query Ajax ?

Answer: We can follow the following steps.

1. Load More link.

Load More

2. Javascript: - Put this at the bottom of the file.

    var ajaxUrl = "";
    var page = 1; // What page we are on.
    var ppp = 3; // Post per page

    $("#more_posts").on("click",function(){ // When btn is pressed.
        $("#more_posts").attr("disabled",true); // Disable the button, temp.
        $.post(ajaxUrl, {
            action:"more_post_ajax",
           // offset: (page * ppp) + 1,
            offset: (page * ppp),
            ppp: ppp
        }).success(function(posts){
            page++;
            $(".name_of_posts_class").append(posts); // CHANGE THIS!
            $("#more_posts").attr("disabled",false);
        });
   });

3. Put this in the functions.php file.

function more_post_ajax(){
    $offset = $_POST["offset"];
    $ppp = $_POST["ppp"];
    header("Content-Type: text/html");

    $args = array(
        'post_type' => 'post',
        'posts_per_page' => $ppp,
        'cat' => 1,
        'offset' => $offset,
    );

    $loop = new WP_Query($args);
    while ($loop->have_posts()) { $loop->the_post();
       the_content();
    }

    exit;
}

add_action('wp_ajax_nopriv_more_post_ajax', 'more_post_ajax');
add_action('wp_ajax_more_post_ajax', 'more_post_ajax');

4. Enjoy :)

PHP- How can we convert string to slug?

Below is the function to convert string to slug.
 
function createSlug($str, $delimiter = '-'){

$slug = strtolower(trim(preg_replace('/[\s-]+/', $delimiter, preg_replace('/[^A-Za-z0-9-]+/', $delimiter, preg_replace('/[&]/', 'and', preg_replace('/[\']/', '', iconv('UTF-8', 'ASCII//TRANSLIT', $str))))), $delimiter));
  
 return $slug;

}
For example:
 
$str= 'this is slug'; //If this is the string which we have to convert as slug
$createdSlug= createSlug($str); //Call funtion
echo $createdSlug; // Print output
Output:
 
this-is-slug

Wordpress- How to disable responsive images srcset in WP 4.4+

We have to just paste below code in theme's function.php
 
//disable src set
function aft_disable_srcset( $sources ) {
    return false;
}
add_filter( 'wp_calculate_image_srcset', 'aft_disable_srcset' );

Wordpress- How can we customize custom logo dimension in child theme?

Question: How can we customize custom logo dimension in child theme?

Answer: Putting following code/hook in child theme functions.php, logo dimension could be customized.

If we want to add width=500 and height=200 for logo, we can use below filter.

add_action( 'after_setup_theme', 'child_theme_logo_customize', 99 );
function child_theme_logo_customize() {
    add_theme_support( 'custom-logo', array(
        'width'  => 500,
        'height' => 200,
    ) );
} 

CSS- Creating full width (100% ) container inside fixed width container.


Question: How can we create full   width (100% ) container inside fixed width container with CSS?
 
Answer: Some times we need to add a full width containers (which spans 100% of window) inside a container which has a fixed width and aligned center.

Like below screenshot-

HTML
 

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

--- Full width container ---

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

CSS
 
.row-full{
 width: 100vw;
 position: relative;
 margin-left: -50vw;
 height: 100px;
 left: 50%;
 background-color:red;
}