Skip to content

Instantly share code, notes, and snippets.

View danielbachhuber's full-sized avatar

Daniel Bachhuber danielbachhuber

View GitHub Profile
@danielbachhuber
danielbachhuber / gist:7684646
Created November 27, 2013 23:06
How to integrate WordPress Core updates with your custom Plugin or Theme
<?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 ) {
@danielbachhuber
danielbachhuber / wordpress-language-codes.csv
Created April 24, 2020 12:35
Language / locale codes used in WordPress
language english_name native_name
af Afrikaans Afrikaans
ar Arabic العربية
ary Moroccan Arabic العربية المغربية
as Assamese অসমীয়া
az Azerbaijani Azərbaycan dili
azb South Azerbaijani گؤنئی آذربایجان
bel Belarusian Беларуская мова
bg_BG Bulgarian Български
bn_BD Bengali (Bangladesh) বাংলা
@danielbachhuber
danielbachhuber / gist:3013900
Created June 28, 2012 21:08
Use the Rewrite API to interpret AJAX requests
<?php
add_action( 'init', function(){
add_rewrite_rule( '^ajax/([^/]*)/([^/]*)/([^/]*)/([^/]*)/?','index.php?ajax=true&action=$matches[1]&type=$matches[2]&offset=$matches[3]&meta=$matches[4]','top' );
});
add_filter( 'query_vars', function( $query_vars ){
$query_vars[] = 'ajax';
@danielbachhuber
danielbachhuber / disable-logged-out-users.php
Last active September 11, 2023 21:52
Disable WP REST API requests for logged out users
<?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 / collection-filter.js
Last active April 18, 2023 20:30
Add a custom taxonomy dropdown filter to the WordPress Media Library
(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 / build-gutenberg-nightly.sh
Last active February 14, 2023 11:08
Create a build of the Gutenberg master branch
set -ex
# Expects git clone git@github.com:WordPress/gutenberg.git ~/gutenberg
cd ~/gutenberg
# Reset working directory
git checkout -f master
git pull origin master
# Run the initial gutenberg ZIP build
yes | npm run package-plugin
# Modify 'Version: ' to bump to next version and append short hash (e.g. '4.0-alpha-610aa4e')
echo '<?php file_put_contents( "gutenberg.php", preg_replace_callback( "#Version: (.+)#", function( $matches ) { $new_version = (float) $matches[1] + .1; $new_version .= ".0-alpha-" . substr( shell_exec( "git rev-parse HEAD" ), 0, 7 ); return str_replace( $matches[1], $new_version, $matches[0] ); }, file_get_contents( "gutenberg.php" ) ) );' | php
@danielbachhuber
danielbachhuber / gist:9379135
Created March 5, 2014 23:43
Fix network admin URL to include the "/wp/" base
<?php
/**
* Fix network admin URL to include the "/wp/" base
*
* @see https://core.trac.wordpress.org/ticket/23221
*/
add_filter( 'network_site_url', function( $url, $path, $scheme ){
$urls_to_fix = array(
'/wp-admin/network/',
@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 / gist:9550397
Created March 14, 2014 15:48
In multisite, give editors and above the ability to upload whatever they want.
<?php
/**
* Allow editors and above to upload whatever they want
*/
add_filter( 'map_meta_cap', function( $caps, $cap, $user_id ) {
if ( 'unfiltered_upload' !== $cap ) {
return $caps;
}
@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;