Skip to content

Instantly share code, notes, and snippets.

View mgratch's full-sized avatar

Marc Gratch mgratch

View GitHub Profile
@mgratch
mgratch / set-4-featured_post.sh
Created August 3, 2018 16:42
Loop through all the blog posts and mark the first 4 as featured. Using variables, loops wp-cli, conditionals.
// loop through all the blog posts and mark the first 4 as featured.
i=0; \
c=4; \
for id in $(wp post list --format=ids); \
do let i++; \
if [ "$i" -lt 4 ]; \
then wp post meta update $id featured_post '1'; \
else wp post meta update $id featured_post '0'; \
fi; \
done; \
@mgratch
mgratch / insert-db-parts.sh
Last active August 3, 2018 20:48
insert db that is split into multiple tables. Using a for loop and wp-cli. This will work for parts to but you might need to run to ensure all tables are successfully created.
// insert db that is split into multiple parts
for f in /sql/*.sql; \
do wp db import $f; \
done; \
@mgratch
mgratch / generate-identical-posts-with-content.sh
Last active August 3, 2018 16:26
create 25 posts with fake yet identical content, using wp-cli, pipes, curl, wget, xargs, npm json
// create 25 posts with fake & identical content
curl http://loripsum.net/api/5 | \
wp post generate --post_content --count=25 --format=ids | \
xargs -d ' ' -I % wp post meta add % _thumbnail_id \
$(wp media import \
$(wget -O - -q -t 1 http://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC | \
json data.images.original.url) --porcelain);
@mgratch
mgratch / random-featured-images.sh
Last active August 3, 2018 20:49
get random images for featured image, using wp-cli, a loop, wget and npm json
@mgratch
mgratch / reset_restore_site.sh
Created August 3, 2018 16:03
Clear my site and load it with randomly generated content, then restore my site!
//backup site and create a new one with dummy posts
wp db export ../backups/now/sql/marcgratch.sql; \
wp db drop; \
mv * ../backups/now/files/; \
wp core download; \
wp config create --prompt; \
wp db create --prompt; \
wp core install --url=marcgratch.test --title=marcgratch.com --admin_user=admin [email protected]; \
curl http://loripsum.net/api/5 | \
wp post generate --post_content --count=25 --format=ids | \
@mgratch
mgratch / add-terms-to-tax-gf-submission.php
Last active March 11, 2018 07:05
When submitting a text field of term names to Pods::add() if the term already exists the user will receive an error. Already existing terms are expected to include `term_id` in the `$tag_data` array.
<?php
/**
* 1. install https://wordpress.org/plugins/gravity-forms-custom-post-types/
* 2. Add the following code
*
* Now text fields in GF marked as taggable with the additional plugin will work as expected. This code is a bit rough and there is more duplication than I would like... this is a first draft :)
* This should work for any text field with a comma separated value, not just the above plugin.
*
*/
@mgratch
mgratch / vagrant_halt_custom.sh
Created March 2, 2018 15:15
Prompt to backup DBs over 100mb -- if timeout default to backup
#!/bin/bash
#
# Create individual SQL files for each database automatically,
# but give me a chance to skip it. These files
# are imported automatically during an initial provision if
# the databases exist per the import-sql.sh process.
echo 'Starting custom vagrant halt...';
mysql -e 'show databases' | \
grep -v -F "information_schema" | \
grep -v -F "performance_schema" | \
@mgratch
mgratch / sprout-invoices-rearrange-toggl-imported-times-by-date.js
Created February 8, 2018 18:29
run from web console. Sort invoice items by date.
var items = [];
//build an array of item id's and date the item was logged
jQuery.each(jQuery(".redactor-editor > p > small:last-child"), function(){
var date = jQuery(this),
item = jQuery(this).parent();
items.push({date: new Date(jQuery(this).text()),ID: jQuery(this).parents('.item').first().data('id')})
})
//yay underscores makes sorting by date easy
@mgratch
mgratch / sprout-invoices-import-items-bulk-delete.js
Created February 8, 2018 18:24
Find the ID of the item you want to keep, select all above that. Useful if a project has hours spanning months and you only need this months hours.
/**
* Delete all items above a specific ID ... i.e. I import a project time from toggl
* which spans multiple months. I only want this months. So I find the first item I need to keep.
* All the items above should be deleted.
**/
jQuery.each(jQuery('#2623').prevAll(), function(){
jQuery(this).find(jQuery('.time_entry_deletion')).click();
})
/**
@mgratch
mgratch / remove-html.php
Last active October 19, 2016 17:38
Batch Strip HTML tags from post/page content. BACKUP YOUR DB BEFORE USING THIS! The plugin should deactivate itself if it's successful.
<?php
/*
* Plugin Name: Remove HTML
* Description: batch Remove HTML from post_content
* Author: Marc Gratch
* Author URI: http://marcgratch.com
* Version: 0.1.0
*/
function rh_get_all_ids(){
$args = array ('post_type' => array('post','page'), 'fields' => 'ids', 'posts_per_page' => -1);