Skip to content

Instantly share code, notes, and snippets.

View sybrew's full-sized avatar

Sybre Waaijer sybrew

View GitHub Profile
@sybrew
sybrew / get-primary-term-tsf.php
Created March 20, 2024 22:41
Gets primary term name automatically from any post type for The SEO Framework WordPress plugin.
<?php
// Don't include the PHP opening tag if PHP is already open.
function get_primary_term_name( $post_id ) {
$taxonomies = array_keys( array_filter(
tsf()->taxonomy()->get_hierarchical( 'objects', get_post_type( $post_id ) ),
'is_taxonomy_viewable',
) );
$taxonomy = reset( $taxonomies );
@sybrew
sybrew / remove-tsf-shop-base-breadcrumbs.php
Last active March 20, 2024 21:20
Removes WooCommerce's shop base from The SEO Framework's breadcrumbs.
<?php
// Don't include the PHP opening tag if PHP is already open.
add_filter(
'the_seo_framework_breadcrumb_list',
function ( $list, $args ) {
if ( isset( $args ) ) {
if ( 'single' === \The_SEO_Framework\get_query_type_from_args( $args ) ) {
$is_product = tsf()->query()->is_product( $args['id'] );
@sybrew
sybrew / tsf-robots-if-term.php
Last active March 20, 2024 20:38
Sets noindex if post has term from CPT taxonomy 'partner'.
<?php
// Do not include the PHP opening tag if PHP is already open.
// For: https://wordpress.org/support/topic/no-index-custom-post-type-specific-taxonomy/
// TSF 5.0+
add_filter(
'the_seo_framework_robots_meta_array',
function ( $meta, $args ) {
@sybrew
sybrew / pods-append-tsf-gen-title.php
Created February 21, 2024 17:23
Append brand name from custom pods field to TSF generated title.php
<?php
add_filter(
'the_seo_framework_title_from_generation',
function ( $title, $args ) {
// If Pods is deactivated, skip further processing.
if ( ! function_exists( 'pods_field' ) ) return $title;
if ( null === $args ) {
@sybrew
sybrew / tsf-acf-custom-image-generator.php
Last active February 21, 2024 00:56
Example snippet to prepend image arrays from ACF generators for The SEO Framework.
<?php
// Do not include this PHP opening tag if PHP is already opened...
// Ref: https://theseoframework.com/docs/using-filters/
// Ref: https://wordpress.org/support/topic/featured-image-as-ogimage/
add_filter( 'the_seo_framework_image_generation_params', 'my_tsf_custom_image_generation_args', 10, 2 );
/**
* Adjusts image generation parameters.
*
@sybrew
sybrew / acf-tsf-term-description.php
Last active February 17, 2024 15:19
Gets a custom term field from ACF to override TSF's generated description.
<?php
// Don't include the PHP opening tag if PHP is already open.
add_filter( 'the_seo_framework_generated_description', 'my_tsf_generated_description', 10, 2 );
/**
* @param string $desc The generated description.
* @param array|null $args The query arguments. Contains 'id', 'tax', 'pta', and 'uid'.
* Is null when the query is auto-determined.
* @return string The overwritten description.
@sybrew
sybrew / tsf-meta-render-data.php
Last active November 22, 2023 17:50
TSF meta render data filter
<?php
// This works on TSF v5.0 and later.
add_filter(
'the_seo_framework_meta_render_data',
function ( $tags_render_data ) {
// Remove og:description:
unset( $tags_render_data['og:description'] );
@sybrew
sybrew / wp-remove-fbclid.php
Last active August 29, 2023 08:12
Removes the fbclid query parameter from the URL when someone visits your site.
<?php
// Don't include the PHP opening tag if PHP is already open in the file.
add_action(
'parse_request',
function() {
if ( isset( $_GET['fbclid'] ) ) {
wp_safe_redirect( home_url( remove_query_arg( 'fbclid' ) ), 301 );
@sybrew
sybrew / test-php-timer.js
Last active September 4, 2024 20:52
TSF's performance testing script.
// Run this script in the browser console, wait for it to complete and show you the testing results.
// You must use the following plugin to load the timings... https://gist.github.com/sybrew/f72f58394e62447d9bad61fdcc0bcb1b
// Config the constants below
const doLoggedIn = false; // whether testing logged in or out.
// NOTE: You must manually log in and out in the browser you're testing this!
const iterations = 100; // Set iterations. Fewer than 100/50 with/without Opcache is inaccurate.
const tick = false; // Whether to display every timestamp recorded on tick. Noisy.
@sybrew
sybrew / htmltimerdump.php
Created August 17, 2023 00:00
Dumps HTML timer in meta id=htmltimerdump as late as possible for you to snatch via JS.
<?php
/**
* Plugin Name: HTML Timer Dump
* Description: Dumps HTML timer in meta id=htmltimerdump as late as possible for you to snatch via JS.
* Author: Sybre Waaijer
* Author URI: https://cyberwire.nl/
* Version: 1.0.0
* License: GLPv3
*
* @package HTMLTimerDump