Always Redirect User to Translated Pages with the WPML Plugin for WordPress

The Context I recently worked on my first translation site for WordPress. We used the WPML (WordPress Multilingual) plugin. The client wanted to have all pages that had an associated translation automatically have users whom have set their browser language to be redirected to the translated page…every time. There is a setting with the value … Continued

A Fun Sorting Lexicographically Bug

Ran into a fun little bug today. We have an integration that’s been around since 2016 where we save their id values as a string because we can’t rely on them not throwing in letters. Looking at the db it seems like they were earlier on but have been only using numbers for a while. … Continued

Installing xdebug for use on remote server

Installing xdebug for use on remote server

I used instructions found in the PHPStorm docs with the xdebug3 set of code. I’m just going to mention some specifics that aren’t listed in the PHPStorm docs or that I found particularly key. Setting up an SSH tunnel https://www.jetbrains.com/help/phpstorm/remote-debugging-via-ssh-tunnel.html#set-up-an-ssh-tunnel-to-the-remote-machine This sets up a way for your remote server to talk to your local computer. … Continued

JSON encoding Windows-1252 Unicode chars

While trying to use the json_encode function a Windows-1252 left and right quote character was breaking the encoding and I was ending up with no JSON. The string being encoded was from html within a PHP file. I’m guessing it was copy pasted from a Word doc. To fix this the <a href="http://php.net/manual/en/function.mb-convert-encoding.php">mb_convert_encoding</a> function, not the <a href="http://php.net/manual/en/function.utf8-encode.php">utf8_encode</a> function, was … Continued

Add browser and device name to WordPress body class

Github Gist
<?php
// Add Browser-Detection
function mv_browser_body_class($classes) {
    global $is_lynx, $is_gecko, $is_IE, $is_opera, $is_NS4, $is_safari, $is_chrome, $is_iphone;
    
    if($is_lynx) $classes[] = 'lynx';
    elseif($is_gecko) $classes[] = 'gecko';
    elseif($is_opera) $classes[] = 'opera';
    elseif($is_NS4) $classes[] = 'ns4';
    elseif($is_safari) $classes[] = 'safari';
    elseif($is_chrome) $classes[] = 'chrome';
    elseif($is_IE) {
            $classes[] = 'ie';
            if(preg_match('/MSIE ([0-9]+)([a-zA-Z0-9.]+)/', $_SERVER['HTTP_USER_AGENT'], $browser_version))
            $classes[] = 'ie'.$browser_version[1];
    } else $classes[] = 'unknown';
    if($is_iphone) $classes[] = 'iphone';
    if ( stristr( $_SERVER['HTTP_USER_AGENT'],"mac") ) {
             $classes[] = 'osx';
       } elseif ( stristr( $_SERVER['HTTP_USER_AGENT'],"linux") ) {
             $classes[] = 'linux';
       } elseif ( stristr( $_SERVER['HTTP_USER_AGENT'],"windows") ) {
             $classes[] = 'windows';
       }
    return $classes;
}
add_filter('body_class','mv_browser_body_class');
 

Recursively List Video Files URL’s

This will recursively list video files (currently filtered by a ‘.mp4’ preg_match) links when put at the root of a url. Rough and not pretty but it’s functional. Github Gist <?php function readDirs($path){ $dirHandle = opendir($path); while($item = readdir($dirHandle)) { $newPath = $path."/".$item; if(is_dir($newPath) && $item != '.' && $item != '..') { readDirs($newPath); } … Continued

Force Post Author Comments to Moderation in WordPress

By default in WordPress when an author of a post comments on their own posting the comment is automatically approved. This makes sense when authors are associated with the site, have access to admin, etc. There are cases where content from the general public is used to create posts and while the user is logged … Continued

Always Unset Values Passed by Reference in a Loop

I’d always read to unset the variables passed by reference in PHP’s foreach loop. Here’s an example: foreach ($posts as &$post) { $current_post = self::get_current_version_for_post($post->ID, true, true); $post->current_post_modified = mysql2date('U', $current_post->post_modified); } unset($post); In the above code I was looping through a set of parent posts and using a custom method to get what I … Continued

Allow html in Contact Form 7 error messages

Contact Form 7 has a filter where one can alter the messages being sent back to the browser called wpcf7_validation_error. A simple htmlspecialchars_decode altering the $message variable before returning will do the trick. This function will run on any error for any form you have in your site (any CF7 form that is). If you … Continued

Jetpack 404 not found: devicepx.js

Jetpack 404 not found: devicepx.js

While setting up this site I noticed some files not found errors in dev console. One was devicepx.js which was in the jetpack modules path. It’s used to optionally load retina/HiDPI versions of files. I believe it would be for gravatars and since this site doesn’t use gravatars I deemed it unnecessary. //remove unecessary queing … Continued

Earliest WordPress Hook Where Global $post Variable Exists

In the file wp-includes/class-wp.php there is a hook run which is simply named 'wp'. This, as far as I can tell, seems to be the earliest hook in which the $post object variable exists. The hook is passed the &$this object which the comments state: references the WP object. It allows for accessing the properties … Continued