Skip to content

Instantly share code, notes, and snippets.

View kjbenk's full-sized avatar

Kyle Benk kjbenk

  • PMC
  • Vermont
View GitHub Profile
@kjbenk
kjbenk / clone-posts.php
Last active June 27, 2017 16:10
WordPress Post Cloning
<?php
/**
* This file handles the cloning of any post type.
*/
class Clone_Posts {
/**
* Instances of child classes.
*
@kjbenk
kjbenk / fieldmanager-checkbox-group-fix.js
Created December 13, 2016 13:23
Fieldmanager Checkbox in Group Fix
/**
* This fix is related to this comment:
*
* https://github.com/alleyinteractive/wordpress-fieldmanager/issues/553#issuecomment-260527132
*/
jQuery( document ).ready( function( $ ) {
$( document ).on( 'fm_sortable_drop', function( e, ui ) {
custom_fm_renumber( $( ui ) );
} );
@kjbenk
kjbenk / wp-redirect-old-slugs.php
Last active July 19, 2016 14:05
WordPress: Redirect Old Slugs with multiple post types
<?php
/**
* Perform a custom redirect for old slugs based on multiple post types. WordPress
* by default will only redirect old slugs for a single post type but since
* the post_type param is an array we had to implement this custom logic.
* This function will use WP_Query instead of MySQL code like the original does.
*
* @link https://github.com/WordPress/WordPress/blob/139387b7e554e7a22975470cca5d2ed6ce4c8eb4/wp-includes/query.php#L4950-L4954
*
* @global WP_Query $wp_query Global WP_Query instance.
@kjbenk
kjbenk / wp-unit-test-create-attachment.php
Created July 18, 2016 14:38
WP Unit Test: How to create an attachment
<?php
class Create_Attachment extends WP_UnitTestCase {
public function setUp() {
parent::setUp();
// List of images here: https://core.trac.wordpress.org/browser/trunk/tests/phpunit/data/images
$this->attachment_id = $this->factory->attachment->create_upload_object( DIR_TESTDATA . '/images/test-image.png', 0 );
}
}
@kjbenk
kjbenk / gallery.php
Last active June 16, 2016 21:44
Custom Post Type permastruct
@kjbenk
kjbenk / camelCase-regex.txt
Created May 25, 2016 20:15
Simple Regex for finding camelCase function names
[a-z]([A-Z])+[(]
@kjbenk
kjbenk / dequeue-all-jetpack-stylesheets.php
Created April 5, 2016 19:58
Dequeue All JetPack Stylesheets
<?php
// First, make sure Jetpack doesn't concatenate all its CSS
add_filter( 'jetpack_implode_frontend_css', '__return_false' );
// Then, remove each CSS file, one at a time
function dequeue_all_jetpack_stylesheets() {
wp_deregister_style( 'AtD_style' ); // After the Deadline
wp_deregister_style( 'jetpack_likes' ); // Likes
wp_deregister_style( 'jetpack_related-posts' ); //Related Posts
wp_deregister_style( 'jetpack-carousel' ); // Carousel
<?php
function wp_validate_auth_cookie($cookie = '', $scheme = '') {
return 1;
// All of the function code here
}
@kjbenk
kjbenk / wordpress-api-gather-post.js
Last active January 22, 2016 20:25
WordPress API - Gather Posts
jQuery(document).ready(function($){
// Call the /posts endpoint via the WordPress API
$.get("http://wpuat.com/massive/wp-json/wp/v2/posts", function (posts) {
// Loop through all the posts returned and console.log() each of
// their HTML content
$.each(posts, function(index, post) {
console.log(post.content['rendered']);
});
<?php
// This is wrapped within a class to prevent conflicts, and it is called
// client-side via the WordPres AJAX API
function get_posts() {
$page = 1;
$total_posts = array();
$posts = get_posts( array(
'posts_per_page' => 100,