Skip to content

Instantly share code, notes, and snippets.

View isaumya's full-sized avatar
👨‍💻
Think Twice, Code Once

Saumya Majumder isaumya

👨‍💻
Think Twice, Code Once
View GitHub Profile
@isaumya
isaumya / wp-count-total-paragraph.php
Created September 19, 2016 15:16
Count the total number of paragraph on any WordPress blog post
<?php
function __check_paragraph_count_blog( $content ) {
global $post;
if ( $post->post_type == 'post' ) {
$count = substr_count( $content, '</p>' );
return $count;
} else {
return 0;
}
}
@isaumya
isaumya / manage-redirection-and-add-security-headers-via-cloudflare-workers.js
Last active March 15, 2022 19:02
Cloudflare Workers code to manage redirection of a domain and adding extra security headers to the correct hostname
/**
* CloudFlare Worker to handle each request
* and based on the given condition either redirect it to
* the proper URL
* OR add the security headers in case of Status 200
* @author Acnam Infotech
* @explanation https://acnam.com/why-and-how-to-use-cloudflare-workers-explained-with-sample-code/
*/
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
@isaumya
isaumya / http2-server-push-nuxt-js.md
Created October 27, 2019 16:47
Add HTTP 2 Server Push to Nuxt JS Application (Universal Mode) - Tested in Zeit Now

How to do HTTP/2 Server Push in Nuxt.JS App (Tested in Universal Mode & Hosted in Zeit Now)

While developing my Nuxt.js app, the one thing I tried to do most was HTTP/2 Server Push, as I am hosting my app on Zeit Now which does support both HTTP/2 and Server Push. The main reason I wanted to do it was I was using Bootstrap Vue on my project. Whcih add the Bootstrap CSS + Bootstrap Vue's own custom CSS as inline style to the document which was making the document huge. Moreover in bootstrap vue you can do bootstrapCSS: false which will not add the Bootstrap's default CSS to your page, but it will add it's own custom CSS.

Also, in my nuxt have I had a lot of CSS at the component/layout level, which are also getting added to the document as inline style. It was getting crazy. At this point I can either load the bootstrap from a CDN which costed me another external request or I can host it myself (Zeit does have CDN) on my own CDN and then do HTTP/2 server push. So, that is what I diid. I also load a style

@isaumya
isaumya / show-image.php
Created September 26, 2016 20:08
Showing image in WordPress works for both SVG & non SVG image
<?php
/*** Function to show images whether SVG or non SVG ***/
/*** $size & $attribute both can hold array if you want ***/
function show_image( $image_id, $size = null, $attributes = null ) {
//first lets get the file info sto understand what kind of file it is
//as for svg file we will take different approach
$file_info = pathinfo( wp_get_attachment_url( $image_id ) );
//so, if the file type is SVG
if ( $file_info['extension'] === 'svg' ) {
@isaumya
isaumya / customize-woocommerce-checkout-fields.php
Created August 21, 2020 10:31
Customizing WooCommerce Core Checkout Fields (Removing Address Line 2) and also change the placeholder text of Address Line 1
<?php
/**
* Remove Address Line 2 from WooCommerce Billing & Shipping Form
* Also add proper placeholder for Address line 1 to include the full address
* @author Acnam Infotech
*/
add_filter( 'woocommerce_checkout_fields', function( $fields ) {
if( is_array( $fields ) ) {
// Remove Address Line 2 from Billing & Shipping fields
unset( $fields['billing']['billing_address_2'] );
@isaumya
isaumya / woocommerce-conditional-product-shipping-cost.md
Created May 12, 2020 07:48
Enable or Disable Free Shipping or Flat Rate Shipping Based On WooCommerce Product

Let's say you have a product for which you just want to show the Free Shipping option or the Flat Rate shipping option, you can easuly do it using the following filters.

Hide The Flat Rate SHipping Option For Certain WooCommerce Product

add_filter( 'woocommerce_shipping_flat_rate_is_available', function( $is_available ) {
	// set the product ids that are eligible
	$eligible = array( '1711' );

	// get cart contents
@isaumya
isaumya / get-product-price-by-id-woocommerce.md
Created April 29, 2020 05:38
A function to get the simple product or variation product's price by passing the product_is or variation_id in WooCommerce.

When to use it?

Lets say in your template you are doing something like a pricing table, where instead of manually putting the price, you just want to pass the variation_id or the product_id (in case of simple product and dynamically get the price of the product.

This is helpful in many cases:

  • You don't have to manage price in two places & if you update the price of the product it will be reflected everywhere.
  • If you are using country based pricing and want to show the price accordingly.

The Function

@isaumya
isaumya / optimize-svg-files-with-svgo.md
Created April 25, 2020 09:50
Optimize SVG files with SVGO

Install SVGO

Intsll the node js tool SVGO

$ [sudo] npm install -g svgo

Run Command

svgo *.svg --multipass --enable=removeMetadata,removeTitle,removeDesc
@isaumya
isaumya / console-log-special-message.js
Created December 11, 2016 11:01
Showing special message in the console.log
/* This script will show a special designed message in the console.log section of your website
* for those who looks for something extra in your website.
* Fiddle Link: https://jsfiddle.net/isaumya/vtjswjuL/
**/
jQuery(function ($) {
try{
console.log("%c < ISAUMYA /> ","background: linear-gradient(to right, rgba(231,76,60,1) 0%,rgba(155,89,182,1) 100%););font-size:3em;border-radius:1em;color:#ffffff;font-weight:bold;font-style: italic;");
console.log('%c made with ♥ by Saumya Majumder © 2016', 'color: #e74c3c;font-size:1.1em;');
}
catch(e){}
@isaumya
isaumya / back-button-refresh.php
Created December 23, 2016 12:05
Refresh page when click browser back button
<?php
/**
* Adding a special function to ensure that when the user click on back button,
* it will reload the pages, instead of loading from cache so that the js can run again
**/
add_action( 'wp_footer', 'aicp_refresh_on_back' );
function aicp_refresh_on_back() {
echo '<input type="hidden" id="refreshed" value="no">
<script type="text/javascript">
onload=function(){