Skip to content

Instantly share code, notes, and snippets.

View jdevalk's full-sized avatar
😀

Joost de Valk jdevalk

😀
View GitHub Profile
@jdevalk
jdevalk / README.md
Last active January 21, 2025 12:11
The Events Calendar SEO fix

The Events Calendar SEO fix

This plugin fixes the horrible SEO mess that The Events Calendar can create.

The Events Calendar has been an SEO problem for ages, and I've reported these issues to them numerous times over the years. They have made some fixes, but it's still not enough and especially on a site that has been running TEC for a long time, the fixes are not good enough, as the problems are hidden, but not fixed.

Problems:

  • If you disable "day" or "month" views in the settings (on by default), those don't get linked, but they still work...
  • If you enable the month view cache (on by default), months without events are only noindex-ed on the first view of that page, when they're cached they no longer get noindex-ed. This plugin solves that by disabling this cache.
<?php
class Joost_Comment_Fixes {
public function __construct() {
// Unhook comment cookies stuff from core.
remove_action( 'set_comment_cookies', 'wp_set_comment_cookies' );
add_action( 'wp_footer', [ $this, 'print_custom_comment_script' ] );
add_filter( 'wp_get_current_commenter', [ $this, 'ignore_comment_cookies_serverside' ] );
add_filter( 'comment_post_redirect', [ $this, 'comment_moderation_redirect' ], 10, 2 );
@jdevalk
jdevalk / fancy-version.php
Last active November 1, 2024 10:12
Replace og:image .avif to .jpg
<?php
/*
* Plugin Name: Replace og:image .avif to .jpg
* Description: Replaces og:image URLs ending in -jpg.avif with .jpg in the page output. Uses the new WP_HTML_Tag_Processor.
*/
add_action( 'template_redirect' , function() {
ob_start( 'joost_replace_og_avif_with_jpg' );
});

Scripts for getting the number of tags and posts on a site

If a WordPress site doesn't have XML sitemaps, or you can't find them, there are a number of ways to figure out how many tags and posts that site has. The easiest way is by using the REST API to get the number. Note both scripts below are fairly similar and could probably be simplified. Also: please don't run this on sites too aggressively. Not all sites handle getting 100 posts from their REST API endpoints very well.

@jdevalk
jdevalk / intro.md
Last active January 26, 2024 07:20

EDD Multi-currency currency by country switcher

The worker.js Cloudflare worker below determines the currency and sets a cookie. The PHP code reads the cookie and sets the session currency in EDD.

Playground Comment Worker

This simple playground.yml creates a comment that opens a WordPress Playground and installs the plugin from the current repository. The current iteration only works with plugins that do not require a build step.

Playground test comment in action

CORS issues

Because GitHub doesn't send the right CORS headers for zip files to be able to install them in a Playground, you need a reverse proxy to bypass the CORS restrictions. The Worker below in worker.js can be used as a Cloudflare worker that

@jdevalk
jdevalk / xmlrpc.php
Created June 12, 2023 11:14
If you're not using XMLRPC, this will save tons of uncached requests to your site, assuming your homepage is cached properly.
<?php
$protocol = 'https';
if ( ! isset( $_SERVER['HTTPS'] ) || $_SERVER['HTTPS'] === 'off' ) {
$protocol = 'http';
}
$url = $protocol . "://" . $_SERVER['HTTP_HOST'];
header( 'Location: ' . $url, true, 301 );
exit;
@jdevalk
jdevalk / copyrightholder.php
Last active January 6, 2022 04:00
This is how to add the copyrightHolder schema to the Website Schema piece Yoast SEO outputs.
<?php
add_filter( 'wpseo_schema_website', 'example_change_website_schema', 10, 2 );
/**
* Changes the Yoast SEO Website schema.
*
* @param array $data The Schema Website data.
* @param Meta_Tags_Context $context Context value object.
*
@jdevalk
jdevalk / register-graph-piece.php
Created January 22, 2020 12:39
This is the code used to generate the Event Schema for YoastCon
<?php
/**
* Adds Schema pieces to our output.
*
* @param array $pieces Graph pieces to output.
* @param \WPSEO_Schema_Context $context Object with context variables.
*
* @return array $pieces Graph pieces to output.
*/
@jdevalk
jdevalk / two-products-best.json
Last active May 7, 2019 01:39
Two JSON blobs, one with the main product, a second one that can be output later on the page that clearly relates itself to the first one as a related product.
<script type="application/ld+json">{
"@context": "https://schema.org",
"@graph": [
{
"@type": "Organization",
"@id": "https://example.com/#organization",
"name": "Yoast",
"url": "https://example.com/",
"sameAs": [
"https://www.facebook.com/yoast",