Skip to content

Instantly share code, notes, and snippets.

View stracker-phil's full-sized avatar

Philipp Stracker stracker-phil

View GitHub Profile
@stracker-phil
stracker-phil / get-css-definition.js
Last active December 28, 2021 14:45
Highly optimised and accurate alternative to the deprecated `getMatchedCSSRules()` method. StackOverflow question: https://stackoverflow.com/questions/66078682/get-the-css-definition-of-an-element/
/**
* Scans all CSS rules in the current document to find the most
* specific definiton of a single CSS property for a given element.
*
* Usage: getStyleDef('#my-element', 'width');
* --> returns the most specific "width" defintiion, e.g. "27em".
*
* @param {HTMLElement} element - The HTML Element to inspect.
* @param {string} prop - The CSS property to inspect.
* @return {string} The most specific CSS definition,
@stracker-phil
stracker-phil / test_json_performance.php
Last active October 4, 2022 23:57
Performance comparison for various PHP functions that test, if a string is valid JSON.
<?php
// https://stackoverflow.com/a/6041773/313501#answer-6041773
function test1( $value ) {
if ( ! is_scalar( $value ) ) {
return null;
}
json_decode( $value );
return ( json_last_error() == JSON_ERROR_NONE );
@stracker-phil
stracker-phil / forminator-via-ajax-script.js
Last active April 18, 2024 18:12
Load Forminator Form via Ajax
// This JS file is loaded by the theme:
(function() {
// The button with the CSS class "get-support" loads the form.
jQuery('.get-support').on('click', load_support_form);
// The form is displayed in a div tag with the CSS class "support-form".
function load_support_form() {
jQuery.get('/wp-admin/admin-ajax.php?action=my_get_support')
.then(function(response) {
@stracker-phil
stracker-phil / wp-dev-login.php
Last active September 18, 2020 11:13
Small MU-Plugin that disables passwords on local development sites. Once installed, you can select a user from a dropdown list and log-in with a single click. ONLY FOR LOCAL DEV-SITES! NEVER USE THIS ON A PUBLIC SITE!
<?php
/**
* Passwordless login for development environments.
*
* Setup:
* 1. Make sure that the "wp-contents/mu-plugins" folder exists. Create it if needed.
* 2. Save this file as "wp-contents/mu-plugins/wp-dev-login.php"
* 3. Check the conditions in line 29 - 30 and adjust them to your requirements.
*
* Once installed, all default WP login forms will display a dropdown list of all
@stracker-phil
stracker-phil / class-mycode.php
Last active January 24, 2023 12:37
Sample Block for MailPoet feature request
<?php
/**
* Custom Block to output Code.
*/
namespace MailPoet\Newsletter\Renderer\Blocks;
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
@stracker-phil
stracker-phil / global-search.js
Last active March 14, 2024 19:10
Recursively searches the entire object tree for a given value
/**
* Recursively searches the startObject for the given value.
*
* All matches are displayed in the browser console and stored in the global variable "gsResults"
* The function tries to simplify DOM element names by using their ID, when possible.
*
* Usage samples:
*
* globalSearch( document, 'someValue' ); // Search entire DOM document for the string value.
* globalSearch( document, '^start' ); // Simple regex search (function recognizes prefix/suffix patterns: "^..." or "...$").
@stracker-phil
stracker-phil / format-date.js
Created June 20, 2019 09:42
A short ES6 function that converts a JS Date object into a localized date-string. It takes the same parameters as the PHP `date` function.
// Detects the browser locale for Date translations.
const locale = (navigator.languages && navigator.languages.length) ?
navigator.languages[0] :
navigator.language ?
navigator.language :
'en';
// Date formatter with php-compatible format syntax
export const formatDate = (format, date) => {
if (format === undefined) {
@stracker-phil
stracker-phil / wp-config.php
Last active May 1, 2019 17:26
Template for a wp-config.php file with included environment detection
<?php
/**
* The base configuration for WordPress
*
* @link https://codex.wordpress.org/Editing_wp-config.php
*
* @package WordPress
*/
// Defines the environment (prod|stage|dev)
@stracker-phil
stracker-phil / wp-action-remove-draft-from-menu.php
Last active April 8, 2019 16:41
This filter removes all unpublished posts and pages from WordPress menus.
<?php
add_filter( 'wp_nav_menu_objects', 'pst_nav_menu_objects', 10, 2 );
/**
* Modify the WordPress menu and remove entries that are not visible for the current
* user. This applies to all menus (primary, footer, widget ...)
*/
function pst_nav_menu_objects( $items, $args ) {
// If you do not want to modify ALL menus, you can check for the menu-location
// or other criteria here.
@stracker-phil
stracker-phil / wp-action-name-and-prio.php
Created April 1, 2019 21:32
Determine the name and priority of the currently called action/filter callback in WordPress.
<?php
/**
* Output the current action name and priority.
*/
function pst_action_and_priority() {
global $wp_filter, $wp_current_filter;
// Find the currently running WP action/filter name.
$action = end( $wp_current_filter );