Skip to content

Instantly share code, notes, and snippets.

View morgyface's full-sized avatar

Dan morgyface

View GitHub Profile
@morgyface
morgyface / date_range.php
Created May 11, 2018 15:09
WordPress | ACF | Simple date range
<?php
function date_span_filter($start_date, $end_date) {
if ( $start_date ) {
// Dates should both be in a Y-m-d format
$start_calendar_date = date('j F Y', strtotime($start_date));
if ( ! $end_date ) {
// There is no range. Just return the required start date
$date = $start_calendar_date;
} else {
if ( $start_date == $end_date ) {
@morgyface
morgyface / file_size_extension.php
Created May 10, 2018 18:02
WordPress | File size and extension
<?php
$download_file = get_sub_field('download_file');
$download_url = $download_file['url'];
$download_path = pathinfo($download_url);
$download_ext = $download_path['extension'];
$download_size = filesize( get_attached_file( $download_file['id'] ) );
function format_bytes($size, $precision = 2) {
$base = log($size, 1024);
$suffixes = array('', 'KB', 'MB', 'GG', 'TB');
@morgyface
morgyface / orientation.php
Created May 8, 2018 10:12
PHP | Image orientation with svg support and tolerance
<?php
function get_orientation($image_file, $tolerance = '1.5') {
// First let us determine what kind of file we are dealing with
$path_parts = pathinfo($image_file);
$extension = $path_parts['extension'];
if ( $extension == 'svg' ) {
// Vector svg
$xmlget = simplexml_load_file($image_file);
$xmlattributes = $xmlget->attributes();
$width = (string) $xmlattributes->width;
@morgyface
morgyface / acf_order.php
Created May 8, 2018 09:24
WordPress | ACF | Dashboard | Custom columns | Order
<?php
// Useful during dev to show order number of fields within acf dashboard list
function acf_field_group_columns($columns) {
$columns['menu_order'] = __('Order');
return $columns;
} // end function reference_columns
add_filter('manage_edit-acf-field-group_columns', 'acf_field_group_columns', 20);
function acf_field_group_columns_content($column, $post_id) {
switch ($column) {
@morgyface
morgyface / remove_menu_items.php
Last active November 28, 2019 09:29
WordPress | Remove dashboard menu items
<?php
function remove_menus(){
remove_menu_page( 'index.php' ); // Dashboard - Menu position: 2
remove_menu_page( 'jetpack' ); // Jetpack*
remove_menu_page( 'edit.php' ); // Posts - Menu position: 5
remove_menu_page( 'upload.php' ); // Media - Menu position: 10
remove_menu_page( 'link-manager.php' ); // Links - Menu position: 15
remove_menu_page( 'edit.php?post_type=page' ); // Pages - Menu position: 20
remove_menu_page( 'edit-comments.php' ); // Comments - Menu position: 25
remove_menu_page( 'themes.php' ); // Appearance - Menu position: 60
@morgyface
morgyface / og_wp.php
Last active April 23, 2018 12:17
WordPress | Open Graph Include
<?php
if ( is_archive() ) {
$post_type = get_query_var( 'post_type' );
$og_url = get_post_type_archive_link( $post_type );
$og_title = post_type_archive_title('', false);
$page_object = get_page_by_title( $og_title );
$post_id = $page_object->ID;
} else {
$og_url = get_permalink();
$og_title = get_the_title();
@morgyface
morgyface / simple_url.php
Last active April 17, 2025 08:44
WordPress | Function | Simplify URL
<?php
// Simplify the URL for display purposes. Remove http(s) the www and any queries.
function url_simplify( $url ) {
if ( $url ) {
$url_parse = wp_parse_url( $url );
if( $url_parse ) {
$url_host = $url_parse['host'];
$url_host = str_replace( 'www.','',$url_host );
$url_simple = $url_host;
if( array_key_exists('path', $url_parse) ) {
@morgyface
morgyface / id_via_template_name.php
Last active August 27, 2019 10:32
WordPress | Get the ID of the first page using a specific template
<?php
// Get the ID of the first page using a specific template
function page_id_from_template( $template_name ) {
$args = array(
'post_type' => 'page',
'fields' => 'ids',
'nopaging' => true,
'meta_key' => '_wp_page_template',
'meta_value' => $template_name
);
@morgyface
morgyface / wp_image_sizes_bootsrap_breakpoints.php
Created March 30, 2018 18:47
WordPress | Image sizes matching Bootstrap 4 breakpoint sizes
<?php
// Custom image sizes using Bootstrap 4 breakpoints
// Set here to crop at a 4:3 aspect ratio
add_action( 'after_setup_theme', 'add_image_sizes' );
function add_image_sizes() {
add_image_size( 'sm', 576, 432, true );
add_image_size( 'md', 768, 576, true );
add_image_size( 'lg', 992, 744, true );
add_image_size( 'xl', 1200, 900, true );
}
@morgyface
morgyface / cycle_for_patterns_of_class.twig
Created March 29, 2018 11:39
Twig | Using cycle to apply a pattern of classes
{% set colours = ['red', 'blue', 'green', 'yellow'] %}
{% for item in services %}
<li class="{{ cycle(colours, loop.index0) }}">
<h3>{{ item.service_title }}</h3>
</li>
{% endfor %}