Ben Lobaugh Online

/(bb|[^b]{2})/

Category: Computing (Page 1 of 47)

Mute and unmute your mic quickly with this Alfred Workflow

I am on a lot of calls for work every day and juggle several different call applications. Finding the apps mute/unmute button quickly gets tedious. On some apps the UI is so poorly designed as to make toggling the mic nearly unusable.

Enter Alfred! Alfred is an amazing productivity app. Alfred let you run custom commands and hotkeys in custom Workflows.

I created a Workflow that, once installed, allows you to mute and unmute your mic with the command-M hotkey.

To install simply download the Workflow and double click it to load it into Alfred.

Click here to download the Mic PTT Alfred Workflow

The Workflow is on Github! If you have ideas for improvements create a fork and send them to me: https://github.com/blobaugh/Mic-PTT-Alfred-Workflow

WordPress Multisite: Global options that can be overridden per site

I recently ran into a situation where a WordPress Multisite install needed to have some options replicated across each site, both new and existing. It also needed to be able to override the options per site if so desired. The solution is fairly trivial.

  • Setup the options page in wp-admin. Ensure it exists on all subsites
  • On the main site fill in the values that should be defaulted across all sites
  • Drop the following code into your site and replace the placeholder with your own option name
  • Freely use the values from the main site or alter the options on a subsite

All you need to do to get this working is drop the following code into your site and change the placeholder to be the same as the option name that should be replicated.

add_filter( 'option_{OPTION_NAME}', 'my_option_settings', 10, 2 );
add_filter( 'default_option_{OPTION_NAME}', 'my_option_settings', 10, 2 );

function my_option_settings( $value, $option_name ) {
  // Do not loop on ourself
  if( 1 == get_current_blog_id() ) {
    // Bail out
    return $value;
  }
  if( ! $value ) {
    $value = get_blog_option( 1, $option_name );
  }
  return $value;
}

WordPress: Add category to permalink and redirect old permalinks

Occasionally you run across a situation where a website permalink structure needs to be changed from what it has been to a new structure. Most sites that have been public will have links to them from other websites that need to properly redirect to the new permalink structure. Removing an element from the permalink structure and redirecting old links is trivial, however adding an additional element to the url structure can be difficult. For example:

A fairly typical structure is: /%year%/%month%/%day%/%postname%/

Lets use a simple example of adding the category to the permalink structure: /%category%/%year%/%month%/%day%/%postname%/

To provide a more useful example:

Original: https://ben.lobaugh.net/2016/05/15/cruising-the-san-juans
With category: https://ben.lobaugh.net/sailing/2016/05/15/cruising-the-san-juans

The original url is going to display the 404 page. I am going to provide an example that you can use in your own site to capture the url causing the 404 and attempt to locate the new post permalink. If none can be found it will fallback to the 404 page. As long as there is something in the original url you can use to locate a post (like the  %postname%) this method will work with minor tweaks.

add_action( 'template_redirect', 'maybe_redirect_404_old_permalink' );
/**
 * Attempts to forward old permalinks to the new permalink structure
 *
 * @author Ben Lobaugh
 */
function maybe_redirect_404_old_permalink() {
    // Only run this function if we are on a 404
    if( ! is_404() ) {
        return;
    }
 
    // "trick" to get the full URL
    $url = add_query_arg( '', '' );

    /*
     * Pull the URL path apart to find a slug (post_name)
     * The final segment should be the slug
     */
    $parts = explode( '/', $url );
    $parts = array_filter( $parts );
    $size = count( $parts );
    $maybe_slug = $parts[ $size ]; // We use size here because the filter turned 1 based

    // Attempt to locate corresponding post in the database
    $args = array(
        'name'        => $maybe_slug,
        'post_type'   => 'post',
        'post_status' => 'publish',
        'numberposts' => 1,
    );

    $posts = get_posts( $args );

    // Identify a found post
    if( $posts && ! empty( $posts[0]->ID ) ) {
        $post_id = $posts[0]->ID;

        $post_url = get_permalink( $post_id );

        // Attempt to forward to the new post permalink
        if( $post_url ) {
            wp_safe_redirect( $post_url, 301 ); // Permanent redirect
        }
    }

    /*
     * If we made it down here then we could not find a matching post in
     * the database. No biggie, simply do nothing and display the 404 page
     * as normal 🙂
     */
}

MySQL: Count number of tables in database

If you need to count the number of tables that exist in a MySQL database you can do so with the following query. Just remember to swap out the database name!

SELECT count(*) 
FROM information_schema.tables 
WHERE table_schema = 'YOUR_DB_NAME'

Bash script to automatically convert git submodules to regular files

Git submodules drive me batty! They are a great idea in theory however in practical application they are a pain in the butt to work with.

I have a project that has accumulated over a dozen submodules over the past couple years. Switching branches and merging anything has become excruciating. This morning was the last straw. I WANT THEM GONE! Removing dozens of submodules by hand is time consuming so I tossed together this quick Bash script. I hope it is helpful to anyone else out there struggling with git submodules.

#!/bin/bash

# Get a list of all the submodules
submodules=($(git config --file .gitmodules --get-regexp path | awk '{ print $2 }'))

# Loop over submodules and convert to regular files
for submodule in "${submodules[@]}"
do  
   echo "Removing $submodule"
   git rm --cached $submodule # Delete references to submodule HEAD
   rm -rf $submodule/.git* # Remove submodule .git references to prevent confusion from main repo
   git add $submodule # Add the left over files from the submodule to the main repo
   git commit -m "Converting submodule $submodule to regular files" # Commit the new regular files!
done

# Finally remove the submodule mapping
git rm .gitmodules

Page 1 of 47

Powered by WordPress & Drip City Coffee