Skip to content

Instantly share code, notes, and snippets.

<?php
function my_body_classes( $classes ){
if ( is_page( 'about' ) ) {
$classes[] = 'page-about';
}
return $classes;
}
import { useSelect } from '@wordpress/data';
/**
* AttachmentImage
*
* This component is used to display an image from the media library.
* It's meant as a JS companion to the PHP function `wp_get_attachment_image()`.
*
* @link https://www.briancoords.com/getting-wordpress-media-library-images-in-javascript/
@bacoords
bacoords / render.php
Last active March 15, 2023 04:56
PHP header for a dynamic gutenberg blocks render.php file
<?php
/**
* All of the parameters passed to the function where this file is being required are accessible in this scope:
*
* @param array $attributes The array of attributes for this block.
* @param string $content Rendered block output. ie. <InnerBlocks.Content />.
* @param WP_Block $block The instance of the WP_Block class that represents the block being rendered.
*
* @package gutenberg-examples
*/
@bacoords
bacoords / editor.js
Created March 9, 2023 01:14
Remove core blocks from WordPress block editor
wp.domReady( () => {
let blocks = wp.blocks.getBlockTypes().map( ( block ) => block.name );
blocks.forEach( ( block ) => {
if ( block.indexOf( 'core/' ) === 0 ) {
wp.blocks.unregisterBlockType( block );
}
});
} );
@bacoords
bacoords / readme.md
Created November 29, 2022 15:21
Default WordPress Readme File

Project Name

This is the public folder for Project Name. It can be used for your local development and is also used to push code to the staging and live environments via WP Engine's Git Push.

It currently contains the source code for:

@bacoords
bacoords / .gitignore
Last active January 20, 2023 05:25
Default WordPress/WP Engine .gitignore File
# Wordpress - ignore core, configuration, examples, uploads and logs.
# Forked from: https://github.com/github/gitignore/blob/main/WordPress.gitignore
# Core
/wp-admin/
/wp-content/index.php
/wp-content/languages
/wp-content/plugins/index.php
/wp-content/themes/index.php
/wp-content/uploads/
@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
@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 / 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 / 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",
"",