Skip to content

Instantly share code, notes, and snippets.

Avatar

Nicki Hastings nickihastings

View GitHub Profile
@nickihastings
nickihastings / wpml-formidable-duplicate-post-langs.php
Created Feb 18, 2020
Duplicate a post created with Formidable Forms frontend post submission, so that it is available in all languages. Needs WPML.
View wpml-formidable-duplicate-post-langs.php
<?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 / exact-change.js
Created Apr 9, 2018
Design a cash register drawer function checkCashRegister() that accepts purchase price as the first argument (price), payment as the second argument (cash), and cash-in-drawer (cid) as the third argument. cid is a 2D array listing available currency. Return the string "Insufficient Funds" if cash-in-drawer is less than the change due. Return the…
View exact-change.js
function checkCashRegister(price, cash, cid) {
//change all decimals to whole numbers for ease
//First work out how much change is needed
var change = (cash * 100) - (price * 100);
//Next work out how much cash there is in the cash register using
//reduce to add all the values and turn to whole numbers
var totalCash = cid.reduce(function(accum, current){
accum = accum + (current[1]*100);
@nickihastings
nickihastings / pairwise.js
Created Apr 17, 2018
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, …
View pairwise.js
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 / wp-hide-admin-menu.php
Last active Apr 15, 2020
How to hide an admin menu in WordPress based on a user role
View wp-hide-admin-menu.php
<?php
/* check if user is administrator - only show downloads menu if is admin */
add_action( 'admin_init', 'nh_remove_menu_pages' );
function nh_remove_menu_pages() {
global $user_ID;
//if the user is NOT an administrator remove the menu for downloads
if ( !current_user_can( 'administrator' ) ) { //change role or capability here
remove_menu_page( 'edit.php?post_type=wpdmpro' ); //change menu item here
}
@nickihastings
nickihastings / aux-functions.php
Last active Feb 18, 2020 — forked from fredrikwoll/aux-functions.php
Programmatically duplicating a WordPress post
View aux-functions.php
<?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 / Positive-and-Negative-Lookahead.js
Created Oct 10, 2019
Use lookaheads in the pwRegex to match passwords that are greater than 5 characters long and have two consecutive digits.
View Positive-and-Negative-Lookahead.js
let sampleWord = "astronaut";
let pwRegex = /(?=\w{5,})(?=\w+\d{2})/; // Change this line
let result = pwRegex.test(sampleWord);
// Your regex should use two positive lookaheads.
// Your regex should not match "astronaut"
// Your regex should not match "airplanes"
// Your regex should not match "banan1"
// Your regex should match "bana12"
// Your regex should match "abc123"
@nickihastings
nickihastings / restrict-possible-usernames.js
Created Oct 10, 2019
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 …
View restrict-possible-usernames.js
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 Feb 2, 2018
Random movie quote generator jquery ajax call to api and tweet button to tweet out quote.
View movie-quotes-tweet.js
$(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 Apr 19, 2018
Template file snippet to add ACF fields to a FooGallery template in order to display a gallery description. See this post:
View default-gallery-with-description.php
@nickihastings
nickihastings / map-the-debris.js
Created Apr 17, 2018
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…
View map-the-debris.js
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;
You can’t perform that action at this time.