Skip to content

Instantly share code, notes, and snippets.

Avatar

Kees Meijer keesiemeijer

View GitHub Profile
@keesiemeijer
keesiemeijer / wp-update
Last active May 15, 2021
A bash script to update everything WordPress (core, plugins, themes and comments).
View wp-update
#!/usr/bin/env bash
# =============================================================================
#
# *** WARNING: THIS SCRIPT IS NO LONGER MAINTAINED ***
#
# use https://github.com/keesiemeijer/wp-update instead
#
# =============================================================================
@keesiemeijer
keesiemeijer / setup-phpunit.sh
Last active Dec 29, 2020
Setup PHPUnit for use in the Local by Flywheel app
View setup-phpunit.sh
#!/usr/bin/env bash
# ===============================================================================
# Script to install PHPUnit in the Local by Flywheel Mac app
# These packages are installed
#
# PHPUnit, curl wget, rsync, git, subversion and composer.
#
# WordPress is installed in the `/tmp/wordpress` directory for use by PHPUnit.
# The WordPress test suite is installed in the `/tmp/wordpress-tests-lib` directory.
@keesiemeijer
keesiemeijer / functions.php
Last active Nov 23, 2016
Only allow one shortcode in post content
View functions.php
add_filter( 'the_content', 'MyTheme_do_first_shortcode' );
function MyTheme_do_first_shortcode( $content ) {
// EDIT: Shortcode name (without brackets)
$shortcode = 'shortcode-name';
if ( has_shortcode( $content, $shortcode ) ) {
$regex = get_shortcode_regex( array( $shortcode ) );
$i = 0;
@keesiemeijer
keesiemeijer / functions.php
Created Sep 18, 2016
Insert HTML between paragraphs in WordPress post content
View functions.php
<?php
/**
* Insert HTML between paragraphs in WordPress post content using php DOMDocument().
*
* Inserts HTML in the middle of top level paragraphs.
* For example:
*
* <p>Hello</p>
* <blockquote>
* <p>Awesome</p><!-- not a top level paragraph -->
@keesiemeijer
keesiemeijer / functions.php
Last active Feb 5, 2016
WordPress — Query all posts in a taxonomy
View functions.php
<?php
// Add a new public query variable: taxonomy_archive
add_filter ( 'query_vars', 'add_taxonomy_query_var' );
function add_taxonomy_query_var( $query_vars ) {
$query_vars['taxonomy_archive'] = '';
return $query_vars;
}
add_action( 'posts_clauses', 'taxonomy_archive_clauses', 10, 2 );
@keesiemeijer
keesiemeijer / functions.php
Last active Feb 5, 2016
Add post terms to related posts results
View functions.php
<?php
add_filter( 'related_posts_by_taxonomy_posts_clauses', 'rpbt_post_where', 10, 4 );
function rpbt_post_where( $pieces, $post_id, $taxonomies, $args ) {
global $wpdb;
$pieces['select_sql'] .= " , GROUP_CONCAT( DISTINCT tt.term_id SEPARATOR ',' ) as related_terms ";
return $pieces;
}
@keesiemeijer
keesiemeijer / functions.php
Last active Nov 16, 2015
Easy way to have text linking to images
View functions.php
<?php
add_filter( 'attachment_fields_to_edit', 'km_attachment_text_link_field', 10, 2 );
function km_attachment_text_link_field( $form_fields, $post ) {
$form_fields['km-text-link'] = array(
'label' => 'Text Link',
'input' => 'text',
'value' => '',
'helps' => 'If Text Link is provided, the image will be linked from a text link',
@keesiemeijer
keesiemeijer / bootstrap.php
Last active Mar 28, 2016
Install the Wordpress PHPUnit test framework from the same branch as the WordPress version you test against. The benefit over WP-CLI unit tests scaffolding is that you can test any version without fatal errors for undifined functions or classes
View bootstrap.php
<?php
$_tests_dir = '/tmp/wordpress-tests-lib';
require_once $_tests_dir . '/includes/functions.php';
function _manually_load_plugin() {
require dirname( dirname( __FILE__ ) ) . '/ddd.php';
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
@keesiemeijer
keesiemeijer / get-page-templates.php
Last active Feb 5, 2016
get all page templates
View get-page-templates.php
<?php
$post_templates = array();
$theme = wp_get_theme();
$files = (array) $theme->get_files( 'php', 1 );
foreach ( $files as $file => $full_path ) {
$headers = get_file_data( $full_path, array( 'Template Name' => 'Template Name' ) );
if ( empty( $headers['Template Name'] ) ) {
continue;
View shortcode-regex-finder.php
<?php
/**
* Plugin Name: Shortcode Regex Finder
* Plugin URI:
* Description: This plugin creates the regex WordPress would use to find a specific shortcode in content.
* Author: keesiemijer
* License: GPL v2
* Version: 1.0
* Text Domain: shortcode-regex-finder
*/