Johan Hermansson https://johanhermansson.se Web developer who says random things Mon, 22 May 2023 09:12:38 +0000 sv-SE hourly 1 https://wordpress.org/?v=6.7.1 Delete orphan postmeta rows in WordPress https://johanhermansson.se/2020/08/delete-orphan-postmeta-rows-in-wordpress Tue, 18 Aug 2020 14:57:36 +0000 https://johanhermansson.se/?p=576 Running a WordPress site for a long time probably means you’ve tried out lots of different plugins handling your data. This usually means you have a lot orphans in your database. So very sad.

Delete orphan postmeta rows in Wordpress 1

Of course you want an orphan free database!

But you can easily clean up your database by running a few SQL queries.

First you should probably look if you have any orphan wp_postmeta rows in your database:

SELECT *
FROM wp_postmeta AS pm
LEFT JOIN wp_posts AS p ON p.ID = pm.post_id
WHERE p.ID IS NULL

Doing a LEFT JOIN means that SQL will look for posts, but still return a row if no parent post was found. And we only want to get post meta rows with no post parent; therefor we check where posts are NULL.

To be sure you won’t delete all your data, I strongly recommend to always backup your database and compare the row count from the above query with the total count of rows in your wp_postmeta table.

When you are sure, you can delete the rows with following query:

DELETE pm
FROM wp_postmeta AS pm
LEFT JOIN wp_posts AS p ON p.ID = pm.post_id
WHERE p.ID IS NULL

Happy orphan hunting!

Delete orphan postmeta rows in Wordpress 2
]]>
Fix cURL request being really slow locally with Valet https://johanhermansson.se/2020/08/fix-curl-request-being-really-slow-locally-with-valet Thu, 13 Aug 2020 13:41:57 +0000 https://johanhermansson.se/?p=574 I’m using Valet for running all my local WordPress development installations, which has worked perfectly until updating Valet to PHP 7.4 after the summer vacations.

Suddenly all my outgoing cURL requests (through wp_remote_post) was running really slow, always above 5 seconds which was really frustrating to work with.

I found out that cURL has problems locally to resolve DNS lookups on IPv6. cURL is easily configured to ignore the IPv6 resolver by setting the resolve option:

curl_setopt( $ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );

However – this is not as easy if wp_remote_post is doing the request. There are no arguments to set for cURL. I scratched my head for awhile when searching in WordPress’ source code, but then I found an action to fix the problem:

add_action( 'http_api_curl', 'force_ipv4_resolver' );
function force_ipv4_resolver( &$ch ) {
	curl_setopt( $ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
}

The cURL handler is passed by reference, so remember to use the & to reference it correctly.

]]>
How to Switch Option and Command Keys on a Varmilo Keyboard for Mac https://johanhermansson.se/2020/01/varmilo-keyboard-for-mac-with-switched-option-and-command-key Fri, 17 Jan 2020 08:43:53 +0000 https://johanhermansson.se/?p=570 Are you experiencing confusion with your Varmilo keyboard for Mac because the command and option keys are switched? Here’s a guide on how to solve it.

I recently bought a Varmilo keyboard for my MacBook, a VA109Mac to be specific, but I was a bit confused when it was delivered with command and option key switched.

I recently bought a VA109Mac Varmilo keyboard for my MacBook, but I was a bit confused when it arrived with the command and option keys switched.

After scratching my head for a few minutes, I started to search for solutions, and got informed that with Karabiner Elements you can switch the keys yourself. It worked as expected – problem solved!

However – many weeks later I accidentally switched the function key and option key, by holding them down for 3 seconds. Back to the head scratching again, but now with irritated outbursts because of my current workload.

The Varmilo keyboards are apparently (obviously) delivered with predefined setting macros that I did not know. In hindsight, I should have read the documentation more carefully when I started using the keyboard.

How to Switch Option and Command Keys on a Varmilo Keyboard for Mac 3

How to switch command and option key on a Varmilo keyboard without Karabiner Elements

Step one: Read the manual!

Apparently the Varmilo keyboards for Mac are delivered in ”Windows mode”. This means that the option key is the ”Windows key”, and the command key is the alt key.

To switch to ”Mac mode” you need to hold down the function (Fn) key, and the A key for 3 seconds, then the Capslock key will blink. If it does not blink, the keyboard is already in that mode.

To switch back to ”Windows mode” you instead hold down the function key and the W key for 3 seconds.

The problem is solved, without the need for any third-party applications such as Karabiner Elements. However – I would really recommend Karabiner Elements if you want to create your own macros, like put your Mac to sleep and such. It’s a really great application.

]]>
Advent of Code meetup at Learning Well https://johanhermansson.se/2019/12/advent-of-code-meetup-at-learning-well Tue, 17 Dec 2019 23:09:50 +0000 https://johanhermansson.se/?p=559 I went to the Advent of Code meetup at Learning Well’s offices in Nyköping.

