Skip to content

Instantly share code, notes, and snippets.

@lgedeon
lgedeon / admin-notice.php
Created August 22, 2017 13:10
Wrong approach maybe some usable pieces.
<?php
/**
* Display our notice and hide default.
*/
function action_admin_notices() {
$screen = get_current_screen();
if ( ! in_array( $screen->id, array( 'post', 'page' ), true ) ) {
return;
}
@lgedeon
lgedeon / do_with_arrays.php
Last active October 6, 2017 21:40
Call a function on each combination of the values of multiple arrays. When array_map etc. isn't enough.
<?php
/**
* Call a function on each combination of the values of multiple arrays.
*
* First parameter should by a callable function name or an array with the
* following key value pairs:
*
* 'callback' string A callable function or method name.
* 'break_early' boolean True if function can quit early.
* 'break_on' mixed Value that if returned by callback will short-circuit iteration.
@lgedeon
lgedeon / helper.php
Last active December 15, 2017 20:54
Several snippets that could be handy on many projects.
<?php
// todo store as an option and address all concerns at https://core.trac.wordpress.org/ticket/14671
function helper_count_required_args ( $function ) {
static $arg_counts = array();
$key = is_scalar( $function ) ? $function : serialize( $function );
if ( isset( $arg_counts[$key] ) ) {
return $arg_counts[$key];
}
@lgedeon
lgedeon / missing-ids.php
Created February 2, 2018 21:11
Diagnostic code.
<?php
// This section is entirely for testing filters and other behavior peculiar to the VIP environment.
if ( isset( $post->post_content ) ) {
$urls = $this->get_content_images( $post->post_content );
$image_ids = [];
foreach ( $urls as $url ) {
$image_ids[] = $this->get_attachment_id( $url );
}
@lgedeon
lgedeon / log.php
Created February 7, 2018 18:28
Self-contained log function.
<?php
/**
* Log messages, timestamps and values throughout a page load and return them at the end for output at the bottom of
* the page or for storage with a single database touch.
*
* @param string $message Log message.
* @param mixed $value Log value.
* @param string $format Optional. Return type. Either text or array. Default array.
*
* @return array|string
@lgedeon
lgedeon / report-hook-args.php
Created February 27, 2018 04:15
Generic argument report for filters and action hooks.
<?php
function report_args() {
echo "\n----------" . current_filter() . "\n";
$args = func_get_args();
var_export( $args );
return $args[0];
}
add_filter( 'nav_menu_item_args', __NAMESPACE__ . '\report_args', 10, 3 );
@lgedeon
lgedeon / auto-create-menu.php
Created March 5, 2018 15:00
Automatically create menus for local dev environment.
<?php
// Register navigation/menu locations.
register_nav_menus( array(
'footer' => 'Footer',
'footer-social' => 'Footer Social Links',
) );
add_action( 'admin_init', function () {
if ( ! has_nav_menu( 'footer' ) ) {
<?php
/**
* Allow italics to be used in the sitename.
*
* @action bloginfo
*
* @param mixed $output The requested non-URL site information.
* @param mixed $show Type of information requested.
*
* @return mixed
@lgedeon
lgedeon / submenu.php
Created March 14, 2018 20:16
Add class to sub-menu item.
<?php
add_filter( 'nav_menu_css_class', [ $this, 'add_sub_menu_item_css_class' ], 10, 4 );
/**
* Filters the CSS class(es) applied to a menu item's list item element.
*
* @filter nav_menu_css_class
*
* @param array $classes The CSS classes that are applied to the menu item's `<li>` element.
* @param WP_Post $item The current menu item.
@lgedeon
lgedeon / links-to-spans.php
Created April 15, 2018 03:28
Convert list of category links to spans.