Skip to content

Instantly share code, notes, and snippets.

View danielbachhuber's full-sized avatar

Daniel Bachhuber danielbachhuber

View GitHub Profile
@danielbachhuber
danielbachhuber / bylines-only-posts.php
Last active July 28, 2017 14:21
Limit bylines to only posts
<?php
/**
* Only use bylines with posts.
*
* @see https://bylines.io/docs/customize-post-types/
*/
add_filter( 'bylines_post_types', function() {
return array( 'post' );
});
@danielbachhuber
danielbachhuber / thrasher-v2.php
Created June 5, 2017 18:14
Thrash APCu until it can no longer allocate memory
<?php
/**
* Thrash APCu until it can no longer allocate memory
*/
header("Cache-Control: no-cache, no-store, must-revalidate");
header("Pragma: no-cache");
header("Expires: 0");
@danielbachhuber
danielbachhuber / host-check.php
Last active September 20, 2017 01:05
Check that the WordPress install is still hosted at its internal domain.
<?php
/**
* Check that the WordPress install is still hosted at its internal domain.
*
* Run with `wp --require=host-check.php host-check --path=<path-to-wp>`
*
* Disables WP cron to prevent 'wp_version_check' from being run.
*
* @when before_wp_load
<?php
global $start_time;
function log_message( $message ) {
global $start_time;
$elapsed_time = microtime( true ) - $start_time;
WP_CLI::log( sprintf( '[%s] %s', format_log_timestamp( $elapsed_time ), $message ) );
}
@danielbachhuber
danielbachhuber / gist:b4e99931ceb570134b6fec826bff46ce
Created February 24, 2017 14:00
ack "When I (run|try)" *.feature --count
aliases.feature:29
cache.feature:18
cap.feature:8
cli-bash-completion.feature:23
cli-info.feature:4
cli.feature:15
command.feature:44
comment-generate.feature:5
comment-list.feature:5
comment-meta.feature:10
@danielbachhuber
danielbachhuber / fetch-wpcom.php
Created February 22, 2017 01:32
Fetch a post from WordPress.com and insert into the database.
<?php
if ( class_exists( 'WP_CLI' ) ) {
/**
* Fetch a post from WordPress.com and insert into the database.
*
* <site>
* : Site URL to download from.
*
* <slug>
@danielbachhuber
danielbachhuber / add-rel-nofollow-checkbox.php
Created February 13, 2017 17:06
Add a 'Add rel="nofollow" to link' checkbox to the WordPress link editor
<?php
/**
* Add a 'Add rel="nofollow" to link' checkbox to the WordPress link editor
*
* @see https://danielbachhuber.com/tip/rel-nofollow-link-modal/
*/
add_action( 'after_wp_tiny_mce', function(){
?>
<script>
@danielbachhuber
danielbachhuber / email-address-login.php
Created August 3, 2016 13:17
Display email address instead of the user_login when user is already logged in
<?php
add_filter( 'wp_login_errors', function( $errors ){
$GLOBALS['hb_backup_user_login'] = null;
if ( ! empty( $GLOBALS['user_login'] ) && empty( $_POST['log'] ) ) {
$GLOBALS['hb_backup_user_login'] = $GLOBALS['user_login'];
$GLOBALS['user_login'] = wp_get_current_user()->user_email;
}
return $errors;
});
@danielbachhuber
danielbachhuber / customizer.css
Created July 19, 2016 22:05
Visually indicate draggable elements in the Customizer
.widget-title h3:before,
.menu.ui-sortable .item-title:before {
content: "↕";
font-size: 1.4em;
vertical-align: middle;
margin-right: 4px;
color: #989898;
}
@danielbachhuber
danielbachhuber / blog-archive.php
Last active January 21, 2022 02:26
Creating a blog post archive at /blog/ without awkwardly publishing a page
<?php
add_filter( 'register_post_type_args', function( $args, $post_type ) {
global $wp_rewrite;
if ( 'post' === $post_type && ! is_null( $wp_rewrite ) ) {
$archive_slug = 'blog';
// Setting 'has_archive' ensures get_post_type_archive_template() returns an archive.php template.
$args['has_archive'] = $archive_slug;
// We have to register rewrite rules, because WordPress won't do it for us unless $args['rewrite'] is true.
$archive_slug = $wp_rewrite->root . $archive_slug;