Skip to content

Instantly share code, notes, and snippets.

@bacoords
bacoords / use-remote-media.php
Last active July 21, 2023 17:21 — forked from kingkool68/use-remote-media.php
Check if a local file exists in the WordPress media library and if it doesn't exist, replace the URL with a different URL. Helpful when working with a local site that doesn't have all of the media downloaded as the production site. See https://localwp.com/community/t/proxying-requests-to-wp-content-uploads-to-a-production-site/15701
<?php
// Put this in wp-config.php and replace https://example.com/ with the URL of the production site.
define( 'BC_USE_REMOTE_MEDIA_URL', 'https://example.com' );
// Put the rest of this in functions.php or a custom plugin or somewhere else.
if ( defined( 'BC_USE_REMOTE_MEDIA_URL' ) && ! empty( BC_USE_REMOTE_MEDIA_URL ) ) {
add_filter( 'wp_get_attachment_image_src', 'bc_filter_wp_get_attachment_image_src' );
add_filter( 'wp_calculate_image_srcset', 'bc_filter_wp_calculate_image_srcset' );
add_filter( 'wp_get_attachment_url', 'bc_filter_wp_get_attachment_url' );
}
@willgorham
willgorham / woocommerce-auto-complete-virtual-orders.php
Last active December 29, 2023 02:06
WooCommerce: Automatically complete virtual orders
<?php
add_filter( 'woocommerce_payment_complete_order_status', 'wmg_auto_complete_virtual_orders', 10, 3 );
/**
* Automatically complete orders with only virtual products
*
* @param string $payment_complete_status Order status used after an order payment is received
* @param int $order_id ID of the order being processed
* @param WC_Order $order Order object being processed
@lukecav
lukecav / functions.php
Created December 29, 2017 16:38
Get All orders IDs for a given product ID in WooCommerce
/**
* Get All orders IDs for a given product ID.
*
* @param integer $product_id (required)
* @param array $order_status (optional) Default is 'wc-completed'
*
* @return array
*/
function get_orders_ids_by_product_id( $product_id, $order_status = array( 'wc-completed' ) ){
global $wpdb;
@ollietreend
ollietreend / acf-php-to-json.php
Last active April 22, 2024 11:12
Convert Advanced Custom Fields Pro configuration from PHP to JSON.
<?php
/**
* Plugin Name: Convert ACF PHP to JSON
* Description: Convert Advanced Custom Fields Pro configuration from PHP to JSON.
*/
namespace ConvertAcfPhpToJson;
/**
* Add submenu item under 'Custom Fields'
@danielpost
danielpost / actions.php
Last active March 11, 2017 10:32
Preload blurred image and fade in full image
<?php
/**
* Adds a Base64 encoded version of the Featured Image thumbnail
* to post meta
*
* @param int $post_id ID of the post that's being updated.
*/
function dp_add_base64_featured_image_thumb($post_id) {
// If this is just a revision, don't do anything.
if (wp_is_post_revision($post_id)) {
@benlk
benlk / 0-toc.md
Last active March 29, 2023 13:13
Collection of notes on WP_UnitTestCase
  1. Table of contents
  2. General information
    1. Terms
    2. General structure of a test
    3. WordPress-specific assertions and test functions
      • enqueues
      • creating posts
      • creating terms
      • attaching images
  • ?
@gerbenvandijk
gerbenvandijk / Mark parent navigation active when on custom post type single page
Last active January 1, 2024 21:22
Mark (highlight) custom post type parent as active item in Wordpress Navigation.When you visit a custom post type's single page, the parent menu item (the post type archive) isn't marked as active. This code solves it by comparing the slug of the current post type with the navigation items, and adds a class accordingly.
<?php
function add_current_nav_class($classes, $item) {
// Getting the current post details
global $post;
// Get post ID, if nothing found set to NULL
$id = ( isset( $post->ID ) ? get_the_ID() : NULL );