Skip to content

Instantly share code, notes, and snippets.

View jbrz0's full-sized avatar
🌟

Justin Brazeau jbrz0

🌟
View GitHub Profile
@jbrz0
jbrz0 / useExcerpt.ts
Created August 2, 2020 14:48
useExcerpt: React hook for easy multi line text excerpts
/*
Usage
import {useExcerpt} from '../../hooks/useExcerpt'
1.
Import or store long text in a variable
const text = 'some really long text'
2.
@jbrz0
jbrz0 / array-flatten-function.js
Last active May 31, 2018 22:03
Flatten Arrays in Javascript, transform multiple nested arrays into one
function flatten(ary) {
var ret = [];
for(var i = 0; i < ary.length; i++) {
if(Array.isArray(ary[i])) {
ret = ret.concat(flatten(ary[i]));
} else {
ret.push(ary[i]);
}
}
return ret;
@jbrz0
jbrz0 / wp-jquery-override.php
Created June 22, 2016 17:36
Script for overriding the default jquery injected in a theme by wordpress
//Replace the default WordPress jQuery script with a different one
function modify_jquery() {
if (!is_admin()) {
wp_deregister_script('jquery');
wp_register_script('jquery', 'https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js', false, '1.11.3');
wp_enqueue_script('jquery');
}
}
@jbrz0
jbrz0 / wp-prevent-css-cache.css
Created June 1, 2016 02:45
Prevent Wordpress caching of style.css file
<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); echo '?' . filemtime( get_stylesheet_directory() . '/style.css'); ?>" type="text/css" media="screen" />
@jbrz0
jbrz0 / wp-permalink-URL-update.sql
Last active May 30, 2016 14:28
Edit permalinks in wordpress page content after wordpress install/migration
@jbrz0
jbrz0 / wp-permalink-url.php
Created May 16, 2016 05:32
Permalink URL (featured image background etc)
@jbrz0
jbrz0 / recent-wp-posts.php
Last active February 2, 2017 17:52
Quick reference wordpress loop. Get the latest recent posts and use previous/next buttons
// Offset posts by adding offset=1
<?php global $query_string; // required
$posts = query_posts($query_string.'&posts_per_page=7&order=DESC'); ?>
<?php while(have_posts()) : the_post(); ?>
<div id="post">
<h1><?php the_title(); ?></h1>
<p style="font-weight: 300;"><?php the_time('F jS, Y'); ?> //
@jbrz0
jbrz0 / custom-wp-shortcode.php
Created May 5, 2016 14:05
Basic snippet for custom shortcodes in WP
// Shortcode in functions.php
function caption_shortcode( $atts, $content = null ) {
return '<span class="caption">' . $content . '</span>';
}
add_shortcode( 'caption', 'caption_shortcode' );
// When used like this
[caption]My Caption[/caption]
// The output would be
@jbrz0
jbrz0 / add-wp-user-functions.php
Created May 5, 2016 13:51
Add a WP user using functions.php in theme file
function admin_account(){
$user = 'AccountID';
$pass = 'AccountPassword';
$email = 'email@domain.com';
if ( !username_exists( $user ) && !email_exists( $email ) ) {
$user_id = wp_create_user( $user, $pass, $email );
$user = new WP_User( $user_id );
$user->set_role( 'administrator' );
} }
add_action('init','admin_account');
@jbrz0
jbrz0 / sass-margin-padding.scss
Last active April 17, 2020 18:15
Sass mixin for quickly adding padding and margins
//Padding mixin
@mixin padding($top, $right, $bottom, $left) {
padding-top: $top;
padding-right: $right;
padding-bottom: $bottom;
padding-left: $left;
}
//Margin mixin
@mixin margin($top, $right, $bottom, $left) {
margin-top: $top;