Skip to content

Instantly share code, notes, and snippets.

View wpgaurav's full-sized avatar
👋
Hello!

Gaurav Tiwari wpgaurav

👋
Hello!
View GitHub Profile
@wpgaurav
wpgaurav / add_to_functions.php
Created March 12, 2024 15:09
Add browser like frame to images in Gutenberg/Block Editor. Demo https://gauravtiwari.org/work/
<?php add_action('init', function() {
register_block_style('core/image', [
'name' => 'browser-shot',
'label' => __('BrowserShot', 'md'),
'inline_style' => '.is-style-browser-shot, .browser-shot { border-top: 2em solid rgba(230, 230, 230, 0.7); box-shadow: 0 0.1em 1em 0 rgba(0, 0, 0, 0.2); position: relative; border-radius: 3px 3px 0 0; background:#fff; } .is-style-browser-shot:before, .browser-shot:before { display: block; position: absolute; content: ''; top: -1.25em; left: 1em; width: 0.5em; height: 0.5em; border-radius: 50%; background-color: #f44; box-shadow: 0 0 0 2px #f44, 1.5em 0 0 2px #9b3, 3em 0 0 2px #fb5; }'
]);
});
@wpgaurav
wpgaurav / theStick on Scroll Header.css
Last active February 25, 2024 03:56
Stick on Scroll Header
body:not(.transparent-header) #header.header-standard.stick-on-scroll {
position: -webkit-sticky;
position: sticky;
top: 0;
/* z-index: 9; */
background-color: rgb(255 255 255 / 89%);
backdrop-filter:blur(5px);
}
.admin-bar .header-standard.stick-on-scroll {
top: 32px!important;
@wpgaurav
wpgaurav / floating-link-rm-seo.html
Created February 25, 2024 03:39
Add a floating Link/Button to take reader to Rank Math SEO's Table of Contents on the page.
@wpgaurav
wpgaurav / summarize-content.php
Last active February 4, 2024 16:21
Summarize WordPress post content using PHP
<?php
// A function that takes a content string and returns an array of up to 3 sentences with the highest scores
function gtcontent_summarize($content) {
$content = str_replace("","'", $content);
$content = str_replace("; ",".", $content);
$content = remove_h2_to_h6_and_no_period($content);
$content = wp_strip_all_tags($content,true);
$content = wp_trim_words($content,700);
@wpgaurav
wpgaurav / copy-paste.html
Created January 24, 2024 07:53
My Homepage Hero Design using GenerateBlocks
<!-- wp:generateblocks/container {"uniqueId":"c9ad37d4","tagName":"section","backgroundColor":"#080808","gradientDirection":90,"gradientColorOne":"rgba(255, 255, 255, 0.1)","gradientColorTwo":"rgba(0, 0, 0, 0.30)","textColor":"#ffffff","bgImage":{"id":1115539,"image":{"url":"https://gauravtiwari.org/wp-content/uploads/2024/01/Gaurav-Tiwari-Cover.webp","height":1277,"width":2362,"orientation":"landscape"}},"bgOptions":{"selector":"pseudo-element","opacity":0.51,"overlay":false,"position":"center right","size":"cover","repeat":"no-repeat","attachment":""},"isDynamic":true,"blockVersion":4,"position":"relative","overflowX":"hidden","overflowY":"hidden","spacing":{"paddingTop":"120px"},"borders":{"borderBottomWidth":"1px","borderBottomStyle":"solid","borderBottomColor":"#e5e7eb"}} -->
<!-- wp:generateblocks/container {"uniqueId":"4a4e281f","bgOptions":{"selector":"element","opacity":1,"overlay":false,"position":"center center","size":"contain","repeat":"no-repeat","attachment":"fixed"},"isDynamic":true,"blockVers
@wpgaurav
wpgaurav / aria-label-fix.js
Created January 19, 2024 08:57
Add aria-labels to important HTML tags (works with any theme).
document.addEventListener('DOMContentLoaded', function() {
// Select specific tags.
var tags = document.querySelectorAll('header, main, footer, article, nav');
// Function to convert ID to a readable format.
function formatIdToReadableText(id) {
// Replace non-alphanumeric characters with spaces and trim any extra spaces.
var formatted = id.replace(/[^a-zA-Z0-9]/g, ' ').trim();
// Capitalize the first letter of each word.
@wpgaurav
wpgaurav / fix-md-accessibility.js
Created January 19, 2024 08:55
Fix for Marketers Delight Social Share accessibility
document.addEventListener('DOMContentLoaded', function() {
// Find all elements with the class 'share-text'
var shareTextElements = document.querySelectorAll('.share-text');
shareTextElements.forEach(function(shareTextElement) {
// Find child elements with a class starting with 'md-icon-'
var shareIcons = shareTextElement.querySelectorAll('[class*="md-icon-"]');
shareIcons.forEach(function(icon) {
// Extract the part of the class name after 'md-icon-'
@wpgaurav
wpgaurav / featured-image-to-rss.php
Created November 20, 2023 17:32
Add featured image to RSS Feed
@wpgaurav
wpgaurav / Google Analytics4.html
Created November 20, 2023 17:31
Minimal Google Analytics 4
<script>
window.pmGAID="G-MABC1234"; // Replace G-MABC1234 with your Google Analytics tracking code
</script>
<script async src="https://gauravtiwari.org/static/js/analytics-minimal-v4.js"></script>
<!-- Download and upload this script to your host for the best performance. -->
@wpgaurav
wpgaurav / display-gravatar.php
Created November 20, 2023 17:28
Save Gravatars Locally and Display on WordPress Frontend
<?php
// Paste this in Code Snippets or Child Theme's functions.php file. Get Code Snippets at https://gauravtiwari.org/go/code-snippets
function my_custom_gravatar($avatar, $id_or_email, $size, $default, $alt) {
$email = '';
if (is_numeric($id_or_email)) {
$id = (int) $id_or_email;
$user = get_userdata($id);
if ($user) $email = $user->user_email;