Skip to content

Instantly share code, notes, and snippets.

Navigate to web root and run:
```
wp post delete $(wp post list --post_type='shop_order' --post_status=wc-pending --format=ids --force) --force
```
Check if current archive is a category listing or if it is the lowest level category, in which case, list products. Useful for having different styles for products and categories.
```
/**
* Determine if a listing page us a category with subcategories, or a category with products
* @return string
*/
function get_wc_list_type() {
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); // get current term
$parent = get_term($term->parent, get_query_var('taxonomy') ); // get parent term
$children = get_term_children($term->term_id, get_query_var('taxonomy')); // get children
```
/**
* Display categories or products view
*
* If the display type (set in product categories in WP) is set to "standard", add a div with categories so that it can
* be styled. Else, use the set display type value as the class
*/
add_action('woocommerce_before_shop_loop', function(){
$current_term = get_queried_object();
$display_type = get_term_meta( $current_term->term_id );
Add this to a general include or functions file somewhere
```
// Get current page name and use that to highlight current page in menu
$current_page = basename($_SERVER['PHP_SELF'], ".php");
```
Then add this to your links:
```
<li class="<?php echo $current_page == 'about' ? 'active' : ''; ?>"><a class="menu-about" href="about">about</a></li>
```
```
// Initialize modules
// Importing specific gulp API functions lets us write them below as series() instead of gulp.series()
const { src, dest, watch, series, parallel } = require('gulp');
// Importing all the Gulp-related packages we want to use
const sourcemaps = require('gulp-sourcemaps');
const sass = require('gulp-sass');
const concat = require('gulp-concat');
const uglify = require('gulp-uglify');
```
<?php
/**
* Add generic tracking code to all pages except "Thank you" page
*/ ?>
<!-- Global site tag (gtag.js) - Google Ads: 734634785 -->
<script async src="https://www.googletagmanager.com/gtag/js?id=AW-XXXXXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);}
gtag('js', new Date()); gtag('config', 'AW-XXXXXXXXXXX');
```
/*
Plugin Name: Bulk Update Post
Description: Quick plugin to update posts with whatever you want
Author: James Wills
*/
add_action('init','update_stuff');
function update_stuff(){
Check if the user is logged in as admin so we can test data dumps:
```
$current_user = wp_get_current_user();
if (user_can( $current_user, 'administrator' )) {
echo '<pre>';
print_r($my_dump);
echo '</pre>';
}
```
## Differences between MySQL and MongoDB
Selecting records from the customer table:
```
MySQL: SELECT * FROM customer
MongoDB: db.customer.find()
```
Inserting records into the customer table:
```
MySQL: INSERT INTO customer (cust_id, branch, status) VALUES ('appl01', 'main', 'A')
Useful for mini carts not updating when back button is pressed (bfCache), mainly in Safari but also Firefox. Triggers a page reload.
Once your page is in a final state that you don't want to outlive a redirect, you can just bind the pageshow event and then check for the "persisted" property being true to determine if you should reload it.
```
window.addEventListener("pageshow", function(evt){
if(evt.persisted){
setTimeout(function(){
window.location.reload();
},10);
}