Skip to content

Instantly share code, notes, and snippets.

@mishterk
mishterk / load-images-from-production.php
Last active Jul 14, 2022
WordPress plugin for loading images from production on a staging/development website. See https://hookturn.io/load-media-images-from-production-wordpress-plugin/
View load-images-from-production.php
<?php
/**
* Plugin Name: Load Images From Production (for staging/dev)
* Description: Hooks into WP's media URL generation and replaces the domain with the production domain.
* Author: Phil Kurth
* Author URI: https://hookturn.io
*/
// If this file is called directly, abort.
defined( 'WPINC' ) or die();
@mishterk
mishterk / replace-images-with-kittens.php
Last active Jul 14, 2022
Replace all attachment images with kitten placeholders...
View replace-images-with-kittens.php
<?php
add_filter( 'wp_get_attachment_image_src', function ( $image ) {
$image[0] = "https://placekitten.com/$image[1]/$image[2]";
return $image;
}, 10 );
@mishterk
mishterk / LocalValetDriver.php
Last active Jul 14, 2022
A local Valet driver for proxying images to a remote host
View LocalValetDriver.php
<?php
/**
* Class LocalValetDriver
*
* This class demonstrates how we might go about proxying any missing local images to a remote host. i.e; the production
* site. This has been created with WordPress in mind but could be adjusted to work with any other system.
*/
class LocalValetDriver extends WordPressValetDriver {
View register-custom-admin-columns.php
<?php
$post_type = 'my_post_type';
// Register the columns.
add_filter( "manage_{$post_type}_posts_columns", function ( $defaults ) {
$defaults['custom-one'] = 'Custom One';
$defaults['custom-two'] = 'Custom Two';
View how-to-update-acf-google-map-field.php
<?php
// The field accepts a value with this structure
$value = [
'address' => '123 Example St, Townsville XYZ 1234, Country',
'lat' => - 38.1486228,
'lng' => 144.360414,
'zoom' => 14,
'place_id' => 'Ei0xMjMgTW9vcmFib29sIFN0LCBHZWVsb25nIFZJQyAzMjIwLCBBdXN0cmFsaWEiMBIuChQKEgmX0JaIHBTUahFyH_LC9sYD8hB7KhQKEglDz-DYDxTUahECZY8QisCjzg',
'street_number' => 123,
@mishterk
mishterk / register-acf-options-page.php
Last active Jun 3, 2022
How to register options pages in Advanced Custom Fields for WordPress (ACF). See https://www.awesomeacf.com/snippets/register-options-page/ for more details.
View register-acf-options-page.php
<?php
// register a top-level options page
if ( function_exists( 'acf_add_options_page' ) ) {
acf_add_options_page( [
'page_title' => 'My Options Page',
'menu_title' => 'My Options Page',
'menu_slug' => 'my-options-page',
'capability' => 'edit_posts',
'parent_slug' => '',
@mishterk
mishterk / load-flexi-field-layout-partials-in-templates.php
Created Sep 17, 2021
Split an ACF flexible content field into template parts
View load-flexi-field-layout-partials-in-templates.php
<?php
// In a post/page template, loop over the ACF flexible field layouts and load the partial
// responsible for rendering the layout.
while ( the_flexible_field('my_flexi_field') ) {
get_template_part( 'components/'. get_row_layout() );
}
@mishterk
mishterk / define-google-maps-api-key-for-acf.php
Last active Jun 3, 2022
Register Google Maps API Key for ACF
View define-google-maps-api-key-for-acf.php
<?php
// Define this in the site's wp-config.php file.
define('GOOGLE_API_KEY', 'your-google-api-key-here');
// Add this to your functions.php file, or a config plugin/MU plugin.
add_filter( 'acf/fields/google_map/api', function ( $api ) {
$api['key'] = GOOGLE_API_KEY;
return $api;
@mishterk
mishterk / control-acf-menu-visibility.php
Created Sep 17, 2021
Hide the ACF admin menu dynamically based on site domain
View control-acf-menu-visibility.php
<?php
add_filter( 'acf/settings/show_admin', function () {
// Get the current site url.
$site_url = get_bloginfo( 'url' );
// Define an array of protected site urls.
$protected_urls = array(
'https://www.example.com',
'http://staging.example.com'
@mishterk
mishterk / disable-core-meta-metabox.php
Created Sep 17, 2021
Speed up ACF backend loading time
View disable-core-meta-metabox.php
<?php
add_filter('acf/settings/remove_wp_meta_box', '__return_true');