Skip to content

Instantly share code, notes, and snippets.

View morgyface's full-sized avatar

Dan morgyface

View GitHub Profile
@morgyface
morgyface / category_filter.php
Created November 8, 2019 11:50
WordPress | Category term filter
<?php
// A filter function to remove certain terms or categories
function categories_filtered( $post_id, $term_slug ) {
$category_objects = null;
$categories = get_the_category( $post_id ); // Get the categories assigned to the post
if ( ! empty( $categories ) ) {
$category_ids = wp_list_pluck( $categories, 'term_id' ); // Convert list into array of cat IDs
$excluded = get_category_by_slug( $term_slug ); // Get category object of term to be excluded
$excluded_id = $excluded->term_id; // Get the ID of the excluded term
$key = array_search( $excluded_id, $category_ids ); // Now get the array key of the excluded category
@morgyface
morgyface / vimeo_thumbnail.php
Created September 9, 2019 11:24
PHP | Vimeo oEmbed API | Thumbnail function
<?php
function vimeo_thumbnail( $video_embed, $referer_url, $width ) {
if( strpos($video_embed, 'vimeo') !== false ) {
preg_match('/src="(.+?)"/', $video_embed, $matches); // Finds the src element of the iframe
$video_url = strtok($matches[1], '?'); // Removes all parameters from URL
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://vimeo.com/api/oembed.json?url=' . $video_url . '&width=' . $width,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
@morgyface
morgyface / selected_or_fallback.php
Last active November 26, 2020 10:17
WordPress | ACF | Selected or fallback
<?php
/**
* Works with the ACF relationship field to always display the quota of posts
*/
function selected_or_fallback( $current_id, $total_required, $acf_relationship_name, $post_type = null) {
$excluded = array( $current_id );
$obtained = 0;
$related = array();
// Check to see if any posts have been selected via ACF relationship
@morgyface
morgyface / grid.scss
Created September 2, 2019 15:14
CSS | Grid layout with crude non-supporting fallback for IE
.list {
margin: 0;
padding-left: 0;
list-style-type: none;
@media only screen and (min-width: $breakpoint-sm) {
display: grid;
grid-column-gap: 50px;
grid-row-gap: 100px;
grid-template-columns: repeat(2, 1fr);
}
@morgyface
morgyface / add_color_to_vimeo.php
Created August 27, 2019 14:30
ACF oEmbed | Adding color param to vimeo videos
<?php
// Project specific function that strips out parameters on a vimeo URL and adds a color param
function add_color_to_vimeo( $iframe ) {
if( strpos($iframe, 'vimeo') !== false ) { // Does the iframe contain vimeo
preg_match('/src="(.+?)"/', $iframe, $matches); // Finds the src element of the iframe
$src = $matches[1];
$clean_src = strtok($src, '?'); // Removes all parameters
$new_params = array(
'color' => 'FEE600' // New color parameter
);
@morgyface
morgyface / favicon_tile.php
Created July 9, 2019 14:13
WordPress | Favicon and tile color function
<?php
function list_favicons($favicon_folder, $tile_color = null) {
/**
* Looks in the theme's favicon folder for ico and png files, if files exist
* a list of favicons will be generated within <head>
*
* To cover most devices, copy four files into the folder:
* A standard favicon.ico which is a compilation of three PNG's 16, 32 and 48.
* Two Apple device favicons: favicon-152.png and favicon-120.png
* A Microsoft favicon: favicon-144.png
@morgyface
morgyface / favicons.php
Created July 9, 2019 13:28
WordPress | Favicon list function
<?php
function add_favicons() {
/**
* Looks in the theme's favicon folder for ico and png files, if files exist
* a list of favicons will be generated.
*
* To cover most devices, copy four files into the folder:
* A standard favicon.ico which is a compilation of three PNG's 16, 32 and 48.
* Two Apple device favicons favicon-152.png and favicon-120.png
* A Microsoft favicon favicon-144.png
@morgyface
morgyface / remove_crap.php
Created July 8, 2019 16:14
WordPress | Remove crap function
<?php
function remove_crap() {
/**
* This function removes redundant and unnecessary tags and scripts from
* the head section of the site, a lot of this stuff is useful for blogs
* but, generally, not utilised for a typical business website.
* Whilst there is not necessarily a load-time advantage we might as well
* keep the front-end tidy by removing everything we know is not required.
* This function is hooked into the after_setup_theme hook,
* which runs before the init hook.
@morgyface
morgyface / social_icon.php
Last active July 5, 2019 09:25
WordPress | Function | PHP | Fontawesome | Get social icon class
<?php
// Get social icon class
function get_social_icon_class($social_url) {
if (strpos($social_url, 'facebook') !== false) {
$icon_class = 'fab fa-facebook-f';
} elseif (strpos($social_url, 'instagram') !== false) {
$icon_class = 'fab fa-instagram';
} elseif (strpos($social_url, 'twitter') !== false) {
$icon_class = 'fab fa-twitter';
} elseif (strpos($social_url, 'etsy') !== false) {
@morgyface
morgyface / click_change_class.html
Created May 16, 2019 15:57
JS | jQuery | Click button to change class
<button class="reveal">-></button>
<div id="nav" class="closed">
<!-- buttons -->
</div>
<script>
$('.reveal').click(function(){
$('#nav').toggleClass( "open" ).toggleClass( "closed" );
});
</script>