Skip to content

Instantly share code, notes, and snippets.

View wpmark's full-sized avatar

Mark Wilkinson wpmark

View GitHub Profile
@wpmark
wpmark / render-post-terms-block-output.php
Created March 17, 2023 10:08
Modify the post terms block output in WordPress
View render-post-terms-block-output.php
<?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.
View readme.md

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 December 19, 2022 08:51
Register WordPress block styles with PHP
View php-block-styles.php
<?php
/**
* Register some default block editor styles for this block.
*/
function hd_testimonials_register_testimonials_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
View register-facets-in-php.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
View placeholder-image-id-setting.php
<?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
View wp-email-tweaks.php
<?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.
View alignment-options.js
// 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
View readme.md

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.

@wpmark
wpmark / post-type-support.php
Created October 8, 2021 13:26
Add post type support for the post excerpt
View post-type-support.php
<?php
/**
* Add support for the excerpt on pages.
*/
function hd_add_custom_post_type_excerpt_support() {
// add post type support for pages.
add_post_type_support( 'page', 'excerpt' );
// add post type support for case studies.
@wpmark
wpmark / hd-just-in-time-css.php
Created August 13, 2021 09:13
A plugin to provide Just in Time CSS for WordPress blocks
View hd-just-in-time-css.php
<?php
/*
Plugin Name: Just in Time CSS
Plugin URI: https://highrise.digital/
Description: A plugin from Highrise Digital to provide just in time CSS functionality.
Version: 1.0
License: GPL-2.0+
Author: Highrise Digital Ltd
Author URI: https://highrise.digital/
Text domain: hd-just-in-time-css