Skip to content

Instantly share code, notes, and snippets.

View michael-sumner's full-sized avatar
:electron:
Making the world better with incredible digital experiences

Michael Sumner michael-sumner

:electron:
Making the world better with incredible digital experiences
View GitHub Profile
@michael-sumner
michael-sumner / .htaccess
Created February 5, 2022 19:07
Redirect old domain to new domain and all pages within it, using htaccess file.
# BEGIN DOMAIN REDIRECT 301 TOWARDS newdomain.com
# ADD THE CODE WITHIN YOUR OLDDOMAIN.COM
# MODIFY THE www TO YOUR SPECIFIED SUBDOMAIN
# IF YOU ARE USING A NAKED DOMAIN, THEN REMOVE !^www\.
# IF YOUR NEWDOMAIN RESOLVES TO ANOTHER SUBDOMAIN
# THEN USE THAT NEW SUBDOMAIN INSTEAD OF www.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.newdomain\.com
RewriteRule (.*) https://www.newdomain\.com/$1 [R=301,L]
# USEFUL FOR:
@michael-sumner
michael-sumner / wp-perf.md
Created October 21, 2022 09:40 — forked from Ruzgfpegk/wp-perf.md
WordPress Performance & Development tips
@michael-sumner
michael-sumner / markup.php
Created April 19, 2023 11:49
You can load the script of the block within the php file
<?php
/**
* Block markup
*
* @package MyNamespace\Blocks\MyBlock
*
* @var array $attributes Block attributes.
* @var string $content Block content.
* @var WP_Block $block Block instance.
@michael-sumner
michael-sumner / add_tag_to_2022_posts.sh
Created April 21, 2023 15:51
The script uses WP-CLI to add a tag with specified ID to all posts published in the year 2022. It first gets a list of post IDs for the year 2022 and then loops through each post ID to add the tag. The script is useful for quickly adding a tag to a large number of posts without manual editing in the WordPress admin interface.
#!/bin/bash
###
# The script uses wp post list command to get a list of post IDs from the year 2022 and saves them in a file called post_ids.txt. It then loops through each post ID in the file and adds the tag with ID 47365 to the post using the wp post term add command with the post_tag taxonomy.
# Please note that you need to have the post_tag taxonomy registered for posts in your WordPress site and you may need to replace 47365 with the actual ID of the tag you want to add.
#
# Save the script with a filename like add_tag_to_2022_posts.sh, make it executable using the chmod +x add_tag_to_2022_posts.sh command, and run it by executing ./add_tag_to_2022_posts.sh in your terminal.
###
# Get all posts from the year 2022
@michael-sumner
michael-sumner / search-post-meta.sh
Last active May 10, 2023 13:12
This script prompts the user to enter a postmeta key to search for, and then retrieves the value of that postmeta key for each post on your WordPress site using the WP-CLI command wp post meta get. If the postmeta value is not "null", then the script prints the post ID to the console. This script allows you to quickly find all posts on your site…
#!/bin/bash
# Prompt the user to enter a postmeta key to search for
read -p "Enter the postmeta key to search for: " postmeta_key
# Find all posts with the specified postmeta key
post_ids=$(wp post list --format=ids)
for id in $post_ids
do
meta_value=$(wp post meta get $id $postmeta_key --format=json)
@michael-sumner
michael-sumner / functions.php
Last active May 18, 2023 17:35
get_recent_post_ids() Gets an array of recent post IDs based on the number of posts. Contains caching mechanism for high-performance. ⚡️
<?php
/**
* Gets an array of recent post IDs based on the number of posts.
*
* @param int $num_posts The number of recent posts to retrieve. Defaults to 6.
* @param int $expiration_time The expiration time for the cache, in seconds. Defaults to 1 hour.
*
* @return array An array of recent post IDs.
*/
@michael-sumner
michael-sumner / index.sh
Created May 26, 2023 12:04
Copy git diff staged to clipboard for macOS
git diff --staged | pbcopy
<?php
// List down all the functions assigned to the "the_content" filter hook.
function get_the_content_filters() {
$filters = array();
ob_start();
the_content();
$output = ob_get_clean();
global $wp_filter;
<!doctype html>
<html class="bg-gray-100">
<head>
<title>Checksum Generator</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
<div
class="min-h-screen flex items-center justify-center py-12 px-4 sm:px-6 lg:px-8"
>
@michael-sumner
michael-sumner / functions.php
Created September 5, 2023 15:23
Disable WordPress core attempts to redirect old posts.
<?php
// Disable WordPress core attempts to redirect old posts.
add_filter( 'old_slug_redirect_post_id', '__return_zero' );
add_filter( 'redirect_canonical', '__return_empty_string' );