Use Google Photos as post attachment gallery

My dad doesn’t know about computers at all. Although he did a few lessons decades ago, he never got really into it. But he knows how to use a smartphone these days. It’s not that hard for him anymore, after using such a device for years now. He also makes a lot of pictures one needs to handle first. For this very reason I got him to use Google Photos. I showed him how to manage the pictures he took and created an album he wants to show off as gallery on his website.

How it works

The script uses wp-cli to deal with WordPress and takes 2 arguments (required):

  • the URL to an album on Google Photos and
  • the post id where the images should be attached to

It is looking for an option stored in the database every 30 seconds. If it is missing, it will download all the images with the Google Photos API and renames them immediately to take care of the order. All the images attached to the post will be deleted, before they will be uploaded again. It will also flush the cache and notify by mail once the work is done. The post just needs to include the default gallery shortcode without any options then.

#!/bin/bash

WP=/srv/http/johann

URL=${1?Missing url}
ALBUM=$(basename "$1")
POST_ID=${2?Missing post id}
OPTION=google_photos_${ALBUM}

EMAIL="office@johann-masken.at"
SUBJECT="Bilder aktualisiert!"
MESSAGE="via johann-masken.at"

cd $WP

function download {
	PHOTOS=$(curl -L "$URL" | perl -MHTML::Entities -pe 'decode_entities($_);' | grep -oP '\["AF1Q.*?",\["\K(.*?)(?="\,)')
	ATTACHMENTS=$(wp post list --post_type=attachment --format=ids --post_parent=$POST_ID)

	rm -rf "$WP/wp-content/photos" && i=1
	mkdir -p "$WP/wp-content/photos" && cd $_
	mkdir -p "$WP/wp-content/photos/$ALBUM"

	wp post delete $ATTACHMENTS --force

	for photo in $PHOTOS; do
		curl -s -JLO "${photo}=d"

		for f in *; do
			test -f "$f" && mv "$f" $(printf "$ALBUM/%03d_$f" $i)
		done

		((i++))
	done

	wp media import $ALBUM/* --post_id=$POST_ID --user=wpcli
	wp super-cache flush --post_id=$POST_ID
	wp option update $OPTION $(date "+%s")
	wp eval 'wp_mail($EMAIL, $SUBJECT, $MESSAGE);'
}

while sleep 30; do

	wp option get $OPTION || download

done;

The site is hosted on uberspace so I run the script as service with supervisord.

[program:gphotos]
command=gphotos https://photos.app.goo.gl/Cv5DDPJbpnh51mgZ7 12
autostart=yes
autorestart=yes

Now you only need to delete the option in the database every time the album got changed. I like to use the HTTP Shortcuts for Android app and create a widget on the home screen, which will do an HTTP request.

global $wpdb;
$wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE 'google_photos_%'");

With all these images, the media library sure looks messy. So I just hide all the uploads by other users. It also prevents me to use these images in other posts as they will be deleted next time the album got updated.

add_filter('ajax_query_attachments_args', function($query) {

	$query['author'] = get_current_user_id();

	return $query;

});

Leave a comment

Your email address will not be published. Required fields are marked *