Author: Narender Singh

Myself Narender Singh Thakur ( NST ) and i share my Experience/Knowledge and Tricks for folks and beginners to solve their issues while making websites through this Planet.

add JS or CSS to website using functions.php file in wordpress!

How to properly add style and script to your wp website using functions.php file ?

Sometime we want to add CSS or JS to front-end of website without editing the parent theme files. like header.php or footer.php

So here is the simple function that help to add or apply your custom CSS code or js code to your front of website.

So here is code to add custom style to your website.

Note: Create a folder in your activate theme ( if not there ) with name CSS and a new file under that folder with name custom.css … so path will be like /css/custom.css .

Now add the bottom code in you functions.php file and hit on save button.

apply custom css to wp using functions file wp_enqueue
<?php 
function nst_custom_css(){
      $nst_csssrc = get_stylesheet_directory_uri() . '/css/custom.css';
      wp_enqueue_script( 'nst_custom_css', $nst_src , array(), '1.0',  false );   
}
add_action( 'wp_enqueue_scripts', 'nst_custom_css' );
?>

Now you can add your custom styles in your new file. ๐Ÿ™‚

Now the way to add Js code to your wp using functions file…

Like above

Note: Create a folder in your activate theme ( if not there ) with name JS and a new file under that folder with name custom.js … so path will be like /js/custom.js .

<?php
function nst_custom_js(){
      $nst_jssrc = get_stylesheet_directory_uri() . '/js/custom.js';
      wp_enqueue_script( 'nst_custom_js', $nst_src , array(), '1.0',  true );   
}
add_action( 'wp_enqueue_scripts', 'nst_custom_js' ); ?>

Now you can add your custom scripts in your new file. ๐Ÿ™‚

Just contact me if you still need help regarding this or any other issues.

Thanks ๐Ÿ™

Make changes in database after import to live wordpress!

Simple steps to make changes in database after import to live in wordpress or import to new domain , place, hosting.

Quickly change your wordpress database from the cpanel / phpmyadmin.

Here are simple code you have to edit and paste into your domain database SQL tab and then click on GO to process it..

Here is the code

UPDATE wp_options SET option_value = replace(option_value, 'http://www.oldurl', 'http://www.newurl') WHERE option_name = 'home' OR option_name = 'siteurl';

UPDATE wp_posts SET guid = replace(guid, 'http://www.oldurl','http://www.newurl');

UPDATE wp_posts SET post_content = replace(post_content, 'http://www.oldurl', 'http://www.newurl');

UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://www.oldurl','http://www.newurl');


Steps to follow :-

  1. As you see the above code.. you have to change the url of the domain path as per OLD and NEW.
  2. http://www.oldurl is path of where from you want to move.
  3. http://www.newurl new path of where you want to install or move.

Also please check the table prefix of your database.. if it is started with “wp_” then it’s okay other wise change this too as per your tables.

Click here ..how to check used database table prefix

Go to database and click on the database is used.

After this click on SQL tab and paste the above code .. i mean the one you changed now as per new and old url.

Like here ..

1) Click on used database
2) Then click on “SQL” tab.
3) Then paste the code in query box.
4) After this hit the “Go” button .. right side of the query box.
5) it will show you Successfully done message when the query is completed.

That’s it .. i hope this helps you and if still has any confusion then please contact me by chat box or contact forms.

Shopify to show product created and published date!

Sometime we want to show the product publish date on product template.

Here is the code to show product created and publish date on product page.

1) product.published_at :-  Returns the date and time the product was published.

2) product.created_at :-  Returns the date and time the product was created

For example:- I want to show the NEW badge on product pages which one are published less than 15 days ago.

 {% assign product_pub_at = product.published_at | date: '%s' %}
    
{% assign time_ago = 'now' | date: '%s' | minus: product_pub_at | divided_by: 86400 %}
                     
{% if time_ago < 15 %}
    <div class="new_banner prd">NEW</div>
{% endif %}  

For products added in the last 15 days or you can modify the days in coding.

shopify product created and published date

I hope this helps you ..

Thanks ๐Ÿ™‚

How to add Category name to body class wordpress!

What is the purpose of body_class function in wordpress ?

body_class function helps to show the bunch of classes name to the body element that have information about what kind of page is currently being displayed.

Here is the way to implement body_class function into theme template.

<body <?php body_class(); ?>>

So now we want to add category name to body class .. mostly for styling purposes. ๐Ÿ˜‰

Add this code to your functions.php file and hit on save button.

add_filter('body_class','nst_add_category_name');
function nst_add_category_name($classes) {
if (is_single() ) {
global $post;
foreach((get_the_category($post->ID)) as $category) {
// add category slug name to the $classes array
$classes[] = $category->category_nicename;
}
}
// return the $classes array here
return $classes;
}

I hope this helps you.. Thanks ๐Ÿ™‚

How to show Estimate Delivery Date on product Page in Shopify!

Here is one of the way to Show Estimate Delivery Date and time of products on product page template under add to cart button in Shopify.

NOTICE : Please do only if you have little bit knowledge of files.. otherwise you can contact your developer or Just drop a message for me in chat box.

1. Login to your Shopify website admin and visit to Online Store Section and then Themes option.

shopify online store theme option

2. Edit your theme by click on Edit Code option.

shopify edit code

3. From left sidebar under Sections tab find product-template.liquid file.

section from shopify online store

3. Open product-template.liquid file and scroll to the end of form tag </form> .. like here..