The meetup was part of the Nyköping Developers meetup group, who meets biweekly to geek and talk developing topics. It’s a really great way to get away from the current projects for a few hours.

Advent of Code is a yearly Advent calendar event (and optional competition), where you every day get a new programming puzzle. It’s not only for people with a computer science background, but also for people with just some basic programming knowledge and problem solving skills.

You could brute force your way through the puzzles

The puzzles are not only “puzzles”, but they are written as stories, which also are part of a bigger main story. Respect to the creators for this, I can only guess how many hours they have put into the project.

Advent of Code meetup at Learning Well 4

You could brute force your way through the puzzles, and I heard someone really did early on by just testing directly in the answer fields. Now they have safe guards against it.

The meetup mission was to get together and solve some of the current puzzles. I had never participated in Advent of Code before, but I was really close last year.

Each day has a two part puzzle, which are also related (so far). The second part is likely to add another layer of complexity to the first puzzle.

Today I solved the first two days’ puzzles. It went pretty well, I think. I’m not really used to this type of problem — It’s not really what I do in my work.

What I really liked these puzzles is the programming nature of them. What I usually do in my work is “just” mostly rendering components and transforming collections into another maintainable structure, which then are rendered as components. It’s of course something I enjoy doing, but sometimes it’s fun to just solve problems.

My puzzle solutions are shared on GitHub, if someone is interested.

]]>
Website for Svalorna Indien Bangladesh https://johanhermansson.se/2018/12/website-for-svalorna-indien-bangladesh Tue, 25 Dec 2018 21:37:20 +0000 https://johanhermansson.se/?p=508 I have helped Svalorna Indien Bangladesh with a new website. The site is very modern with advanced content modules and it has an advanced donation functionality.

Svalorna will also be selling products on the site with WooCommerce. It’s not a complex solution, but works really well for what they need.

Donation solution

I found some donation plugins for WordPress and Woocommerce, but they all felt visually uninteresting and structurally more complex than they have to.

Instead I built my own donation solution on top of WordPress and WooCommerce.

Website for Svalorna Indien Bangladesh 5

With every donation an order is created in WooCommerce, to take advantage of the automatic receipts and the admin functionalities.

Automatically generated PDF:s

Most users wants the possibility to donate for others, for example as a present or as a memory gift at a funeral.

For this I built a solution that generates a PDF based on selected image and text input. The PDF is sent to the user automatically when it is generated, or to Svalorna if they need to print it and send it in a letter.

]]>
Get WordPress posts from all Polylang languages in your blog and post archives https://johanhermansson.se/2018/12/get-wordpress-posts-from-all-polylang-languages-in-your-blog-and-post-archives Thu, 06 Dec 2018 20:54:42 +0000 https://johanhermansson.se/?p=480 As usual – clients have different wishes and requests. Sometimes they’re reasonable, and sometimes they’re not.

This request was not unreasonable. In a country like Sweden, and especially in Stockholm, where many corporations and companies speak english – there are english-only speaking people.

Get Wordpress posts from all Polylang languages in your blog and post archives 6

Of course you want your Swedish speaking friends to not miss all of your content. They probably understand English – too ?. And others needs to see how an active blogger you are! 

Then you would like the possibility to show blog posts from all languages in your blog. A nice feature is also to show categories with posts from all languages.

I could not find any of these tricks while searching the web or fully described in the Polylang documentation. Maybe I searched with wrong phrases, but then again – someone else probably does too.

How to get posts from all languages in your blog

The lang parameter as described in the documentation needs to be set to empty string, but for some reason the tax_query gets set before the pre_get_posts filter. Nevermind. Just remove the language taxonomy from the query and you will be all set.

function awesome_theme_pre_get_posts( &$query ) {
	if ( ! function_exists( 'pll_the_languages' ) ) {
		return;
	}

	if ( $query->is_main_query() and ( is_home() or is_archive( 'post' ) ) ) {
		$query->set( 'lang', '' );

		if ( is_array( $query->get( 'tax_query' ) ) ) {
			$tax_query = $query->get( 'tax_query' );

			foreach ( $tax_query as $i => $row ) {
				if ( 'language' === $row['taxonomy'] ) {
					unset( $tax_query[ $i ] );
				}
			}

			$query->set( 'tax_query', $tax_query );
			$query = new WP_Query( $query->query_vars );
		}
	}
}

add_action( 'pre_get_posts', 'awesome_theme_pre_get_posts', 10 );

