Skip to content

Instantly share code, notes, and snippets.

View rxnlabs's full-sized avatar

De'Yonte W. rxnlabs

View GitHub Profile
@rxnlabs
rxnlabs / php-move-element-to-beggining-of-array.php
Last active August 29, 2015 14:07
PHP - Move element to beginning of array. Shift an array's element position
<?php
/**
* Move array element by index. Only works with zero-based,
* contiguously-indexed arrays
*
* @param array $array
* @param integer $from Use NULL when you want to move the last element
* @param integer $to New index for moved element. Use NULL to push
*
* @throws Exception
@rxnlabs
rxnlabs / git-clone-submodule-to-path-track-branch
Last active June 23, 2016 16:13
Git - resetting changed submodules, updating submodules to the latest commit from the master branch. Easy way to work with Git submodules after changing files or when something goes wrong with trying to update submodules.
git submodule add -b master link_to_git_repo_here_ssh_or_https path_to_clone_git_submodule_to_here
@rxnlabs
rxnlabs / js-call-javascript-function-by-string.js
Last active August 11, 2020 16:16
JS - Call and execute javascript functions by names by passing the function name as a string. Use case: Pass an event to ALL Google analytics properties on a page.
//http://stackoverflow.com/questions/359788/how-to-execute-a-javascript-function-when-i-have-its-name-as-a-string
// http://www.sitepoint.com/call-javascript-function-string-without-using-eval/
function executeFunctionByName(functionName, context, args) {
var args = [].slice.call(arguments).splice(2);
var namespaces = functionName.split(".");
var func = namespaces.pop();
for(var i = 0; i < namespaces.length; i++) {
context = context[namespaces[i]];
}
return context[func].apply(this, args);
@rxnlabs
rxnlabs / php-wordpress-autoload-template-code.php
Created November 7, 2014 14:43
PHP - autoload extra code based on the WordPress template being used. Useful when separating your template functionality into multiple files.
<?php
add_filter( 'template_include', array( $this, 'template_additional_code') );
/**
* Load page specific code for each template.
*
* Load additional code on a per template basis. Code in files are only useful for the particular template.
*
* @author De'Yonte W.<https://github.com/rxnlabs>
@rxnlabs
rxnlabs / php-wp-swiftype-bulk-delete-add.php
Created December 17, 2014 14:25
PHP WP - Verify that all FAQs are in Swiftype and add all FAQs to Swiftype
<?php
// change the DB_HOST constant to the localhost IP address so it works on a Mac. "Localhost" means something different in a Mac environment
define('DB_HOST','127.0.0.1');
// load the wp-load.php file to use WordPress functions. File path is assumming this is being run from the current theme folder (any theme folder would work)
require 'wp-load.php';
require 'wp-admin/includes/plugin.php';
// check if PHP is being executed from the command line
if( php_sapi_name() === 'cli' && defined('SWIFTYPE_AUTH_TOKEN') ){
@rxnlabs
rxnlabs / mac-create-add-environment-variable
Last active August 29, 2015 14:13
Mac - set an environment variable. Set an environment variable using Mac
#set an environment variable for closure compiler
export CLOSURE_PATH=/usr/local/opt/closure-compiler/libexec
source ~/.bash_profile
@rxnlabs
rxnlabs / php-mysql-add-insert-multiple-rows-single-query.php
Last active September 19, 2022 18:49
PHP - Insert multiple rows into MySQL table using a single query statement. Example based on code from https://phpacademy.org/topics/how-to-insert-multiple-rows-in-mysql-in-one-query-using-pdo-from-a-form/34096
<?php
// https://phpacademy.org/topics/how-to-insert-multiple-rows-in-mysql-in-one-query-using-pdo-from-a-form/34096
function placeholder( $text, $count = 0, $separator = ',' ) {
$result = array();
if ($count > 0) {
for ($x = 0; $x < $count; $x++) {
$result[] = $text;
}
}
@rxnlabs
rxnlabs / git-check-changed-files
Created February 14, 2015 20:09
Git - figure out if any files have changed on the production/live server. Great for figuring out which files to download to your local environment
#http://stevegrunwell.github.io/wordpress-git/#/20
git ls-files -dmo --exclude-standard
@rxnlabs
rxnlabs / htaccess-redirect-load-images-from-production
Last active November 18, 2022 15:06
htaccess - Redirect image and file request from the localhost to the production host if the file does not exist on the localhost. Needs to be placed in the root htaccess if you're using WordPress or another system that redirects all request to a index.php file.
#place above all other rewrite rules if using a cms or program that redirects all request to index.php file (e.g. WordPress, Drupal, Laravel, etc...)
#if a file or directory does not exist on the local host, 302 redirect the request to the production host so the browser doesn't cache the file in case you replace the file locally
<IfModule mod_rewrite.c>
RewriteEngine On
#Redirect WordPress uploads
#Prevent infinite redirect. Only apply this if not on any subdomains of the live domain. Comment out line if not needed
RewriteCond %{HTTP_HOST} !^(.*)example\.com
#Prevent infinite redirect. Only apply this if not on the root domain of the live domain. Uncomment line if needed
#RewriteCond %{HTTP_HOST} !^example\.com [NC]
#Prevent infinite redirect. Only apply this if not on the www version of the exact domain of the live domain. Uncomment line if needed
@rxnlabs
rxnlabs / windows-create-alias-wp-cli.bat
Created February 26, 2015 13:25
Windows - create an alias in Windows similar to a Linux and Mac environments
doskey wp=php D:\devtools\wp-cli\wp-cli.phar