Skip to content

Instantly share code, notes, and snippets.

Daniel Bachhuber danielbachhuber

Block or report user

Report or block danielbachhuber

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@danielbachhuber
danielbachhuber / disable-logged-out-users.php
Last active Aug 30, 2019
Disable WP REST API requests for logged out users
View disable-logged-out-users.php
<?php
add_filter( 'rest_authentication_errors', function( $result ) {
if ( ! empty( $result ) ) {
return $result;
}
if ( ! is_user_logged_in() ) {
return new WP_Error( 'restx_logged_out', 'Sorry, you must be logged in to make a request.', array( 'status' => 401 ) );
}
return $result;
@danielbachhuber
danielbachhuber / gist:8af274e2b7f21c8c3bb6
Created Jul 3, 2014
Post-style permalinks for your custom post types
View gist:8af274e2b7f21c8c3bb6
<?php
/**
* Post-style permalinks for your custom post types
* e.g. %year%/%monthnum%/%day%/%postname%
*/
function dbx_get_post_types() {
return array(
// replace with your custom post types
'my-custom-post-type'
);
@danielbachhuber
danielbachhuber / gist:7126249
Last active Aug 27, 2019
Include posts from authors in the search results where either their display name or user login matches the query string
View gist:7126249
<?php
/**
* Include posts from authors in the search results where
* either their display name or user login matches the query string
*
* @author danielbachhuber
*/
add_filter( 'posts_search', 'db_filter_authors_search' );
function db_filter_authors_search( $posts_search ) {
View tasty-recipes-reposition-jump-to.php
<?php
/**
* Re-positions the "Jump to Recipe" button.
*/
add_action( 'init', function(){
if ( method_exists( 'Tasty_Recipes\Shortcodes', 'filter_the_content_late' ) ) {
remove_filter( 'the_content', array( 'Tasty_Recipes\Shortcodes', 'filter_the_content_late' ), 100 );
add_filter( 'the_content', array( 'Tasty_Recipes\Shortcodes', 'filter_the_content_late' ), 10 );
}
});
View migrate-import-cache.php
<?php
/**
* Filters wp_remote_get() to:
* 1. Return a value from the cache when it's available.
* 2. Write a value to the cache when it's been fetched.
*
* Requires the WP_IMPORT_CACHE constant to be set to a writable directory.
*/
if ( defined( 'WP_CLI' ) && WP_CLI ) {
@danielbachhuber
danielbachhuber / postmark-split-wp-mail.php
Created Jul 30, 2019
Splits wp_mail() with more than 40 bcc into multiple batches
View postmark-split-wp-mail.php
<?php
/**
* Splits a wp_mail() call with more than 40 bcc
* headers into multiple batches.
*
* Postmark only accepts 50 bcc, so this ensures the API call doesn't fail.
*
* @param array $args Original arguments passed to wp_mail().
* @return
*/
@danielbachhuber
danielbachhuber / collection-filter.js
Last active Jul 31, 2019
Add a custom taxonomy dropdown filter to the WordPress Media Library
View collection-filter.js
(function(){
/**
* Create a new MediaLibraryTaxonomyFilter we later will instantiate
*/
var MediaLibraryTaxonomyFilter = wp.media.view.AttachmentFilters.extend({
id: 'media-attachment-taxonomy-filter',
createFilters: function() {
var filters = {};
// Formats the 'terms' we've included via wp_localize_script()
@danielbachhuber
danielbachhuber / gist:7684646
Created Nov 27, 2013
How to integrate WordPress Core updates with your custom Plugin or Theme
View gist:7684646
<?php
/**
* How to integrate WordPress Core updates with your custom Plugin or Theme
*
* Filter the `update_plugins` transient to report your plugin as out of date.
* Themes have a similar transient you can filter.
*/
add_filter( 'site_transient_update_plugins', 'wprp_extend_filter_update_plugins' );
add_filter( 'transient_update_plugins', 'wprp_extend_filter_update_plugins' );
function wprp_extend_filter_update_plugins( $update_plugins ) {
View tasty-recipes-filter-block-output.php
<?php
/**
* Filters the output of the Tasty Recipes block.
*
* @param string $output Existing output.
* @param array $block Block data.
* @return string
*/
add_filter( 'render_block', function( $output, $block ){
// Only modify the Tasty Recipes block.
View tasty-recipes-ratings-top.php
<?php
/**
* Adds the Tasty Recipes ratings to the top of the post.
*
* @param string $content Existing post content.
* @return string
*/
add_filter( 'the_content', function( $content ) {
if ( ! class_exists( 'Tasty_Recipes' ) ) {
You can’t perform that action at this time.