Skip to content

Instantly share code, notes, and snippets.

@pattihis
pattihis / edit_all_users.php
Last active April 6, 2021 21:07
Shortcode to edit all Wordpress users: change user_login based on email username and populate display_name/nickname
<?php
function edit_all_users($atts) {
global $wpdb;
$output = '<ol>';
foreach( get_users() as $user ) {
$username = get_user_by( 'id', $user->ID )->user_login;
$email = get_user_by( 'id', $user->ID )->user_email;
$pos = strpos($username, '@');
if ($pos !== false) {
@pattihis
pattihis / julia.php
Last active February 8, 2022 11:24
Wordpress - Redirect home page to Shop
<?php
// Put this in functions.php
add_action( 'template_redirect', function() {
if( is_home() or is_front_page() ) {
$url = esc_url(home_url( '/shop/'));
wp_redirect($url);
exit();
}
});
@pattihis
pattihis / toggle-metabox.php
Last active December 17, 2022 10:49
WordPress: Show/Hide custom Metabox in Editor Screen based on page template selected.
<?php
add_action('admin_head', 'metaboxScripts');
function metaboxScripts() {
global $current_screen;
if ('page' != $current_screen->id) return;
echo <<<HTML
<script type="text/javascript">
@pattihis
pattihis / safe_get_query_var.php
Created March 1, 2025 18:08
A replacement function for WordPress's get_query_var() that addresses the issue of query variables not being set on some servers, by parsing the globals directly.
<?php
/**
* Get the query variable even if the server rewrite rules do not support it.
* This is a workaround for the issue where the query vars are not set in the globals.
*
* @global WP_Query $wp_query WordPress Query object.
*
* @param string $query_var The variable key to retrieve.
* @param mixed $default_value Optional. Value to return if the query variable is not set.
* Default empty string.