Skip to content

Instantly share code, notes, and snippets.

View morgyface's full-sized avatar

Dan morgyface

View GitHub Profile
@morgyface
morgyface / wp_first_category.php
Last active April 28, 2020 18:21
WordPress | First category
<?php
$categories = get_the_category();
if ( ! empty( $categories ) ) {
$first_category = $categories[0];
$first_category_name = esc_html( $first_category->name );
$first_category_link = esc_url( get_category_link( $first_category->term_id ) );
}
?>
@morgyface
morgyface / acf_image_to_featured_image.php
Created April 8, 2020 19:54
WordPress | Set ACF image to be the WP feature image
@morgyface
morgyface / uk_to_eu_shoe.php
Created April 2, 2020 10:40
PHP | Function | Convert UK shoe size to EU
<?php
// Convert UK shoe size to European size
function euro_shoe( $uk_size ) {
$eu_size = null;
if( ! is_numeric( $uk_size ) ) {
return $eu_size; // Abort if we are NOT dealing with a number
}
if ( in_array($uk_size, range(2, 19, 0.5)) ) {
// Check to make sure it is a sensible shoe size number using a range with half size steps
// Now work through the equation
@morgyface
morgyface / age_from_date.php
Created April 1, 2020 09:17
PHP | Age from date
<?php
// Return age in years from date of birth
function get_age( $date_of_birth = 0 ) {
$age = null;
if( strtotime( $date_of_birth ) ) {
$date_today = new DateTime('today');
$date_of_birth = new DateTime($date_of_birth);
$age = $date_of_birth->diff($date_today)->y;
}
return $age;
@morgyface
morgyface / get_names.php
Created March 31, 2020 17:33
WordPress | Get names
<?php
/**
* A function to split the post title into first and last names,
* We assume everything up to the first whitespace is the first name,
* the rest is considered the last name.
*/
function get_names( $full_name ) {
if( $full_name ) {
$full_name = sanitize_text_field($full_name); // remove nasties
/**
@morgyface
morgyface / remove_editor.php
Last active March 25, 2020 14:50
WordPress | Remove the default editor from templates
<?php
// Removes the default editor
add_action('admin_head', 'hide_editor');
function hide_editor() {
$template_file = $template_file = basename(get_page_template());
$excluded = array(
'home-page.php',
'search-page.php'
);
foreach ($excluded as $template){
@morgyface
morgyface / image_helper.php
Last active November 30, 2025 11:56
PHP | WordPress | A generic responsive srcset image helper function
<?php
// A generic responsive image helper
function responsive_image( $acf_image_array, $alt, $sizes, $srcset, $class = null ) {
$default_image_url = get_bloginfo( 'template_directory' ) . '/assets/img/thumb_blank.svg';
$filetype_ext = 'svg';
if( !empty( $acf_image_array ) ) {
$image_url = $acf_image_array['url'];
$filetype = wp_check_filetype($image_url);
$filetype_ext = $filetype['ext'];
if( $filetype_ext == 'svg' ) {
@morgyface
morgyface / last_word_wrap.php
Created February 21, 2020 09:53
PHP | wrap the last word of a string in span
<?php
// Function to wrap last word in span
function last_word_wrap($string, $class = null) {
$span = '<span';
if( $class ) {
$span .= ' class="' . $class . '"';
}
$span .= '>';
$wrapped = preg_replace('/\s(\S*)$/', ' ' . $span . '$1', $string);
$wrapped .= '</span>';
@morgyface
morgyface / menu_item_link_classes.php
Last active November 3, 2023 18:03
WordPress - Custom menu item and menu link classes using nav filters
@morgyface
morgyface / 11ty_nunjucks.njk
Last active October 18, 2023 18:23
Eleventy nunjucks useful stuff
{% if not page.fileSlug|length %}
This is the home page<br>
We know this as the homepage has no slug<br>
So when we use a filter to get the length of the page slug it returns false if we are on the homepage<br>
So if we preceed that test with an if not conditional we know we are on the homepage<br>
We are essentially saying if the page slug is empty do something.
Also see it used in a ternary style arrangment below.
{% endif %}
{{ title if page.fileSlug|length else heading }}