Posts

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; }

The Most Efficient Way to Bulk Delete Things in WordPress

What is the most efficient way to delete thousands of posts, custom post types, media attachments and other things in WordPress? The answer is: not through the admin panel. If you have ever tried deleting a couple of hundred (or so) items from the WordPress admin panel before, then you would know if the request doesn’t timeout, you’ve won the WordPress lottery. And, if it does timeout, you have to keep spamming the refresh button until it works.

My Experience Writing a Long-Running PHP Script to Parse News Content From the Associated Press News API

Filed under: super-specific use case with hints of generality for those wanting to write long-running PHP scripts. For a little while now, I have been building a site with WordPress that consumes news content from the Associated Press news API and then stores the news content in WordPress. My first iteration of the ingest engine with PHP worked quite well, but I encountered dreaded NGINX server timeouts and other issues.

How to Work With DATETIME Values in JavaScript

Dates in Javascript have always been a PITA. It is one of the reasons that libraries like MomentJS reigned supreme for so long. Things are complicated, especially if you’re dealing with different timezones. Fortunately, working with simple DATETIME values has never really been a problem. What prompted this post was that I am working with WordPress a lot at the moment, and all dates in WordPress are MySQL DATETIME formatted dates. Say you have a DATETIME value that looks like this: 2021-10-05 00:00:00 — you can use the Javascript Date object on this because it’s a valid date value.

The Whole of Twitch (and a bunch of other affiliated sites) Has Been Leaked

If you thought the CD Projekt Red leak of Cyberpunk 2077 and other source code was bad, get a load of this latest leak. Twitch just got hacked entirely, and the entirety of its source code, internal repositories, financial payout information and absolutely everything you can think of has been taken and put online in a 128GB torrent over on 4chan. The anonymous individual or group says, “we have completely pwned them,” and let’s be honest, they’ve well and truly pwned Twitch here. What a massive leak. Some heads are going to roll over in the IT department over this. I am curious how this even happened.