Webb – Johan Hermansson https://johanhermansson.se Web developer who says random things Wed, 08 Mar 2023 22:38:08 +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.

]]>
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 3

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 4

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 5

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'     => '',
] );

]]>
Permanentlänkar för egna kategorier i WordPress https://johanhermansson.se/2011/01/permanentlankar-for-egna-kategorier-i-wordpress https://johanhermansson.se/2011/01/permanentlankar-for-egna-kategorier-i-wordpress#comments Wed, 19 Jan 2011 21:55:16 +0000 https://johanhermansson.se/?p=167 För ett par dagar sedan skulle jag skapa en posttyp med egna kategorier och etiketter. Dock uppstod det problem när jag ville att taxonomins länkstruktur skulle vara barn till posttypen.

http://domän/posttyp/etikett/etikettnamn

Efter letande i dokumentation och googlesökande hittade jag ingen lösning. Men av en slump testade jag bara att vid registreringen av taxonomin sätta ett slash i slugparametern, vilket till min stora förvåning fungerade alldeles utmärkt.

register_taxonomy(
'posttyp_etikett',
array('posttyp'),
array(
'hierarchical' => false,
'show_ui' => true,
'query_var' => true,
'rewrite' => array(
'slug' => 'posttyp/etikett'
)
)
);

Det finns säkert någon annan vilsen själ där ute som förtvivlat letar efter samma sak.

]]>
https://johanhermansson.se/2011/01/permanentlankar-for-egna-kategorier-i-wordpress/feed 4
Omformatera egendefinerade datum i WordPress https://johanhermansson.se/2010/03/omformatera-egendefinerade-datum-i-wordpress Mon, 15 Mar 2010 15:43:46 +0000 https://johanhermansson.se/?p=73 Av olika anledningar ville jag spara datum i ett Eget fält eller Custom field, som det också heter. Men när jag skulle skriva ut datumet i mina mallar uppstod ett problem. Hur skulle jag formatera datumet enligt språket i min WordPressinstallation? Funktionerna the_time() och the_date() tillhör The Loop och visar bara inläggets eller sidans publiceringsdatum.

Efter många sökningar utan resultat hittade jag mysql2date(), en WordPressfunktion som just gör detta. Det är bara att skicka in datumet och hur man vill formatera det för att få ett vackert datum tillbaka.

Skriv ut dagens datum som ”Måndag 15 mars”:

echo mysql2date("l j F", date("Y-m-d"));

Det är bara att byta ut date(”Y-m-d”) till t ex get_meta(”qwerty”) för att omformatera ett datum från ett eget fält.

Jag skriver det här inlägget för att jag själv tyckte det var svårt att hitta informationen. Det största problemet kanske var att jag inte hittade rätt sökterm på Google. Förhoppningsvis kanske jag i alla fall hjälper en vilsen själ där ute.

Läs mer om mysql2date() på WordPress.org
http://codex.wordpress.org/Function_Reference/mysql2date

]]>
Välja vart widgets ska synas i WordPress https://johanhermansson.se/2010/01/valja-vart-widgets-ska-synas-i-wordpress https://johanhermansson.se/2010/01/valja-vart-widgets-ska-synas-i-wordpress#comments Fri, 08 Jan 2010 10:02:57 +0000 https://johanhermansson.se/?p=56 Jag fick ett uppdrag att bygga en hemsida åt ett företag med en WordPressinstallation. Det togs fram några olika mallar och några egna widgets, men ett problem uppstod. Kunden ville själva välja vilka sidor deras widgets skulle synas på. Jag började Googla och efter några fraser hittade jag pluginet Widget Logic.

Widget Logic gör så att du kan sätta upp villkor med WordPress egna Conditional Tags för dina widgets. Om du till exempel bara vill visa en widget på inläggsidor med kategorin ”Qwerty”, skriv följande villkor för den:

is_single() && in_category('Qwerty')

Möjligheterna blev i princip oändliga när jag upptäckte att man även kunde använda sig av get_meta() från pluginet More Fields.

More Fields är ett trevligt plugin som gör så att du kan skapa egna fält för dina posttyper. Standard posttyperna i WordPress är Inlägg och Sidor. Det går även att skapa egna posttyper med More Fields, men det är en annan historia.

Med More Fields skapade jag en ”box” med en checkbox för varje widget. Sedan valde jag att ”boxen” skulle finnas under posttypen Sidor. Detta innebär att om jag skapar eller redigerar en vanlig sida kommer det att finnas checkboxar för mina olika widgets. Därefter behövde jag även ställa in varje widget med villkoret get_meta(), innehållande tillhörande checkboxens slug:

get_meta('widget1_checkbox')

Ladda ner Widget Logic och More Fields enkelt från WordPress.org
http://wordpress.org/extend/plugins/widget-logic/
http://wordpress.org/extend/plugins/more-fields/

Läs mer om Widget Logic och More Fields
http://freakytrigger.co.uk/wordpress-setup/
http://labs.dagensskiva.com/plugins/more-fields/

]]>
https://johanhermansson.se/2010/01/valja-vart-widgets-ska-synas-i-wordpress/feed 5