Skip to content

Instantly share code, notes, and snippets.

View hellofromtonya's full-sized avatar

Tonya Mork hellofromtonya

View GitHub Profile
@hellofromtonya
hellofromtonya / challenge.php
Created May 6, 2015 09:15
WordPress Developers Club Challenge - What is the SQL to get the count of all post meta where post ID = 1?
/**
* Get the number of meta for a specific Post ID
*
* @since 1.0.0
*
* @param integer $post_id Post ID to query for
* @return integer Returns the count number
*/
function tonya_get_meta_count_for_post_id( $post_id ) {
@hellofromtonya
hellofromtonya / restrict-media-library.php
Created May 13, 2015 16:01
Restrict Media Library by User Role
<?php
/**
* Restrict Media Library by User Role
*
* @package Restrict_Media_Library
* @since 1.0.0
* @author hellofromTonya
*/
/**
@hellofromtonya
hellofromtonya / 0_reuse_code.js
Last active August 29, 2015 14:21
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@hellofromtonya
hellofromtonya / challenge_05182015.php
Created May 19, 2015 00:53
WordPress Challenge for May 18, 2015
<?php
/**
* As part of our weekly challenges, take a crack at this one.
* To play along,
* 1. Copy the code into a Gist,
* 2. Answer each of the questions by supplying the code;
* 3. Provide some notes as to your train of thought;
* 4. Then submit the gist's URL for us to review.
*
@hellofromtonya
hellofromtonya / user_helpers.php
Created June 11, 2015 16:43
Fetching User Meta by Role
<?php
/**
* Fetch user meta for the specified role and return user ID, first name, and last name
*
* @since 1.0.0
*
* @param string $role
* @return array
*/
@hellofromtonya
hellofromtonya / hoisting-example.js
Created April 7, 2016 18:19
JavaScript Hoisting Example
(function ( document, $, undefined ) {
/*****
* SHOWING HOISTING WITH A VARIABLE
*/
// Let me show hoisting to you by demonstrating a variable and it's
// initialization with a value
// 1. You get "undefined" here - but NOT an error.
console.log( hoistingVariable );
var hoistingVariable = 'I am initializing it now.';
@hellofromtonya
hellofromtonya / allow-login-via-email.php
Created April 12, 2016 02:45
Allow login via username or password for WordPress
<?php
namespace KnowTheCode;
add_filter( 'authenticate', __NAMESPACE__ . '\enable_login_with_email', 20, 3 );
/**
* Allow the user to log in via email address.
*
* If the `$email_or_username` parameter is not an email, then bail out, returning the `$user`.
* Otherwise, get the user's object via the email. Then reauthenticate their login
* via `wp_authenticate_username_password`.
@hellofromtonya
hellofromtonya / layout.php
Created April 22, 2016 14:38
Proposed fix for Genesis header right widgets
function genesis_header_body_classes( array $classes ) {
if ( current_theme_supports( 'custom-header' ) ) {
if ( get_theme_support( 'custom-header', 'default-text-color' ) !== get_header_textcolor() || get_theme_support( 'custom-header', 'default-image' ) !== get_header_image() )
$classes[] = 'custom-header';
}
if ( 'image' === genesis_get_option( 'blog_title' ) || ( get_header_image() && ! display_header_text() ) )
$classes[] = 'header-image';
@hellofromtonya
hellofromtonya / post.php
Created July 9, 2016 01:38
Real example of a "return early pattern" opportunity - actually there are two examples of it in here.
function genesis_do_post_image() {
/**
* Return Early Pattern opportunity
* ================================
* Notice that the code wrapped in this conditional block only executes IF the
* conditional expression is true. Hum, that means if the conditional expression
* is false, then the function is done and nothing else is going to happen.
*/
if ( ! is_singular() && genesis_get_option( 'content_archive_thumbnail' ) ) {
@hellofromtonya
hellofromtonya / post.php
Last active July 9, 2016 01:54
Same function refactored using the "Return Early Pattern" - now when the conditions will not let the function continue, we get out of the function right away
function genesis_do_post_image() {
if ( is_singular() || ! genesis_get_option( 'content_archive_thumbnail' ) ) {
return;
}
$img = genesis_get_image( array(
'format' => 'html',
'size' => genesis_get_option( 'image_size' ),
'context' => 'archive',