Skip to content

Instantly share code, notes, and snippets.

View cameronjonesweb's full-sized avatar
👨‍👦

Cameron Jones cameronjonesweb

👨‍👦
View GitHub Profile
@cameronjonesweb
cameronjonesweb / custom-admin-dashboard.php
Created October 3, 2023 03:41
Wipe the WordPress admin dashboard and replace with your own custom template
<?php
add_action(
'all_admin_notices',
function() {
$screen = get_current_screen();
if ( 'dashboard' === $screen->base ) {
/* Your code goes here */
ob_start();
}
},
@cameronjonesweb
cameronjonesweb / functions.php
Last active June 26, 2023 19:48
Takes custom classes out of the list item and adds them to the anchor element in WordPress menus. Perfect for font icons
<?php
add_filter( 'nav_menu_link_attributes','cameronjonesweb_move_custom_menu_item_class_to_anchor_element', 10, 4 );
add_filter( 'nav_menu_css_class', 'cameronjonesweb_remove_custom_menu_item_class_from_li_element', 10, 4 );
/**
* Get the custom item menu classes and add them to the anchor element
*
* @link https://cameronjonesweb.com.au/blog/how-to-move-the-custom-menu-item-classes-to-the-anchor-element/
* @param array $atts The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored.
@cameronjonesweb
cameronjonesweb / add_control.php
Last active June 1, 2023 16:10
Never Fear, The Customizer Is Here! [WordCamp Sunshine Coast] demo code
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'wordcampsc_link_colour', array(
'label' => __( 'Link Colour', 'wordcampsc' ),
'section' => 'colors',
) ) );
<?php
/**
* A custom WP_Query object that will always be empty
*/
$query = new WP_Query( array(
'post__in' => array( 0 )
) );
@cameronjonesweb
cameronjonesweb / adding-a-variation-that-doesnt-work.js
Last active October 11, 2022 13:58
The painful efforts of adding a custom icon to the social links block
import { Path, SVG } from '@wordpress/primitives';
export const PlayHqIcon = () => (
<SVG width="24" height="24" viewBox="0 0 24 24" version="1.1">
<Path fill="#4897D2" d="M23.3,5.4l-0.1,0.8C16.7,2.5,8.8,2.2,2,5.4v0c2.9-2.1,6.2-3.5,9.7-4.1c0.3-0.1,0.6-0.1,0.9-0.1 c3.6-0.5,7.3-0.1,10.8,1.1C23.4,3.3,23.4,4.3,23.3,5.4L23.3,5.4z"/>
<Path fill="#9057A3" d="M2,5.4C2,5.4,2,5.4,2,5.4C2,5.5,2,5.4,2,5.4L2,5.4z"/>
<Path fill="#9057A3" d="M12.6,1.1c-0.3,0-0.6,0.1-0.9,0.1C8.2,1.9,4.9,3.3,2,5.4C1.9,4.3,1.9,3.3,2,2.2C5.4,1,9,0.7,12.6,1.1z"/>
<Path d="M11.9,3.2C8.5,3.2,5.1,4,2,5.4c0,0-0.1,0-0.1,0C1.5,5.7,1,5.9,0.6,6.2c0.4,3.1,1.4,6.1,3,8.8l1-8.2 c0,0,0-0.1,0.1-0.1s0.1-0.1,0.1-0.1h2c0.1,0,0.2,0.1,0.2,0.2l-0.5,3.7c0,0,0,0.1,0.1,0.1h2.6c0,0,0,0,0.1,0c0,0,0,0,0-0.1l0.5-3.7 c0,0,0-0.1,0.1-0.1c0,0,0.1-0.1,0.1-0.1h2c0,0,0.1,0,0.1,0.1c0,0,0,0.1,0,0.1l-1.2,9.6c0,0,0,0.1-0.1,0.1c0,0-0.1,0.1-0.1,0.1h-2 c0,0-0.1,0-0.1-0.1c0,0,0-0.1,0-0.1l0.5-3.8c0,0,0-0.1-0.1-0.1H6.3c0,0-0.1,0-0.1,0.1l-0.6,5.1c1.8,2.2,3.9,4,6.3,5.4 c2.6-1.5,5-
@cameronjonesweb
cameronjonesweb / get-excerpt-by-id.php
Created October 23, 2021 00:38
Get the excerpt of a post by ID, and generate one if not set manually
<?php
/**
* Get the excerpt of a post by it's ID
*
* @param int $post_id Post ID.
* @return string
*/
function cameronjonesweb_get_excerpt_by_id( $post_id ) {
return apply_filters( 'get_the_excerpt', wp_trim_excerpt( get_post_field( 'post_excerpt', $post_id ), $post_id ), $post_id );
}
@cameronjonesweb
cameronjonesweb / scrolltoanchor.js
Last active September 27, 2022 15:56
jQuery Scroll To Anchor
function scrollToAnchor( $headers ) {
jQuery( 'a[href^="#"]' ).each( function() {
if ( jQuery( this ).attr( 'href' ).length > 1 ) {
if ( jQuery( 'body' ).find( jQuery( this ).attr( 'href' ) ) ) {
jQuery( this ).click( function( e ){
e.preventDefault();
$offset = 0;
for ( i = 0; i < $headers.length; i++ ) {
if ( jQuery( $headers[ i ] ).length ) {
$offset += jQuery( $headers[ i ] ).outerHeight();
@cameronjonesweb
cameronjonesweb / functions.php
Created September 21, 2022 08:46
Bootstrapify `the_posts_pagination()`
<?php
/**
* Update `the_posts_pagination()` markup for Bootstrap 4 styles
*
* Does this monstrosity work? Yes! Should you use it? Probably not.
*
* @param $markup string Original markup.
* @return string Updated markup.
*/
function cameronjonesweb_bootstrapify_posts_pagination( $markup ) {
@cameronjonesweb
cameronjonesweb / add-location-class-to-menus.php
Created May 26, 2022 11:32
Add a location class to WordPress menus
<?php
/**
* Add a class with the menu location to the WordPress menu wrapper
*
* @param array $args Menu arguments.
* @return array
*/
function cameronjonesweb_add_location_class_to_menus( $args ) {
if ( ! empty( $args['theme_location'] ) ) {
$args['items_wrap'] = str_replace( '%2$s', '%2$s menu--' . sanitize_html_class( $args['theme_location'] ), $args['items_wrap'] );
@cameronjonesweb
cameronjonesweb / demos.php
Created March 18, 2022 06:13
Inject an element into an array at a specific position
<?php
/* Returns:
array(5) {
["red"]=>
string(3) "Red"
["green"]=>
string(5) "Green"
["purple"]=>
string(6) "Purple"
["blue"]=>