Skip to content

Instantly share code, notes, and snippets.

View davemac's full-sized avatar

davemac davemac

View GitHub Profile
@davemac
davemac / enqueue Google fonts with preconnect
Last active August 28, 2023 17:38
WordPress enqueue google font with preconnect
wp_enqueue_style( 'dmc-google-font-fragment', 'https://fonts.googleapis.com/css2?family=Montserrat:wght@300&family=Roboto:ital,wght@0,500;1,400&display=swap', array(), null );
function dmc_google_font_loader_tag_filter( $html, $handle ) {
if ( $handle === 'dmc-google-font-fragment' ) {
$rel_preconnect = "rel='stylesheet preconnect'";
return str_replace(
"rel='stylesheet'",
$rel_preconnect,
$html
@davemac
davemac / nginx-redirect-uploads-to-production
Last active August 9, 2023 04:51 — forked from iansvo/nginx-redirect-uploads-to-production
Open the Nginx configuration for your site and look for a server block that listens for port 443. Look for something like Listen 127.0.0.1:443 ssl in the server { ... } block to find the right one. Add the following location rules near the top of the block, after any other definitions.
# Redirect requests to /wp-content/uploads/* to production
set $production themotoringnetwork.com.au;
location @prod_uploads {
rewrite "^(.*)/wp-content/uploads/(.*)$" "https://$production/wp-content/uploads/$2" break;
}
# Rule for handling local requests for images
location ~ "^/wp-content/uploads/(.*)$" {
try_files $uri @prod_uploads;
@davemac
davemac / getlinks
Last active April 4, 2023 04:32
bash wget a remote URL, then extract the URLs from the anchor tags in that URL
@davemac
davemac / wp-cli delete all featured images of a post type
Last active September 5, 2022 12:43
wp-cli delete all featured images of a post type
wp post list --post_type=dmc-sief-project --fields=ID --format=csv | xargs -I % wp post meta delete % _thumbnail_id
@davemac
davemac / pushstage()
Last active March 3, 2022 13:51
push a local WP database to an existing staging site
# push a local WP database to an existing staging site
# uses current directory as theme path and ssh alias
pushstage() {
START=$(date +%s)
# make a backup of the current local database
# get current directory name, used for database and URL
current=${PWD##*/}
cd ~/Sites/$current || return
# rsync the local database to staging site
@davemac
davemac / pullprod
Last active July 10, 2021 03:42
bash alias using WP-CLI to pull a remote WP database into a local site
# pull a production WP database to an existing local site
# uses current directory as theme path and ssh alias
pullprod() {
START=$(date +%s)
# get current directory name, used for database and URL
# TODO: use echo get_template_directory() and get characters from right to first /
current=${PWD##*/}
cd ~/Sites/$current
# make a backup of the current local database
wp db export _db.sql
@davemac
davemac / Font-Awesome-Colored---Brand-And-Social-Icons.markdown
Created April 6, 2015 03:55
Font Awesome Colored - Brand And Social Icons

Font Awesome Colored - Brand And Social Icons

Font Awesome Brand and social Icons with brand color

A Pen by Amey Raut on CodePen.

License.

@davemac
davemac / gist:4145907
Created November 25, 2012 23:47
WordPress hide certain plugins
// thanks to Mike Little on WP-hackers
add_filter( 'all_plugins', 'dmc_remove_some_plugins' );
function dmc_remove_some_plugins( $all ) {
global $current_user;
$remove_these = array(
'limit-login-attempts/limit-login-attempts.php',
'wordpress-firewall-2/wordpress-firewall-2.php',
'update-notifier/update-notifier.php',
'wp-super-cache/wp-cache.php',
@davemac
davemac / gist:3484845
Created August 27, 2012 01:13
Check if ACF fields exists
<?php elseif ( has_post_thumbnail() ) :
$featured_image_link = ( get_field( 'featured_image_links_to' ) );
if ( $featured_image_link ) {
echo '<a href="'.get_permalink($post_object->ID) .'">';
if ( has_post_thumbnail()) { the_post_thumbnail( 'hero-image-medium', array( 'class' => 'large fx' ) ); }
echo '</a>';
} else {
the_post_thumbnail( 'hero-image-medium', array( 'class' => 'large fx' ) );
};
endif; ?>
@davemac
davemac / firstdeploy()
Last active July 16, 2019 07:06
initial deploy of local site to staging server
# for initial site deployment to staging server, excludes dev tools and build files
# uses current directory as theme path and ssh alias
# 3 user inputs: dbname, dbuser, dbpass
firstdeploy() {
current=${PWD##*/}
cd ~/Sites/$current || return
echo "Staging database name:"
read dbname
echo "Staging database user:"