Skip to content

Instantly share code, notes, and snippets.

Avatar

johnlewisdesign

View GitHub Profile
@johnlewisdesign
johnlewisdesign / wp-api-stack-requests.php
Created Jun 18, 2020
Stack WP/WooCommerce API calls to override 100 limit
View wp-api-stack-requests.php
header('Access-Control-Allow-Origin: *', 'Content-Type: application/json'); // Avoid cross origin block
$url1 = 'https://www.example.com/wp/v2/posts?per_page=100&page=1'; // path to your JSON file
$url2 = ''https://www.example.com/wp/v2/posts?per_page=100&page=2'; // path to your JSON file
$json1 = file_get_contents($url1);
$json2 = file_get_contents($url2);
$my_array1 = json_decode($json1, true);
$my_array2 = json_decode($json2, true);
$res = array_merge($my_array1, $my_array2);
@johnlewisdesign
johnlewisdesign / yetVisited.js
Last active Jun 18, 2020
Set whether page has been visited or not yet via jQuery (COVID-19 banner show once)
View yetVisited.js
$(document).ready(function() {
var yetVisited = localStorage['visited'];
if (!yetVisited) {
// open popup
localStorage['visited'] = "yes";
}
});
@johnlewisdesign
johnlewisdesign / chmod-web.sh
Created Jun 18, 2020
Ready web folder permissions without digits - CHMOD 755 folders, 644 files
View chmod-web.sh
chmod -R a=r,u+w,a+X ./web
@johnlewisdesign
johnlewisdesign / remove-array-key.php
Last active Jun 18, 2020
Find and remove an array key from a PHP array
View remove-array-key.php
<?php
function removeRecursive($inputArray,$delKey){
if(is_array($inputArray)){
$moreKey = explode(",",$delKey);
foreach($moreKey as $nKey){
unset($inputArray[$nKey]);
foreach($inputArray as $k=>$value) {
$inputArray[$k] = removeRecursive($value,$nKey);
}
}
@johnlewisdesign
johnlewisdesign / post-versioning-via-comments.php
Created Jun 18, 2020
Log post history in comments - WordPress
View post-versioning-via-comments.php
function add_comment_on_post_update( $post_id, $post_after, $post_before ) {
// If this is just a revision, don't send the email.
if ( wp_is_post_revision( $post_id ) )
return;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;
if (isset($post_before->post_status) && 'auto-draft' == $post_before->post_status) {
return;
}
@johnlewisdesign
johnlewisdesign / set-slug-postmeta.php
Created Jun 18, 2020
WordPress - set postmeta (slug)
View set-slug-postmeta.php
<?php
add_action('save_post', 'set_slug');
function set_slug($post_id){
$new_slug = get_post_meta($post_id,'custom-slug', true);
$post_args = array(
'ID' => $post_id,
'post_name' => $new_slug,
);
@johnlewisdesign
johnlewisdesign / hide-cats-from-admin.php
Last active Jun 18, 2020
Hides categories from everywhere, even admin screens [WordPress/WooCommerce]
View hide-cats-from-admin.php
/*
* Hide Specified Categories (by ID) from administrators
*/
add_action( 'admin_init', 'jl_do_terms_exclusion' );
function jl_do_terms_exclusion() {
if( current_user_can('administrator') ) {
add_filter( 'list_terms_exclusions', 'jl_list_terms_exclusions', 10, 2 );
}
@johnlewisdesign
johnlewisdesign / change-shipping-class.php
Last active Jun 18, 2020
Change shipping class en masse [WordPress/WooCommerce]
View change-shipping-class.php
<?php
/** The category IDs containing the posts you want to change */
$category_ids = array(48, 50, 51, 52, 53); // array of ints or single int
/** The shipping class to set for the products */
$shipping_class_slug = "postcards"; // found in "shipping classes" in woocommerce settings
/** Run query to collect our data */
$products = new WP_Query(array(
'post_type' => 'product',
'posts_per_page' => -1,
'fields' => 'ids',
@johnlewisdesign
johnlewisdesign / override-mysql-root.md
Created Jun 18, 2020
Overriding MySQL root in emergency
View override-mysql-root.md

A safer way to do this would be to add the "init-file=/tmp/grant.sql" to the [mysqld] section of your my.cnf file. In /tmp/grant.sql you would have...

SET PASSWORD FOR root@localhost = PASSWORD('xxxx');

or whatever SQL that would fix the root user.

Restart MySQL and the password is reset. You can then remove /tmp/grant.sql and remove the init-file line from your configuration.

Using the skip-grant-tables exposed your entire database to anyone or any service that has access to your mysql instance. If you make sure that the grant.sql file only is readable by the mysql daemon user, you will have less exposure.

View rangeSlider.js
function getVals(){
// Get slider values
var parent = this.parentNode;
var slides = parent.getElementsByTagName("input");
var slide1 = parseFloat( slides[0].value );
var slide2 = parseFloat( slides[1].value );
// Neither slider will clip the other, so make sure we determine which is larger
if( slide1 > slide2 ){ var tmp = slide2; slide2 = slide1; slide1 = tmp; }
var displayElement = parent.getElementsByClassName("rangeValues")[0];