Skip to content

Instantly share code, notes, and snippets.

View misfist's full-sized avatar

P. E. A. Lutz misfist

  • NYC
View GitHub Profile
@misfist
misfist / wp_revisions_to_keep.php
Last active February 14, 2018 19:06
Function to limit the number of revisions saved
<?php
// Alternative to defining in wp-config.php `define( 'WP_POST_REVISIONS', 3 );`
add_filter( 'wp_revisions_to_keep', function() {
return 10;
} );
@misfist
misfist / wp-cron.php
Last active February 14, 2018 18:51
Cron to Delete Revisions
<?php
// add filter to allow for new weekly schedule
add_filter( 'cron_schedules', 'my_weekly_cron' );
function my_weekly_cron( $schedules ) {
// Adds once weekly to the existing schedules.
$schedules['weekly'] = array(
'interval' => 604800,
'display' => __( 'Once Weekly' )
);
@misfist
misfist / media-utilties.php
Last active January 24, 2018 22:30
WordPress - Get Image ID from Source
<?php
/**
* Get an attachment ID given a URL.
*
* @param string $url
* @return mixed int $attachment_id on success || false on failure
*/
function prefix_get_attachment_id( $url ) {
$attachment_id = false;
@misfist
misfist / media-utilties.php
Last active January 24, 2018 20:34
WordPress - Find first image in post content
<?php
function prefix_find_first_imamge( $post ) {
$content = $post->post_content;
$output = preg_match_all( '/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches );
$first_img = $matches[1][0];
if( empty( $first_img ) ) {
return false;
}
@misfist
misfist / media-utilties.php
Last active January 24, 2018 20:31 — forked from claudiosanches/functions.php
WordPress - Get a first image attachment in post
<?php
function prefix_get_first_attachment( $post ) {
$attachment = get_children(
array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'DESC',
'numberposts' => 1,
)
@misfist
misfist / media-utilties.php
Last active January 24, 2018 20:29 — forked from Santel/functions.php
WordPress - Remove first image from single post content
<?php
function prefix_remove_first_image( $content ) {
if ( !is_page() && !is_feed() && !is_home() ){
$content = preg_replace( "/<img[^>]+\>/i", "", $content, 1 );
}
return $content;
}
add_filter( 'the_content', 'prefix_remove_first_image' );
@misfist
misfist / basic-dropdown-usage.php
Created November 27, 2017 21:09 — forked from joshuadavidnelson/basic-dropdown-usage.php
Filter wp_dropdown_categories by post type.
<?php
/**
* Using wp_dropdown_categories with the post type filter applied.
*
* @link https://joshuadnelson.com/category-taxonomy-dropdown-filtered-by-post-type/
*/
// Taxonomy dropdown arguments
$args = array(
'taxonomy' => 'department',
@misfist
misfist / direct_parent.php
Created October 22, 2017 22:01 — forked from levymetal/direct_parent.php
Custom Wordpress function which uses a nav walker to display a list of child pages from a common parent, which can be called from either the parent page (displays children) or any of the child pages (displays siblings). Detailed instructions available on my blog post here: http://christianvarga.com/how-to-get-submenu-items-from-a-wordpress-menu-…
<?php
wp_nav_menu( array(
'menu' => 'Menu Name',
'sub_menu' => true,
'direct_parent' => true
) );
@misfist
misfist / template-functions.php
Last active September 22, 2017 18:13 — forked from bmarshall511/template-functions.php
Adding WordPress plugin template files.
<?php
/**
* Template Loader
*
* This file is read by WordPress to generate the plugin information in the plugin
* admin area. This file also includes all of the dependencies used by the plugin,
* registers the activation and deactivation functions, and defines a function
* that starts the plugin.
*
* @link https://github.com/misfist/template-loader
@misfist
misfist / jetpack.php
Last active January 24, 2018 20:15
WordPress - Disable JetPack CSS
<?php
function prefix_remove_jetpack_styles() {
wp_deregister_style( 'jetpack-carousel' ); // Carousel
wp_deregister_style( 'the-neverending-homepage' ); // Infinite Scroll
wp_deregister_style( 'post-by-email' ); // Post by Email
wp_deregister_style( 'publicize' ); // Publicize
wp_deregister_style( 'sharedaddy' ); // Sharedaddy
wp_deregister_style( 'sharing' ); // Sharedaddy Sharing
wp_deregister_style( 'stats_reports_css' ); // Stats
wp_deregister_style( 'jetpack-widgets' ); // Widgets