Skip to content

Instantly share code, notes, and snippets.

@kaelri
kaelri / wordpress-conditional-block-css.php
Last active July 5, 2023 17:57
wordpress-conditional-block-css
<?php
add_action( 'wp_enqueue_scripts', 'my_block_enqueue_scripts' );
function my_block_enqueue_scripts() {
wp_enqueue_style( 'my-normal-css' );
// Do this only if the current post/page has an instance of the related block.
if ( has_block('my-block-name') ) {
<?php
add_action( 'get_header', function() {
if ( !is_attachment() ) return;
$file_url = wp_get_attachment_url( get_the_ID() );
$file_name = basename( $file_url );
header( sprintf('Content-type: %s', get_post_mime_type() ) );
<?php
add_action( 'get_header', function() {
if ( !is_attachment() ) return;
$file_url = wp_get_attachment_url( get_the_ID() );
wp_redirect( $file_url );
@kaelri
kaelri / github_actions_ftp_deploy_example.yml
Created May 11, 2022 14:55
Use git-ftp to sync the latest commit’s files to an (S)FTP destination.
name: Publish to Production
on:
push:
branches: [ master ]
jobs:
web-deploy:
name: Deploy
runs-on: ubuntu-latest
steps:
- name: Get Code
@kaelri
kaelri / wordpress-track-and-redirect-media.php
Last active June 16, 2022 20:33
On WordPress singular attachment pages, send a custom Data Layer event to Google Tag Manager and then redirect to the raw file.
<?php
add_action( 'wp_footer', function() {
if ( !is_attachment() ) return;
$file_url = wp_get_attachment_url( get_the_ID() );
?><script>
<?php
add_action( 'rest_api_init', 'register_form_shortcode' );
function register_form_shortcode() {
register_rest_route( 'mdg/v1', '/events', [
'methods' => 'GET',
'callback' => 'get_shortcode_html',
'permission_callback' => '__return_true',
@kaelri
kaelri / wordpress_shared_blocks.php
Created November 11, 2021 22:29
Create a WordPress post whose content can be included in other posts via a shortcode.
<?php
class sharedBlocks {
public static function setup() {
add_action( 'init', [ __CLASS__, 'register_post_type' ], 0 );
add_shortcode( 'shared', [ __CLASS__, 'do_shortcode' ] );
@kaelri
kaelri / build_ical.php
Last active August 23, 2021 16:57
Create ICS calendar event file from data.
<?php
function build_ical( array $input, string $output_path ) {
$template = file_get_contents( __DIR__ . '/template.ics' );
$timezone = new DateTimeZone('America/New_York');
$now = new DateTime( 'now', $timezone );
$start = new DateTime( $input['start'], $timezone );
@kaelri
kaelri / wordpress-smtp.php
Created June 29, 2021 16:22
Make WordPress use an SMTP server for all outgoing email notifications.
<?php
class mdgSMTP {
public static function setup() {
add_action( 'phpmailer_init', [ __CLASS__, 'configure_php_mailer' ] );
if ( class_exists('WP_CLI') ) {
WP_CLI::add_command( 'send-test-email', [ __CLASS__, 'send_test_email' ] );
@kaelri
kaelri / obsidian-dataview-project-cards.css
Last active April 9, 2022 04:22
Obsidian Project List
.project-cards {
margin: 2em 0;
}
.project-card {
margin: .5em 0;
/* border: 1px solid #444; */
background: hsla(0, 0%, 0%, .1);
padding: .5em .5em .5em 2em;
position: relative;