Skip to content

Instantly share code, notes, and snippets.

View morgyface's full-sized avatar

Dan morgyface

View GitHub Profile
@morgyface
morgyface / archive_tax_name.php
Created December 17, 2020 10:04
WordPress get the singular name of the taxonomy the term archive belongs to
<?php
function archive_tax_name() {
$archive_tax_name = null;
if( is_archive() && is_category() ) {
$queried_object = get_queried_object();
$queried_object_taxonomy = $queried_object->taxonomy;
$taxonomy = get_taxonomy($queried_object_taxonomy);
$archive_tax_name = $taxonomy->labels->singular_name;
}
return $archive_tax_name;
@morgyface
morgyface / add_the_categories.php
Created November 25, 2020 17:21
WordPress | Add categories programmatically on theme activation
<?php
// Sets the default categories
function add_the_categories() {
$categories = array(
'Case Study',
'Blog',
'Article'
);
foreach( $categories as $category ) {
$exists = term_exists( $category, 'category' );
@morgyface
morgyface / social_icons_16.svg
Last active November 1, 2020 22:33
Social icons at 16 pixels high
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@morgyface
morgyface / social_icons_20.svg
Last active November 1, 2020 22:38
Social icons at 20 pixels high
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@morgyface
morgyface / eleventy_dates.njk
Created May 26, 2020 10:34
11ty | Dates | Nunjucks
<span class="approval__date">{{ item.designDate.toDateString().slice(4, -5) }}</span>
@morgyface
morgyface / slice.njk
Created May 12, 2020 11:27
Nunjucks | 11ty | Truncate, trim or slice. Return part of a string in nunjucks
{{ set cats = "meowers" }}
{{ cats.slice(1, -1) }}
{# Returns: eower #}
{{ cats.slice(3) }}
{# Returns: wers #}
{{ set dogs = " woofers " }}
{{ dogs | trim }}
@morgyface
morgyface / string_md_array.php
Created May 6, 2020 11:20
PHP | Comma seperated string into ACF compatible multidimensional array
<?php
$string = "Skateboarding, Surfing, Horseriding"; // The original comma seperated string
$columns = explode(", ", $string); // Turns the string into an array
$column_array = array(); // Sets up the outer array
foreach ($columns as $key => $value) {
// Loops through the array creating keys and values
$column_array[$key]['skill'] = $value;
// $column_array[$key]['level'] = '0';
}
@morgyface
morgyface / wp-login-styles.php
Last active October 21, 2020 15:12
WordPress | Login customisation
<?php
// Add the login styles
function add_login_scripts() {
/**
* login_enqueue_scripts is the proper hook to use when enqueuing items that
* are meant to appear on the login page.
*/
wp_enqueue_style( 'core', get_stylesheet_directory_uri() . '/assets/css/login.min.css', array('login') );
}
add_action( 'login_enqueue_scripts', 'add_login_scripts' );
@morgyface
morgyface / allow_svg.php
Created May 3, 2020 19:34
WordPress | Allow SVG files
<?php
// Add SVG to allowed file uploads
function add_file_types_to_uploads($file_types){
$new_filetypes = array();
$new_filetypes['svg'] = 'image/svg';
$file_types = array_merge($file_types, $new_filetypes );
return $file_types;
}
add_action('upload_mimes', 'add_file_types_to_uploads');
@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 ) );
}
?>