Skip to content

Instantly share code, notes, and snippets.

View morgyface's full-sized avatar

Dan morgyface

View GitHub Profile
@morgyface
morgyface / tiling_grid_structure.scss
Last active September 2, 2019 15:30
SCSS | Responsive css tiling layout for fixed width spacing and variable width columns
@mixin colcalc($spacing, $columns) {
// First let us reset any previous margins and clears
// As 2 columns is the first use there's no need to reset, so we make it conditional
@if $columns > 2 {
$previous_columns: $columns - 1;
&:nth-child(#{$previous_columns}n) {
margin-right: $spacing;
}
&:nth-child(#{$previous_columns}n+1) {
clear: none;
@morgyface
morgyface / wp_privacy.php
Created September 28, 2018 09:24
WordPress | Privacy page option
<?php
$privacy_policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' );
if ( ! empty( $privacy_policy_page_id ) ) {
$privacy_policy_page_url = get_the_permalink( $privacy_policy_page_id );
?>
<a href="<?php echo $privacy_policy_page_url; ?>">Privacy Policy</a>
<?php } ?>
@morgyface
morgyface / is_child.php
Created September 14, 2018 11:10
WordPress | Checks to see if a post is a child, that is; does the post have a parent?
<?php
// Check if post has any parents
function is_child($post_id) {
$post = get_post($post_id);
$parent = $post->post_parent;
if( $parent != 0 ) {
return true;
} else {
return false;
}
@morgyface
morgyface / size_from_url.php
Last active May 26, 2022 08:49
WordPress | File size from URL
<?php
// Return a neat file size
function nice_file_size($file_url, $precision = 2) {
$nice_file_size = null;
$attachment_id = attachment_url_to_postid($file_url); // Gets ID of file from URL
$file_path = get_attached_file( $attachment_id ); // Gets path using ID
if ( is_writable( $file_path ) ) { // Locally an error is thrown due to file not being writable
$size = filesize( $file_path ); // NB filesize only works with the path to the file NOT a URL
$base = log($size, 1024);
$suffixes = array('', 'KB', 'MB', 'GG', 'TB');
@morgyface
morgyface / set_image_sizes.php
Last active July 8, 2019 21:34
WordPress | Function | Setting reserved image sizes on theme activation
<?php
// Setting reserved image sizes on theme activation
add_action( 'after_switch_theme', 'enforce_image_size_options' );
function enforce_image_size_options() {
update_option( 'thumbnail_size_w', 160 );
update_option( 'thumbnail_size_h', 160 );
update_option( 'thumbnail_crop', 1 );
update_option( 'medium_size_w', 660 );
update_option( 'medium_size_h', 440 );
update_option( 'medium_crop', 1 );
@morgyface
morgyface / configuration.js
Created September 1, 2018 22:23
Atom | touchbar-utility | configuration.js | html | php | wordpress | acf | bootstrap
const { dispatchAction } = require('./dispatchAction');
exports.configuration = [{
type: 'button',
label: '#',
insertString: '#',
backgroundColor: '#803FF6'
}, {
type: 'button',
label: '#FFF',
@morgyface
morgyface / feat_img_or_placeholder.php
Last active August 16, 2018 14:19
WordPress | Featured image or fallback source
<?php
// If the post does not have a featured image
function feat_img_fallback($post_id, $image_size = 'thumbnail') {
if ( has_post_thumbnail($post_id) ) {
$image_id = get_post_thumbnail_id($post_id);
$image_src = wp_get_attachment_image_src($image_id, $image_size);
$image_url = $image_src[0];
} else {
$image_url = get_bloginfo('template_directory') . '/images/placeholder.png';
}
@morgyface
morgyface / has_thumbnail_size.php
Created August 9, 2018 12:04
WordPress | Image size test function
<?php
// Works with thumbnail, medium, large and full
function has_thumbnail_size($size, $post_id) {
if ( has_post_thumbnail($post_id) ) {
$image_id = get_post_thumbnail_id($post_id);
$image_attributes = wp_get_attachment_image_src($image_id, 'full');
$image_width = $image_attributes[1];
$image_height = $image_attributes[2];
$media_width = $size . '_size_w';
$media_width = get_option($media_width);
@morgyface
morgyface / feat_img_or_svg.php
Created August 3, 2018 11:32
WordPress | Function | Featured image or SVG
<?php
// If the post does not have a featured image
function feat_img_fallback($image_size, $post_id, $alt) {
if ( has_post_thumbnail($post_id) ) {
$image_id = get_post_thumbnail_id($post_id);
$image_src = wp_get_attachment_image_src($image_id, $image_size);
$image_url = $image_src[0];
$image = '<img src="' . $image_url . '" alt="' . $alt . '">';
} else {
$image_width = $image_size . '_size_w';
@morgyface
morgyface / url_from_template.php
Last active August 27, 2019 10:34
WordPress | Get URL from page template name
<?php
// Get the URL of the first page using a specific template
function url_from_template( $template_name ) {
$args = array(
'post_type' => 'page',
'fields' => 'ids',
'nopaging' => true,
'meta_key' => '_wp_page_template',
'meta_value' => $template_name
);