Skip to content

Instantly share code, notes, and snippets.

View wpmark's full-sized avatar

Mark Wilkinson wpmark

View GitHub Profile
@wpmark
wpmark / gallery-block-counters.php
Created November 25, 2024 13:38
Add image and total image count to the WordPress Gallery block
@wpmark
wpmark / block-stylesheets.php
Last active October 31, 2024 08:57
Easily enqueue a stylesheet for any block just by placing one in your theme.
<?php
/**
* Enqueues a stylesheet for each block, if it exists in the theme.
*/
function hd_enqueue_block_styles() {
// get all of the registered blocks.
$blocks = WP_Block_Type_Registry::get_instance()->get_all_registered();
// if we have block names.
@wpmark
wpmark / render-post-terms-block-output.php
Created March 17, 2023 10:08
Modify the post terms block output in WordPress
<?php
/**
* Changes the links on post terms block for job listing category terms.
*
* @param string $block_content The block content.
* @param array $block The full block, including name and attributes.
* @param WP_Block $instance The block instance.
*
* @return string $block_content The block content.
*/
@wpmark
wpmark / readme.md
Last active June 1, 2023 20:32
Escaping output with only specific allowed HTML elements.

Escaping output with only specific allowed HTML elements

Sometimes when developing for WordPress, you will want to echo something. As all good developers know, any such content must be escaped. This makes sure that nothing nasty can be echoed and keeps you code more secure.

WordPress comes with a number of escaping functions, however sometimes they don't quite do as you want. Take this example.

@wpmark
wpmark / php-block-styles.php
Last active November 27, 2024 10:27
Register WordPress block styles with PHP
<?php
/**
* Register some default block editor styles for this block.
*/
function hd_register_block_styles() {
// add the small image style.
register_block_style(
'core/heading', // name of your block
array(
@wpmark
wpmark / register-facets-in-php.php
Last active March 14, 2023 11:22
Register Facets with the excellent FacetWP plugin via PHP
<?php
/**
* Registers the Facets with FacetWP.
*
* @param array $facets The current array of registered facets.
* @return array $facets The modified array of registered facets.
*/
function hd_utility_add_job_facets( $facets ) {
// add the job industry facet.
@wpmark
wpmark / placeholder-image-id-setting.php
Created March 24, 2022 15:56
Add a placeholder image ID setting in WordPress, under Settings > Media
<?php
/**
* Registers any additional settings required for the plugin.
*/
/**
* Registers the setting for the placeholder attachment ID.
*/
function hd_utility_register_image_placeholder_id_setting() {
@wpmark
wpmark / wp-email-tweaks.php
Created October 12, 2021 17:55
Easily customise the name and email address of WordPress emails
<?php
/**
* Used to filter email from 'address'
*/
function hd_email_send_wp_mail_address( $input ) {
// return a new from email address.
return 'no-reply@highrise.digital';
}
@wpmark
wpmark / alignment-options.js
Last active January 4, 2023 07:50
Add alignment options for WordPress core blocks.
// set alignment options for cover, video, and paragraph blocks.
wp.hooks.addFilter(
'blocks.registerBlockType',
'hd-theme/hd-theme',
function( settings, name ) {
if ( name === 'core/cover' || name === 'core/video' || name === 'core/paragraph' || name === 'core/list' ) {
return lodash.assign( {}, settings, {
supports: lodash.assign( {}, settings.supports, {
// allow support for full and wide alignment.
align: ['full', 'wide'],
@wpmark
wpmark / readme.md
Last active February 1, 2023 11:06
An example of caching data using a WordPress transient

Caching WordPress data using Transients - Example

In this simple example we create a function for obtaining data from an external source and caching it for 24 hours. You can use the function hd_get_external_data() to get the data and work with it in your site.

If you want to force a refresh of the cache, you can pass a value of true into the function.

You can place the code into your themes functions.php file or better still in a plugin. If you are placing it in a plugin, remember to use function_exists() when using this. This ensures that the code will fail correctly if the plugin is not active.