Login as anyone on Wordpress

Sometimes you need to test what a user is seeing. This snippet lets your IP address login as anyone. You need to enter a password just so it isn’t empty, but it can be anything. This hook says any password is a match if it’s from your IP address. add_filter(‘check_password’, function( $match ){ return $_SERVER[‘REMOTE_ADDR’] […]

Wordpress Sitemaps: disable, remove users, add lastmod

Wordpress 5.5 shipped with sitemaps enabled by default.  This quite possibly isn’t a good thing. Disable them entirely add_filter( ‘wp_sitemaps_enabled’, ‘__return_false’ ); Disable just the Users sitemap add_filter( ‘wp_sitemaps_add_provider’, function( $prov, $name ) { return ‘users’ === $name ? false : $prov; }, 10, 2 ); <lastmod> If you do want them, you may want […]

Remove URL slug from custom post type

Two functions, first one removes the slug from links throughout the site. Second one adds the custom post type to the query, so that it looks for the name in both regular posts and custom post type. function remove_cpt_slug( $link, $post ) { if ( ‘my_post_type’ === $post->post_type ) { $link = str_replace( ‘/’ . […]

Redirect entire site to a new domain with apache rewrite

Just put one of these rules at the top of your .htaccess file. Pick your favorite. You might need to add in the forward slash after newdomain.com depending on your setup. RewriteRule ^(.*) https://newdomain.com$1 [R=301,L] RewriteRule ^ https://newdomain.com%{REQUEST_URI} [R=301,L] RedirectMatch 301 ^(.*) https://newdomain.com$1 If you still want access to something (say adminer so you can […]

Wordpress logs you out at inconvenient times

It’s annoying to be signed out in the middle of your work. The default keeps you logged in for 2 weeks, and a lot of suggestions for changing this time use a multiple of 24 hours… so you’re always trapped in a cycle of getting logged out during working hours. (Say you log in monday […]

VS Code doesn’t select the $$$

When using Microsoft Visual Studio Code / Monaco for PHP it’s really annoying to me how double-clicking a variable doesn’t include the dollar sign in the selection. Go to the settings and search for editor.wordSeparators: Word Separators Characters that will be used as word separators when doing word related navigations or operations. You can simply […]

Add a spinner to a submit button for slow forms

If you have a form that is slow to submit, this is a simple way to replace the submit button with a spinner. You may need to change the two selectors in the Javascript to be more specific, or different. Border-color may also want to be different, that’s the color of the spinner. <script>document.querySelector(‘form’).addEventListener(‘submit’,function(){ document.querySelector(‘[type=submit]’).className+=’ […]

Delete Wordpress post revisions older than x days

Delete post revisions older than 10 days: DELETE FROM wp_posts WHERE post_type = ‘revision’ AND post_modified_gmt < DATE_SUB(NOW(), INTERVAL 10 DAY) Delete post revisions older than 2 months: DELETE FROM wp_posts WHERE post_type = 'revision' AND post_modified_gmt < DATE_SUB(NOW(), INTERVAL 2 MONTH)

Quickly get rewrite rules for all wordpress posts

Run this SQL command (using adminer or whatever) and you’ll get a list of mod_rewrite rules ready to paste into your .htaccess file. SELECT CONCAT(‘RewriteRule ^’, post_name, ‘ https://yoururl.com/’, post_name, ‘/ [R=301,L]’) as ‘RewriteEngine On’ FROM `wp_posts` WHERE `post_type` = ‘post’ AND `post_status` = ‘publish’ ORDER BY `post_name` Change ‘post’ to ‘page’ if you want […]