Skip to content

Instantly share code, notes, and snippets.

View bebaps's full-sized avatar

Alexander Persky bebaps

View GitHub Profile

Keybase proof

I hereby claim:

  • I am bebaps on github.
  • I am apersky (https://keybase.io/apersky) on keybase.
  • I have a public key whose fingerprint is 5D7D EB25 B38F 7731 3FB5 DDAF 091F DA36 8E8E EE0A

To claim this, I am signing this object:

@bebaps
bebaps / functions.php
Last active June 2, 2017 16:10
Manually defer the parsing of JS files in WordPress. Depending on the theme and installed plugins, it may be better to let a plugin handle this.
<?php
/**
* Defer all JS files
*
* @method defer_parsing_of_js
* @param String $url The URL of the script
* @return String The "cleaned" URL, including the additional defer attribute
*/
function defer_parsing_of_js( $url ) {
// If the URL is not of a .js file, just return it
@bebaps
bebaps / pagination-old.php
Created February 22, 2017 01:55
WordPress pagination when using a custom query within a custom page template
<?php
// An older method, a hack really that works by reseting the default WP_Query in order to trick WordPress into thinking that our custom query is the default query
// Not a fan of this method, but post it because it is still a reliable alternative
// Store the orginal, default WP_Query in a temp variable
$temp = $wp_query;
// Flush the default WP_Qeury
$wp_query = null;
@bebaps
bebaps / some-file.php
Created February 22, 2017 01:51
Passing a variables to a template part using get_template_part()
<?php
// Set up the variable(s) to be passed as an associative array
// NOTE: it MUST be an associative array for this to work, because of the 'extract()' function to be used
$variables = [
'text' => 'See you on the other side!'
];
// Pass the array along with a reference name using the 'set_query_var()' function
// This function basically passes variables via a URL query string
set_query_var( 'passed-variables', $variables );
@bebaps
bebaps / rest-api-loop.php
Created February 22, 2017 01:26
Standard loop of posts using the WP REST API
<?php
// Standard API query arguments
$args = array(
'orderby' => 'title',
'per_page' => 3
);
// Put into the `add_query_arg();` to build a URL for use
// Just an alternative to manually typing the query string
$url = add_query_arg( $args, rest_url('wp/v2/posts') );
@bebaps
bebaps / load-more.js
Created February 21, 2017 14:54
AJAX load more button using the WP REST API
// This function implements a "Load More" button.
// It assumes that there is already a matching query that has executed on the page, so this AJAX call is just a continuation of that query to grab additional posts.
// There are no animations added here. For a smoother experience, it is a good idea to add animations to the button (ex. a loading icon during the request), or making the new posts animate in (ex, using Animate.css to fade them into the page)
$(function() {
// Grab the load more button, since I only want to run the code if the button is on the page
var loadMoreButton = $('#load-more');
if (loadMoreButton) {
// Because there are already posts on the page, we need to manually tell this query what page of results to grab so that is doesn't load duplicate posts
var loadPosts = function(page) {
@bebaps
bebaps / load-posts.js
Created February 21, 2017 14:53
Load posts into a page using the WP REST API
// This function uses AJAX to run a query.
// It assumes that there are no posts on the page, and they will be loaded by the user on demand.
// // There are no animations added here. For a smoother experience, it is a good idea to add animations to the button (ex. a loading icon during the request), or making the new posts animate in (ex, using Animate.css to fade them into the page)
$(function() {
// Grab the load button, since I only want to run the code if the button is on the page
var ajaxButton = $('#ajax-button');
if (ajaxButton) {
// The AJAX request
var getPosts = function() {
@bebaps
bebaps / scp
Created November 9, 2016 15:55
Securely download a directory from a remote server
scp -r username@remote:/path/to/folder /dest/local/path
@bebaps
bebaps / remove-unit.scss
Created October 23, 2016 20:32
Remove units from a value
@function remove-unit($value) {
@return $value / ($value * 0 + 1);
}
@bebaps
bebaps / debug.php
Last active May 19, 2017 16:14
Debug some PHP code
<?php
/**
* Debug some piece of code
*
* @method debug
* @param Variable|Function $code The code that you want to check
* @param Boolen $die Should the rest of the code on the page be prevented from executing
*/
function debug( $code, $die = true ) {
printf( '<pre>%s</pre>', print_r( $code, true ) );