Skip to content

Instantly share code, notes, and snippets.

shouldShow = groups => {
const field = this.props.field
let show = true
if ( field.conditional_logic ) {
field.conditional_logic.forEach( conditions => {
conditions.forEach( condition => {
const { field } = fieldLookup( groups, condition.field )
if ( field ) {
if ( eval( `"${field.value}" !${condition.operator} "${condition.value}"` ) ) {
show = false
@cjkoepke
cjkoepke / loadDependencies.js
Created January 11, 2018 17:52
Load script/style dependencies asynchronously using Promises.
// Use the function like so:
_loadDependecies([
{tag: 'script', url: 'example.com/asset.js'},
{tag: 'script', url: 'example.com/asset2.js'},
{tag: 'link', url: 'example.com/asset.css'}
]).then(() => {
// Do something.
});
// Function to return a promise catch-all.
@cjkoepke
cjkoepke / logging_errors.js
Created August 10, 2017 18:20
Debugging tips and tricks.
// Great for getting values, outputting context strings, and so on.
console.log(variable)
// Same as console.log(), but prints in yellow as a warning. Useful to show a non-breaking error in a dev env.
console.warn('It is a good idea to use .map instead of a for-loop.')
// Same as console.log(), but prints in red as an error. Useful to show a breaking error in a dev env.
console.error(`The value must be an integer. You provided a ${typeof variable}.`)
// Group console data together for better viewing. Nice to group console logs together.
@cjkoepke
cjkoepke / example.md
Created August 10, 2017 17:17
Terse is Better Than Verbose

Terse is Better Than Verbose

Below are some examples on how the best practice of writing "readable" code is not always synonymous with writing it verbosely. For example, in scenarios where your code is complicated, being verbose might actually be harder to scan and quickly see the purpose without a bunch of decorative wrappers.

See below for more explanation.

@cjkoepke
cjkoepke / fetch.js
Last active July 26, 2017 03:50
A simple way to add headers to a fetch call.
const myHeaders = new Headers();
myHeaders.append('Content-Type', 'document');
fetch('url-here', {
method: 'GET',
headers: myHeaders
}).then(res => {
// Do something.
});
@cjkoepke
cjkoepke / functions.php
Last active February 22, 2016 05:05
This is what functional code looks like that still equals crap.
<?php
add_action('wp_enqueue_scripts','genesis_sample_google_fonts' );
function genesis_sample_google_fonts() {
wp_enqueue_style( 'google-fonts','//fonts.googleapis.com/css?family=Lato:300,400,700', array(), CHILD_THEME_VERSION ); }
add_theme_support( 'html5', array(
'search-form', //search
'comment-form', 'comment-list' //comments
)
@cjkoepke
cjkoepke / functions.php
Created February 19, 2016 22:39
Example code of properly prefixed functions.
<?php
/*
* Example code of properly prefixing your functions
* Unique identifier: ck
*
*/
add_action( 'genesis_after_header', 'ck_third_navigation' );
function ck_third_navigation() {
@cjkoepke
cjkoepke / functions.php
Created February 17, 2016 15:46
Example code of badly prefixed functions.
<?php
/*
* Example code of badly prefixed functions.
*
*/
add_action( 'genesis_after_header', 'sp_third_navigation' );
function sp_third_navigation() {
// Code
@cjkoepke
cjkoepke / functions.php
Created January 5, 2016 02:22
Reposition page title and keep the entry title in the loop
<?php
add_action( 'genesis_meta', 'producer_handle_titles' );
function producer_handle_titles() {
//* Default entry header markup
add_action( 'genesis_after_header', 'producer_entry_header_markup_open', 5 ); /* Function below */
add_action( 'genesis_after_header', 'genesis_do_post_title', 8 );
add_action( 'genesis_after_header', 'producer_entry_header_markup_close', 15 );
@cjkoepke
cjkoepke / login.php
Last active December 17, 2015 02:11
A login page template for WordPress.
<?php
/*
Template Name: Login/Logout Page
*/
//* Remove our default page content
remove_action( 'genesis_entry_content', 'genesis_do_post_content' );
//* Add custom login form to our page content
add_action( 'genesis_entry_content', 'ck_do_login_form' );