Skip to content

Instantly share code, notes, and snippets.

View nickihastings's full-sized avatar
🏠
Working from home

Nicki Hastings nickihastings

🏠
Working from home
View GitHub Profile
@nickihastings
nickihastings / wpml-formidable-duplicate-post-langs.php
Created February 18, 2020 12:31
Duplicate a post created with Formidable Forms frontend post submission, so that it is available in all languages. Needs WPML.
<?php
add_action('frm_after_create_entry', 'add_translation', 30, 2);
function add_translation($entry_id, $form_id) {
$forms = array( 64 ); // <---- change or add form ids here, comma separated, e.g. (24, 53, 64)
// check if form id is in array, if so run code
if( in_array($form_id, $forms) ) {
@nickihastings
nickihastings / pairwise.js
Created April 17, 2018 19:01
Given an array arr, find element pairs whose sum equal the second argument arg and return the sum of their indices. If multiple pairs are possible that have the same numeric elements but different indices, return the smallest sum of indices. Once an element has been used, it cannot be reused to pair with another. For example pairwise([7, 9, 11, …
function pairwise(arr, arg) {
//if its an empty array return 0
if(arr.length === 0){
return 0;
}
//reduce the array by looping over the array and checking if the
//sum of the current element and another element = the argument.
//if they match and the indexes of both are different, add the indexes to
//the output array.
var pairs = arr.reduce(function(accum, curr, index, array){
@nickihastings
nickihastings / aux-functions.php
Last active February 18, 2020 11:05 — forked from fredrikwoll/aux-functions.php
Programmatically duplicating a WordPress post
<?php
/**
* Duplicates a post & its meta and it returns the new duplicated Post ID
* @param [int] $post_id The Post you want to clone
* @return [int] The duplicated Post ID
*/
function duplicate($post_id) {
$title = get_the_title($post_id);
$oldpost = get_post($post_id);
@nickihastings
nickihastings / restrict-possible-usernames.js
Created October 10, 2019 10:39
You need to check all the usernames in a database. Here are some simple rules that users have to follow when creating their username. 1) The only numbers in the username have to be at the end. There can be zero or more of them at the end. 2) Username letters can be lowercase and uppercase. 3) Usernames have to be at least two characters long. A …
let username = "JackOfAllTrades";
let userCheck = /^[a-z][a-z]+\d*$/i; // Change this line
let result = userCheck.test(username);
// Your regex should match JACK
// Your regex should not match J
// Your regex should match Oceans11
// Your regex should match RegexGuru
// Your regex should not match 007
// Your regex should not match 9
@nickihastings
nickihastings / movie-quotes-tweet.js
Created February 2, 2018 22:08
Random movie quote generator jquery ajax call to api and tweet button to tweet out quote.
$(document).ready(function(){
//get the quote api information with Ajax
getQuote();
var bgCols = ['#2e67f7','#ea5c30','#38b582','#683a20','#59357d', '#555'];
function getQuote(){
var num = Math.floor(Math.random()*6+1);
$.ajax({
url: "https://andruxnet-random-famous-quotes.p.mashape.com/?cat=movies&count=1",
type: "GET",
@nickihastings
nickihastings / default-gallery-with-description.php
Created April 19, 2018 17:33
Template file snippet to add ACF fields to a FooGallery template in order to display a gallery description. See this post:
@nickihastings
nickihastings / map-the-debris.js
Created April 17, 2018 07:03
Return a new array that transforms the element's average altitude into their orbital periods. The array will contain objects in the format {name: 'name', avgAlt: avgAlt}. You can read about orbital periods on wikipedia. The values should be rounded to the nearest whole number. The body being orbited is Earth. The radius of the earth is 6367.4447…
function orbitalPeriod(arr) {
var GM = 398600.4418;
var earthRadius = 6367.4447;
//create a function to calculate orbital period
function calcPeriod(avgAlt){
var orbitalRadius = earthRadius + avgAlt;
//calculation is 2 multiplied by pi multiplied by the square root of the
//orbital radius to the power of 3 divided by the GM
var period = Math.round(2*Math.PI*(Math.sqrt(Math.pow(orbitalRadius,3)/GM)));
return period;
@nickihastings
nickihastings / make-a-person.js
Created April 16, 2018 18:29
Fill in the object constructor with the following methods below: getFirstName() getLastName() getFullName() setFirstName(first) setLastName(last) setFullName(firstAndLast) Run the tests to see the expected output for each method. The methods that take an argument must accept only one argument and it has to be a string. These methods must be the …
var Person = function(firstAndLast) {
var arr = firstAndLast.split(' ');
var firstName = arr[0];
var lastName = arr[1];
// Complete the method below and implement the others similarly
this.getFullName = function() {
return firstName + ' ' + lastName;
};
this.getFirstName = function() {
@nickihastings
nickihastings / no-repeats-please.js
Created April 15, 2018 14:55
Return the number of total permutations of the provided string that don't have repeated consecutive letters. Assume that all characters in the provided string are each unique. For example, aab should return 2 because it has 6 total permutations (aab, aab, aba, aba, baa, baa), but only 2 of them (aba and aba) don't have the same letter (in this c…
function permAlone(str) {
//check if the string provided is only one character
if(str.length === 1){
return 1;
}
//function to create the permutations
//for each value in the string, add the permutations of the rest
function getPermutations(str){
var output = [];
@nickihastings
nickihastings / inventory-update.js
Created April 10, 2018 17:52
Compare and update the inventory stored in a 2D array against a second 2D array of a fresh delivery. Update the current existing inventory item quantities (in arr1). If an item cannot be found, add the new item and quantity into the inventory array. The returned inventory array should be in alphabetical order by item.
function updateInventory(arr1, arr2) {
// All inventory must be accounted for or you're fired!
//check the items in arr2 against those in arr1
//if the item is found update the value and set found to true.
//if the item is not found add it to arr1
for(var i = 0; i < arr2.length; i++){ //loop over array 2
var found = false;
for(var j = 0; j<arr1.length; j++){ //loop over array 1
if(arr2[i][1] === arr1[j][1]){