Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

Gaurav Tiwari wpgaurav

🎯
Focusing
View GitHub Profile
@wpgaurav
wpgaurav / reduce-comment-spam.php
Created Apr 8, 2020
Reduce Comment Spam WordPress
View reduce-comment-spam.php
<?php
// Copy this code in your theme's functions.php file before closing ?>
// This removes website field from comment forms
add_filter('comment_form_default_fields', 'website_remove');
function website_remove($fields)
{
if(isset($fields['url']))
View one-click-screenshot.html
<!DOCTYPE html>
<html lang="en-US" prefix="og: http://ogp.me/ns#">
<head>
<title>One click screenshot of any webpage</title>
<meta name="description" content="Get a one to two click screenshot of any webpage in .png. Download and use it on blogs and websites." />
<meta name="robots" content="follow, index, , max-snippet:-1, max-video-preview:-1, max-image-preview:large" />
</head>
<body>
<form id="your_form" onsubmit="yourFunction()" target="_blank">
<input type="url" name="url">
@wpgaurav
wpgaurav / estimated-reading-time.php
Last active Apr 9, 2020
Want to tell your readers how long it will take an article to read completely? Insert the following code into the right file (e.g., single.php or page.php) in the right place. Visit https://gauravtiwari.org/code/ for more
View estimated-reading-time.php
<?php
// Insert the following code into the right file (e.g., single.php or page.php) in the right place (above post meta and below post title) in your theme. It counts total words in an article and calculates time by estimating that on average a reader can read 200 words in a minute.
$mycontent = $post->post_content;
$word = str_word_count(strip_tags($mycontent));
$m = floor($word / 200);
// considering 200 words per minute is human ready capacity average
$s = floor($word % 200 / (200 / 60));
$est = $m . ' minute' . ($m == 1 ? '' : 's') . ', ' . $s . ' second' . ($s == 1 ? '' : 's');
?>
@wpgaurav
wpgaurav / functions.php
Created Apr 9, 2020
Hook/Add a custom editor.css file to Gutenberg Block editor.
View functions.php
<?php
add_action(
'enqueue_block_editor_assets',
function () {
$theme = wp_get_theme();
wp_enqueue_style(
'gt-main-editor-styles',
get_stylesheet_directory_uri() . '/custom-editor.css',
[],
@wpgaurav
wpgaurav / google-auto-ads-posts-only.php
Created Apr 9, 2020
Add Google Auto Ads to posts only
View google-auto-ads-posts-only.php
<?php add_action('wp_head', 'google_ads');
function google_ads() {
if (is_singular('post')){
echo '<script data-ad-client="ca-pub-1495783196891276" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>';
}
// Change data-ad-client="ca-pub-1495783196891276" with your data-ad-client
}
?>
@wpgaurav
wpgaurav / svg-support.php
Created Apr 9, 2020
Add SVG file format support to WordPress
View svg-support.php
<?php
// Add these to child theme/theme's functions.php file
function cc_mime_types($mimes) {
$mimes['svg'] = 'image/svg+xml';
return $mimes;
}
add_filter('upload_mimes', 'cc_mime_types');
?>
@wpgaurav
wpgaurav / excerpt-custom.php
Created Apr 13, 2020
Custom Excerpt for MD theme
View excerpt-custom.php
<?php function new_excerpt_more($more) {
global $post;
remove_filter('excerpt_more', 'new_excerpt_more');
return ' <a class="read-more" href="'. get_permalink($post->ID) . '">' . 'Read more &rarr;' . '</a>';
}
add_filter('excerpt_more','new_excerpt_more');
?>
@wpgaurav
wpgaurav / custom-to-rss.php
Created Apr 25, 2020
Add your custom post types to your sites main RSS feed by default.
View custom-to-rss.php
<?php // ADD CUSTOM POST TYPES TO THE DEFAULT RSS FEED
function custom_feed_request( $vars ) {
if (isset($vars['feed']) && !isset($vars['post_type']))
$vars['post_type'] = array( 'post', 'site', 'plugin', 'theme', 'person' );
return $vars;
}
add_filter( 'request', 'custom_feed_request' );
?>
@wpgaurav
wpgaurav / custom-in-search.php
Created Apr 25, 2020
Include custom post types in the search results.
View custom-in-search.php
<?php // MAKE CUSTOM POST TYPES SEARCHABLE
function searchAll( $query ) {
if ( $query->is_search ) { $query->set( 'post_type', array( 'site', 'plugin', 'theme', 'person' )); }
return $query;
}
add_filter( 'the_search_query', 'searchAll' );
?>
View dark-mode.scss
@media (prefers-color-scheme: dark) { body, .blog-teaser, article, .sidebar > *, .has-white-background-color, .style-default .box-style, .style-default .box-style-list ul, .style-default .tagcloud, .style-default #wp-calendar, .header, article div, .amzn-native-product-title-container a, ol, ul, .megamenu, .sub-menu, .content-inner > *{
background:#333!important;
}
a, li, li a, h1, h2, h3, h4, h5, h6, p, blockquote, format > *, .sidebar > *, .blog-teaser h2 a, div, .content-inner > *{
color:#eee!important;
}
img{
opacity:0.8;
}
.header-logo img{