How to get categories with posts from all languages

This is much easier. Just set the lang argument to empty string ?

$categories = get_terms( [
	'taxonomy' => 'category',
	'lang'     => '',
] );

]]>
The Predator https://johanhermansson.se/2018/09/the-predator Sat, 15 Sep 2018 18:04:41 +0000 https://johanhermansson.se/?p=434 Shane Black är verkligen sjukt bra på att skriva knasiga karaktärer med fantastisk dialog. Glimten i ögat krävs i en film som The Predator, där temat är väldigt överdrivet, och det har de fått till.

De har siktat på att göra en en så kallad ”dum actionfilm” med 80-talskänsla, alltså inte något djupare än en underhållande film, och det har de verkligen lyckats med.

Tyvärr tappar filmen en del på att ha ett konstant högt tempo, och det är jättetydligt att de har haft problem i postproduktion. De försöker dölja det i klippningen.

Karaktärer med ett egentligt fortsatt syfte i sidohistorier kan oförklarligt försvinna och aldrig dyka upp igen.

Många sidohistorier som jag misstänker är filmade känns verkligen bortklippta, men ibland har det faktiskt fungerat bra att ta bort dem. Ofta känns det dock som att information saknas.

Sammanfattningsvis – Underhållningen är på topp, men jag hade verkligen gillat den ännu mer med fler praktiska effekter och lite lägre tempo. Alla knasiga karaktärer hade också kunnat få ta mer plats. 

]]>
Tre dagars vattenfasta https://johanhermansson.se/2018/09/tre-dagars-vattenfasta Wed, 12 Sep 2018 08:16:32 +0000 https://johanhermansson.se/?p=430 Visst har det varit jobbigt att vara hungrig, men jobbigast har nog varit avsaknaden av underhållningen att äta.

Min bror berättade att han vattenfastade och kände sig mycket bättre i kroppen efteråt, vilket gjorde mig nyfiken på att testa.

Dag 1

Första dagen kände jag framförallt av hungern och kurrande mage. Avsaknaden av att underhållas av mat, och att äta, var också som starkast första dagen.

Dag 2

Jag kände fortfarande av hungern, men den var inte alls lika tydlig. Mest kände jag av en slags spattig energi, nästan som för mycket koffein. Typ en energikick för att kunna leta mat ]]> Justice League https://johanhermansson.se/2017/11/justice-league Mon, 20 Nov 2017 15:32:40 +0000 https://johanhermansson.se/?p=387 Alla dessa superhjältefilmer har blivit tjatigare och tjatigare. Marvel har lyckats ganska bra, men DC har verkligen stora problem med sina filmer.

Tonen som Zack Snyder försökte sätta med Man of Steel, Batman vs Superman och nu med Justice League fungerat över huvudtaget inte. Det är alldeles för hårdrock-coolt och då tycker jag egentligen inte att det är något fel med hårdrock. Problemet är snarare att egentligen empatin är så svag för att alla är så coola att man inte bryr sig om någon.

Humorn är mest löjlig, och känns för det mesta pålagt. Det är som att man tror att one-liners alltid är roligt och måste vara med i varje actionsekvens.

Jag tycker nog att Ben Affleck passar bra som Batman, men Jason Momoas Aquaman är väldigt spretig. Gal Gadots Wonder Wiman är helt ok, men Tha Flash är nog ändå charmigast.

Musiken är såsig. Bilderna är smetiga förutom ett par som är väldigt fint komponerade. 

Jag har inte mycket bra att säga om Justice League. Lägg ner. Lägg ut. Gör om. Gör rätt. Det kan också vara så att ett cinematiska universum inte passar DC. 

]]>
Geostorm https://johanhermansson.se/2017/11/geostorm Mon, 20 Nov 2017 15:32:21 +0000 https://johanhermansson.se/?p=385 Att se Geostorm var lite som att se den riktiga uppföljaren till Independence Day. Katastroffilmer måste ha mycket charm för att vinna mitt hjärta, och den första Independence Day har det. Geostorm har inte det, men den har mer hjärta än Independence Day: Resurgance.

Filmen är väldigt bekväm. Det är nästan som om de anpassat den efter en mall från en manusbok. Jag tror att problemet framförallt ligger i att storyn är svag och ganska ointressant. 

Det bästa med filmen är verkligen inte Gerard Butler eller manuset som sagt. Det är specialeffekterna, vissa bilder är riktigt välgjorda och det var kul att se dem på bio.

Men i det stora hela är det tyvärr en film man (jag) glömmer direkt efteråt. Men det är klart det finns dem som gillar den här typen av filmer, och det är inget fel med det.

]]>