Skip to content

Instantly share code, notes, and snippets.

@bacoords
bacoords / shortcode-javascript.php
Last active June 10, 2021 16:20
how to enqueue js for a shortcode
<?php
/**
* Our theme/plugin enqueue scripts function.
*/
function example_enqueue_scripts() {
// Register the script in the normal WordPress way.
wp_register_script( 'example-shortcode-js', '...example-shortcode-script.js' );
@bacoords
bacoords / functions.php
Created October 20, 2021 18:15
Remove lazy loading from WordPress custom logo
<?php
function understrap_get_custom_logo_attributes( $custom_logo_attr ){
$custom_logo_attr['data-no-lazy'] = '1';
$custom_logo_attr['loading'] = 'eager';
return $custom_logo_attr;
}
add_filter( 'get_custom_logo_image_attributes', 'understrap_get_custom_logo_attributes' );
@bacoords
bacoords / custom-javascript.js
Created January 11, 2022 21:03
Understrap - how to use bootstrap components
// Add your custom JS here.
import Popover from 'bootstrap/js/dist/popover'
var popoverTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="popover"]'))
var popoverList = popoverTriggerList.map(function (popoverTriggerEl) {
return new Popover(popoverTriggerEl)
@bacoords
bacoords / archive.php
Created January 15, 2022 20:26
Understrap archive with columns layout
<?php
/**
* The template for displaying archive pages
*
* Learn more: http://codex.wordpress.org/Template_Hierarchy
*
* @package Understrap
*/
// Exit if accessed directly.
@bacoords
bacoords / content-card.php
Created January 15, 2022 20:27
Understrap post content with card classes
<?php
/**
* Post rendering content according to caller of get_template_part
*
* @package Understrap
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
?>
@bacoords
bacoords / understrap-post-nav.php
Created January 19, 2022 00:29
Replace Understrap post nav font-awesome reference with inline Bootstrap Icons SVGs
<?php
/**
* Display navigation to next/previous post when applicable.
*/
function understrap_post_nav() {
// Don't print empty markup if there's nowhere to navigate.
$previous = ( is_attachment() ) ? get_post( get_post()->post_parent ) : get_adjacent_post( false, '', true );
$next = get_adjacent_post( false, '', false );
if ( ! $next && ! $previous ) {
return;
@bacoords
bacoords / snippet.json
Created February 14, 2022 17:39
VSCode snippet for a simple jQuery wrapper
{
"JQuery Frame": {
"prefix": "wpjquery",
"body": [
"(function($) {",
"",
"\t$(document).ready(function($){",
"",
"\t\t$1",
"",
@bacoords
bacoords / jquery-wrapper.js
Last active February 14, 2022 17:44
My default wrapper when using jQuery in WordPress
(function($) {
$(document).ready(function($){
});
})( jQuery );
@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' );
}
@bacoords
bacoords / wc_mail.php
Created August 26, 2022 22:18
Since wc_mail doesn't seem to work perfectly, a custom version
<?php
/**
* Our custom alternative to wc_mail.
*
* @param mixed $to Receiver.
* @param mixed $subject Subject.
* @param mixed $message Message.
* @param string $headers Headers. (default: "Content-Type: text/html\r\n").
* @param string $attachments Attachments. (default: "").
* @return bool