Skip to content

Instantly share code, notes, and snippets.

View developer-anuragsingh's full-sized avatar

Anurag Singh developer-anuragsingh

  • Cendyn
  • Gurugram
View GitHub Profile
<?php
/**
* Post's comment form validation rules
* Path : wp-content/mu-plugins/comment-form-validations.php
*/
function ans_comment_validation_rules()
{
if (is_single() && comments_open()) { ?>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/jquery.validate.min.js"></script>
<script type="text/javascript">
@developer-anuragsingh
developer-anuragsingh / .htaccess
Last active November 8, 2022 12:34
301 redirect www to non-www & 301 Redirect HTTP to HTTPS
# BEGIN Redirects
RewriteEngine On
# 301 redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# 301 redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
@developer-anuragsingh
developer-anuragsingh / php.json
Last active October 17, 2022 08:16
VS Code - PHP snippets to add conditions for environment variable and developer variable
{
// HOW TO ADD COOKIE - add following code in browser's console - document.cookie = 'env=dev'
// HOW TO DELETE COOKIE - Open 'Applications' tab in browser console, then 'Cookies' tab in 'Storage' section. Find the cookie name under 'url'. Then right click on cookie name and click 'delete'
"Setup development environment": {
"prefix": "dev_env",
"body": [
"if(isset($$_COOKIE['env']) && $$_COOKIE['env'] === 'dev') {",
"$1",
"}"
],
@developer-anuragsingh
developer-anuragsingh / comment-form-validations.php
Created September 20, 2022 11:43
Wordpress Comment Form Validations
<?php
/**
* Post's comment form validation rules
* Path : wp-content/mu-plugins/comment-form-validations.php
*/
function ans_comment_validation_rules()
{
if (is_single() && comments_open()) { ?>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/jquery.validate.min.js"></script>
<script type="text/javascript">
@developer-anuragsingh
developer-anuragsingh / Pages sitemap
Last active October 28, 2022 07:25
Add WordPress's html sitemap with Shortcode. Also exclude pages which having 'sitemap' word in the title.
<?php
/**
* Create sitemap with shortcode
*/
function ans_sitemap($atts)
{
// Get all pages
$pages = get_pages();
foreach ($pages as $page) {
@developer-anuragsingh
developer-anuragsingh / functions.php
Created August 8, 2022 12:31
Add defer in script calls
function defer_parsing_of_js( $url ) {
if ( is_user_logged_in() ) return $url; //don't break WP Admin
if ( FALSE === strpos( $url, '.js' ) ) return $url;
if ( strpos( $url, 'jquery.js' ) ) return $url;
return str_replace( ' src', ' defer src', $url );
}
add_filter( 'script_loader_tag', 'defer_parsing_of_js', 10 );
@developer-anuragsingh
developer-anuragsingh / gist:dde788a6aa022123500fc162da6d021e
Created April 26, 2022 10:55
Force site to load securely with an .htaccess file
# Redirect http to https
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
Header always set Content-Security-Policy "upgrade-insecure-requests;"
@developer-anuragsingh
developer-anuragsingh / remove-plugins-scripts.php
Created April 12, 2022 14:23
Remove plugin's stylesheet from WordPress front-end
function dequeue_plugin_style(){
wp_dequeue_style( 'page-list-style' ); //Stylesheet handler name
}
add_action( 'wp_enqueue_scripts', 'dequeue_plugin_style', 999 );
@developer-anuragsingh
developer-anuragsingh / Remove Contact Form 7 Scripts and Stylesheet if short-code not found in post content
Last active August 25, 2022 13:15
Remove Contact Form 7 & Google reCaptcha Scripts and Stylesheet if short-code not found in post content to improve page speed.
/**
* Remove Google recaptcha and contact form 7 recaptcha scripts & stylesheets
* if shortcode not found in post-content
*/
add_action('wp_enqueue_scripts', function () {
global $post;
if ( is_a( $post, 'WP_Post' ) && !has_shortcode( $post->post_content, 'contact-form-7') ) {
// if shortcode not found
wp_dequeue_script( 'google-recaptcha' );
wp_dequeue_script( 'wpcf7-recaptcha' );
@developer-anuragsingh
developer-anuragsingh / wp-config.php
Created April 6, 2022 13:30
Add Code in wp-config.php file to enable error reporting in dev environmemnt
// enable debugging and error reporting on, if dev varriable is set
if( isset($_REQUEST['dev']) && (1 == $_REQUEST['dev']) ) {
define('WP_DEBUG', true);
} else {
// else off the debugging
define('WP_DEBUG', false);
}