Skip to content

Instantly share code, notes, and snippets.

View chaance's full-sized avatar
🏄‍♂️
Out there

Chance Strickland chaance

🏄‍♂️
Out there
View GitHub Profile
@chaance
chaance / hooks.js
Last active May 29, 2019 23:39
Some handy hooks for React
import React from 'react';
import flattenInput from './utils/flattenInput';
/**
* Store a component's previous value in a ref for use after the value changes.
*
* const prevProps = usePrevious(props);
* const { children: prevChildren } = usePrevious(props) || {};
* const prevState = usePrevious(state);
*/
@chaance
chaance / _deprecated.js
Last active May 24, 2019 05:37
WordPress editor block example with deprecated versions
/**
* Each version gets a directory where I copy over all of the old block files,
* then export them from here for use in your main block file.
*/
export { default as v1 } from './v1';
export { default as v1_01 } from './v1_01';
@chaance
chaance / semantic-commit-messages.md
Last active June 29, 2022 13:58 — forked from joshbuchea/semantic-commit-messages.md
Semantic Commit Messages

Semantic Commit Messages

See how a minor change to your commit message style can make you a better programmer.

Format: <type>(<scope>): <subject>

<scope> is optional

Example

@chaance
chaance / menu.js
Created September 12, 2018 14:24
Recursive menu component (React + Next.js)
import React, { Component } from 'react';
import Link from 'next/link';
import { uniqueId } from 'lodash';
import PropTypes from 'prop-types';
class Menu extends Component {
renderSubMenu = (children) => {
if (children && children.length > 0) {
return (
<ul className="menu__submenu">{this.renderMenuItems(children)}</ul>
@chaance
chaance / util.js
Last active March 10, 2019 19:25
Random collection of utility functions. Because we could all use fewer dependencies.
// Camel case a thing!
export const camelCase = str => `${str.charAt( 0 ).toLowerCase()}${str.replace( /[\W_]/g, '|' ).split( '|' )
.map( part => `${part.charAt( 0 ).toUpperCase()}${part.slice( 1 )}` )
.join( '' )
.slice( 1 )}`;
// Debounce a thing!
export const debounce = ( func, wait, immediate ) => {
let timeout;
return function() {
@chaance
chaance / truncate-string.php
Last active July 24, 2018 01:42
Truncate a string to a maximum number of characters to the nearest complete word.
<?php
/**
* Truncate a string to a maximum number of characters to the nearest complete word.
*
* @param string $string String to truncate.
* @param int $max_char_width Maximum number of characters.
* @param string $append New string to append at the end of the truncated string.
* @return string Truncated string.
*/
function truncate_string( $string = '', $max_char_width = 200, $append = '' ) {
@chaance
chaance / wp-new-admin.sql
Created July 16, 2018 22:56
Add a new WP admin user via SQL.
INSERT INTO `wp_users` (`user_login`, `user_pass`, `user_nicename`, `user_email`, `user_status`)
VALUES ('chance', MD5('Password123'), 'Chance Strickland', '[email protected]', '0');
INSERT INTO `wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`)
VALUES (NULL, (Select max(id) FROM wp_users), 'wp_capabilities', 'a:1:{s:13:"administrator";s:1:"1";}');
INSERT INTO `wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`)
VALUES (NULL, (Select max(id) FROM wp_users), 'wp_user_level', '10');
@chaance
chaance / wp-get-primary-tax-term.php
Last active September 11, 2020 08:13
Get the Yoast SEO primary term for a given taxonomy.
<?php
/**
* Get primary taxonomy term (YoastSEO).
*
* @param mixed $taxonomy Taxonomy to check for.
* @param boolean $term_as_obj Whether to return an object or the term name.
* @param int $post_id Post ID.
* @return mixed The primary term.
*/
function xx_get_primary_tax_term( $taxonomy = 'category', $term_as_obj = true, $post_id = 0 ) {
@chaance
chaance / wp-team-post-save-link-user.php
Last active June 12, 2018 15:01
Function to dynamically sync CPT team posts with WP users. The post type requires custom meta fields for linked_author (user ID) and email.
<?php
/**
* Helper funnction to remove name suffixes.
*
* @param string $name Name.
* @return array Suffix-free name parts.
*/
function xx_get_suffix_free_name_parts( $name ) {
// Format first and last names.
$parts = explode( ' ', (string) $name );
@chaance
chaance / localwp
Created February 25, 2018 18:11 — forked from anonymous/localwp
Quickly CD into working WP theme directory
## Add to your bash or zsh profile
## Modify path to match where you keep your local WP installs
## Your install and theme names should match!
function localwp() {
cd ~/Desktop/local/"$@"/app/public/wp-content/themes/"$@"
}
## To use, type `localwp themename` (replace themename obvi)
## Now you’re in your theme directory! Yay!