4. Now add these line of code there ..

  {% comment %}          
    To show under add to cart product page.          
  {% endcomment %}          
                    
        {% assign mont = 'now' | date: '%m'  %}            
          
         {% if mont == '01' %}          
            {% assign nexm = 'Feb' %} 
         
          {% elsif  mont == '02' %}          
            {% assign nexm = 'Mar' %}
          
          {% elsif  mont == '03' %}          
            {% assign nexm = 'Apr' %}
          
          {% elsif  mont == '04' %}          
            {% assign nexm = 'May' %}
          
          {% elsif  mont == '05' %}          
            {% assign nexm = 'Jun' %}
          
          {% elsif  mont == '06' %}          
            {% assign nexm = 'Jul' %}
          
          {% elsif  mont == '07' %}          
            {% assign nexm = 'Aug' %}
          
          {% elsif  mont == '08' %}          
            {% assign nexm = 'Sep' %}
          
          {% elsif  mont == '09' %} 
            {% assign nexm = 'Oct' %}
          
          {% elsif  mont == '10' %}          
            {% assign nexm = 'Nov' %}
          
          {% elsif  mont == '11' %}
            {% assign nexm = 'Dec' %}
          
          {% else %}          
            {% assign nexm = 'Jan' %}          
         {% endif %}          
         
          
  {% if mont == '04' or mont == '06' or mont == '09' or mont == '11' %}  
        {% assign f_mont = '30' %}
        {% elsif  mont == '02' %}
        {% assign f_mont = '28'%}
        {% else %}
        {% assign f_mont = '31'%}  
  {% endif %}  
          
         {% assign nexm = nexm | append: ". " %} 
         {% assign c_day = 'now' | date: '%d' %} 
         {% assign tday = f_mont | minus: c_day %}           
         
          
      {% if tday < 7 %}  
         {% assign time_inc = 7 | minus: tday | prepend: nexm %}
      {% else %}          
         {% assign mon =  'now' | date: "%b" | append: ". " %}
         {% assign time_inc = c_day | plus: 7 | prepend: mon %}          
      {% endif %}          
                            
    <div class="us-shp  ">โœˆ๏ธ U.S. Delivery {{time_inc}}</div>  

          
         
          

You can style it as per your theme color.

After add this click on save button and check the result on product page now.

It will be similar to this…

shopping in shopify date

This date will auto update the date of delivery by 7 days from today’s date.

You can alter the code as per your need … if you want any help then contact me please.

Thanks ๐Ÿ™‚

Shopify apply discount code as per if cart has particular product

So here are the way to apply manual discount automatically on checkout page.. if particular products are in cart items.

You can also use automatic discount for same purpose but if 1 automatic is already active then you can choose this 2nd way to apply auto discount.

As currently shopify plus support only one automatic discount at a time.

So here is the script you have to apply on cart liquid file of your theme. Mine is ( cart-template.liquid ) in Debut theme.

NOTE : Please create a normal discount in admin first then you can apply that code there in script.

Here is the way to add discount code in shopify….

Like in the above screenshot.. click on create discount button. New window will be like this..

Choose first one and next window will be..

create discount code

Add your discount code there like helo or something you like or as per your purpose..

So we have now discount code name “helo” ..

add this script to near after form start.. on your theme cart liquid file.
<form action=”/cart” method=”post”>

if you want to match single product in cart item then..

   {% assign prod = false %}  
      
{% for item in cart.items %} 
      
  {% if item.product.title == 'Your product name or title' %}      
         {% assign prod = true %}      
  {% endif %}            
   
{% endfor %}
      
{% if prod %}      
     <input type="hidden" name="discount" value="helo"/>        
{% endif %}

Please change “Your product name or title” to with your product name.

If you want to match two products then follow this script..

   {% assign prod = false %}
   {% assign prod1 = false %}
      
{% for item in cart.items %} 
      
  {% if item.product.title == 'Your product name or title' %}      
         {% assign prod = true %}      
  {% endif %}
            
  {% if item.product.title == 'Your product name or title 2' %}      
       {% assign prod1 = true %}         
  {% endif %}    
  
{% endfor %}
      
{% if prod and prod1 %}      
     <input type="hidden" name="discount" value="helo"/>        
{% endif %}

So above scripts will only add discount on cart page if they matched to your mentioned products name and discount code will reflect on checkout page automatically.

You can also show message to cart page like this.. showing in red color on right side of screenshot.

show message on cart page shopify

add this one script to where you want to show message on cart page.

 {% if prod and prod1 %}            
    <strong class="cod-check1">*Your discount will apply in checkout.</strong> 
  {% endif %}

I hope this will help you .. If you need more help then contact with me using chat box.

Thanks ๐Ÿ™

How to check and download correct database used in your wp website ?

Here is the way to find the correct database used for your website..

Login to your website hosting and Click on File Manager.

find database

Now find the file name wp-config.php and right click on that file to view it.

Now check your database name like here..

Now go to main homepage of your cpanel and click on phpMyAdmin icon.. like here..

phpmyadmin

Click on that and  new window will openโ€ฆlike this .. so follow the steps i mentioned thereโ€ฆ select the correct database to export and than click on โ€œExportโ€ 3 point top right.

export database

After click on โ€œExportโ€.. new window will be..

how to export database in phpmyadmin

After Click on Go .. database will be downloaded to your machine .

That’s it .

Thanks ๐Ÿ™

Click to Chat