Skip to content

Instantly share code, notes, and snippets.

Avatar
👨‍👦
My son is my everything

Cameron Jones cameronjonesweb

👨‍👦
My son is my everything
View GitHub Profile
@cameronjonesweb
cameronjonesweb / add-location-class-to-menus.php
Created May 26, 2022
Add a location class to WordPress menus
View add-location-class-to-menus.php
<?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 Mar 18, 2022
Inject an element into an array at a specific position
View demos.php
<?php
/* Returns:
array(5) {
["red"]=>
string(3) "Red"
["green"]=>
string(5) "Green"
["purple"]=>
string(6) "Purple"
["blue"]=>
View kofi-user-contactmethod.php
<?php
add_filter( 'user_contactmethods', 'add_kofi_user_contactmehod' );
function add_kofi_user_contactmehod( $methods ) {
$methods['kofi'] = 'Ko-fi Username';
return $methods;
}
function create_kofi_button_from_user_meta() {
$kofi = get_the_author_meta( 'kofi', get_the_author_ID() );
@cameronjonesweb
cameronjonesweb / get-excerpt-by-id.php
Created Oct 23, 2021
Get the excerpt of a post by ID, and generate one if not set manually
View get-excerpt-by-id.php
<?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 / php.ini.hbs
Created Sep 13, 2021
Prevent var_dump being truncated using Local by Flywheel
View php.ini.hbs
xdebug.var_display_max_depth = -1
xdebug.var_display_max_children = -1
xdebug.var_display_max_data = -1
@cameronjonesweb
cameronjonesweb / cameronjonesweb-create-admin.php
Last active Jul 5, 2021
Automatically create admin user
View cameronjonesweb-create-admin.php
<?php
/**
* Create an admin user
*/
function cameronjonesweb_create_admin_user() {
$username = 'username';
$password = 'password';
$email = 'email@example.com';
if ( ! username_exists( $username ) && ! email_exists( $email ) ) {
View add-slug-wocommerce-csv-export.php
<?php
/**
* Add the custom column to the exporter and the exporter column menu.
*
* @link https://github.com/woocommerce/woocommerce/wiki/Product-CSV-Importer-&-Exporter#adding-custom-export-columns-developers
*
* @param array $columns.
* @return array $columns.
*/
function cameronjonesweb_add_export_columns( $columns ) {
@cameronjonesweb
cameronjonesweb / 404-fix.php
Created May 1, 2021
Fix 404 pages returning true for have_posts if permalinks have a prefix ie "/news/%postname/"
View 404-fix.php
<?php
function cameronjonesweb_404_fix() {
global $wp_query;
if ( $wp_query->is_404 ) {
$wp_query->posts = array();
$wp_query->current_post = 0;
$wp_query->post = null;
}
}
View cameronjonesweb-wordpress-jquery-fix.php
<?php
function cameronjonesweb_jquery_fix() {
// Restore old jQuery.
wp_deregister_script( 'jquery' );
wp_deregister_script( 'jquery-core' );
wp_register_script( 'jquery-core', get_template_directory_uri() . '/includes/js/jquery-1.12.4.min.js', array(), '1.12.4', false );
wp_register_script( 'jquery', false, array( 'jquery-core' ), null, false );
wp_enqueue_script( 'jquery' );
}
View cameronjonesweb-delete-default-options.php
<?php
/**
* Delete the option setting for the default category
*
* @link https://cameronjonesweb.com.au/blog/how-to-remove-the-uncategorised-category-from-wordpress-and-woocommerce
*/
function cameronjonesweb_delete_default_category_option() {
if ( get_option( 'default_category' ) ) {
delete_option( 'default_category' );
}