Posts

How to Sort an Array of Object Values Alphabetically and Numerically

While sorting an array of objects recently, I needed to sort by an identifier prefixed with two letters and ended with numbers. An example is “AQ110” — you might be tempted to reach for a library, but you can achieve this in Javascript with very little code. Say hello to localCompare. I had a list of values that looked like this: AQ2 AQ110 AQ19 AQ190 AQ64 AQ5 The lettering didn’t matter so much, but the numbers did. By default, just using a plain old .sort() will not be enough. If you were to use .sort() by itself, it would get tripped up on high numbers starting with the same value (eg. 10, 110, 11 and so on).

How to Query WordPress Multisite by ACF Option Values

Did you know WordPress Multisite got some powerful new functionality in WordPress 5.1, allowing you to query sites by meta values? This functionality allows you to query sites in a WordPress Multisite network by meta values and other query-based syntax to get back one or more locations. Like many who develop WordPress websites, I use the awesome Advanced Custom Fields plugin. What ACF Pro gives you is powerful field functionality that really should be core functionality in WordPress itself.

How to Remove the My Sites Menu From the WordPress Admin Bar

I’ve been working on a WordPress Multisite installation that is going to eventually have upwards of 20,000 sites. Somewhere around the 50 site mark, I noticed the WordPress admin panel was beginning to slow down. And, after some investigation, I discovered the “My Sites” menu in the admin bar was part of the problem. Using the admin_bar_menu action hook, we can bind to this admin bar (like most things in WordPress), and remove items we don’t want to show.

Shared Uploads Directory in WordPress Multisite

If you’re working with WordPress Multisite, you might encounter a scenario where you want all uploaded media to share the same folder. In my case, I needed all sites to use the same uploads directory as the parent site. As with most things in WordPress, there is some hook or filter you can use to change core WordPress functionality. For the upload directory, there is a filter called upload_dir which is called when the upload directory is configured.

WordPress Multisite Domain Issues? You Might Be Forgetting to Call restore_current_blog

Recently, I created a WordPress Multisite installation on Amazon AWS Lightsail. I had this bizarre issue where my child sites were working, but whenever I would try creating posts or pages, the new button would take me to the root domain. Chalk this one up to stupidity (on my part). It was a perplexing issue because it was acting like I was in the admin panel for the root domain, not the child site. It was even showing my post counts for the root domain (but not posts). Going to the permalinks screen was showing the root domina, everything was a disaster.

How to Remove Duplicate Items From an Array in JavaScript (the Easy Way)

Languages such as PHP have methods for de-duplicating arrays, but Javascript strangely does not. In PHP, you can write. array_uniqueAnd this function will remove duplicates. There are so many solutions online you will find for this problem. They mostly centre around using filter and index checks, others recommend libraries like Lodash. Forget installing libraries or writing convoluted functional programming inspired solutions. You can do this: const arr = [1, 2, 3, 9, 1, 6, 4, 3, 9, 2, 4, 6]; const unique = [...new Set(arr)]; If you were to console log the contents of unique using the spread operator, we convert it back to a unique array. You would get an array containing: [1, 2, 3, 9, 6, 4] — the Set object is creating for storing unique collections, not only arrays but other types of data too.

Creating a Custom WordPress REST API Endpoint That Can Accept Spaces

The WordPress REST API is one of my favourite features in WordPress. The ability to create custom endpoints for getting data and other facets of building in WordPress makes life so much easier. Recently, while building a custom endpoint that allowed for some custom post type content to be searched, I encountered a snag. register\_rest\_route( 'raw-queue/v1', '/queue/(?P[a-zA-Z0-9-]+)/(?P[\\d]+)/(?P[\\d]+)/(?P[a-zA-Z0-9-]+)/(?P([a-zA-Z])+)', array( 'methods' => 'GET', 'callback' => 'rest\_get\_news\_stories\_from\_category', 'permission\_callback' => '\_\_return\_true' ) ); For singular search terms, it worked well. However, once I searched for words like “basketball player”, I would get the dreaded 404 error.

Don’t Back Projects on Kickstarter Until Supply Chains Are Fixed

The world might be attempting to get back to normal under the “new normal” label, but supply chains are still absolutely busted. As we head towards Christmas 2021, things show no sign of going back to normal in the world of logistics and supply chains. Postal service providers like USPS and Australia post are struggling to deal with the large volumes of mail. USPS temporarily suspended postage to 21 countries, including Australia.

Querying WordPress Multisite Sites With Meta Queries

WordPress documentation is usually robust. However, recently I had a use case where I needed to query sites by custom meta values. In WordPress 5.1, the WP_Site_Query was introduced to allow a faster way to query sites in a WordPress Multisite setup without using the old hack of needing to get all ID values using get_sites and then inside of a loop using switch_to_blog to query the sites and get values.

Get Advanced Custom Field Options by Site ID in WordPress Multisite

Out of the box, Advanced Custom Fields offers immense power but provides no functions to work with WordPress Multisite sites. Fortunately, WordPress makes it easy to work with multisite sites. Throw the following function into your theme functions.php file and call it at will. Just provide the field name and site ID, the function will get your value from that site (if there is a value and field exists). function get\_acf\_site\_option($field\_name, $site\_id) { switch\_to\_blog($site\_id); $value = get\_field($field\_name, 'option'); restore\_current\_blog(); return $value ? $value : null; }