Skip to content

Instantly share code, notes, and snippets.

View keesiemeijer's full-sized avatar

Kees Meijer keesiemeijer

View GitHub Profile
@keesiemeijer
keesiemeijer / wp-update
Last active November 17, 2023 07:08
A bash script to update everything WordPress (core, plugins, themes and comments).
#!/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 December 8, 2023 11:02
Setup PHPUnit for use in the Local by Flywheel app
#!/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 November 23, 2016 18:53
Only allow one shortcode in post content
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 September 18, 2016 18:12
Insert HTML between paragraphs in WordPress post content
<?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 February 5, 2016 10:17
WordPress — Query all posts in a taxonomy
<?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 February 5, 2016 10:19
Add post terms to related posts results
<?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 November 16, 2015 16:23
Easy way to have text linking to images
<?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 March 28, 2016 12:48
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
<?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 February 5, 2016 10:20
get all page templates
<?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;
@keesiemeijer
keesiemeijer / shortcode-regex-finder.php
Last active January 17, 2018 19:45
Shortcode Regex Finder
<?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
*/