How to replace text in WordPress with hooks

Recently a customer asked me to replace the text “Law Office” for “Law Office & McGill” on his WordPress website. The problem was that this website had around 77 pages and 55 blog posts and probably 80% of the content had the text to replace.

The way I solved this problem was by using the WordPress add_filter(); hook and the PHP str_replace(); function which made my life easier, and my customer happy. This approach is also SEO friendly, which is a win-win situation.

The code in functions.php

function replace_text($my_text){
  $my_text = str_replace('Law Office', 'Law Office & McGill', $my_text);
  return $my_text;
}

add_filter('the_content', 'replace_text');

In this case I used ‘the_content’ as the hook but there’s a whole list of options to use.

MongoDB.live 2021

  • Date: 13 – 14 July
  • Location: Online
  • Cost: Free
  • Register: Link

MongoDB.live is the flagship conference for the data platform built by — and for — developers. This two-day event provides a deep dive into the future of data, and features interactive workshops, explorations of the latest data trends and products, Ask Me Anything panels with the top minds in data, and more.


WordCamp Europe 2021

  • Date: 7 – 9 June
  • Location: Online
  • Cost: Free
  • URL: Link

“WordCamp Europe is an informal, community-organised gathering of WordPress enthusiasts – from casual users to Core developers – where we discuss all things WordPress, share ideas and get to know each other.”


This event was a great opportunity to catch up with modern practices in the WordPress community. 100% online but still accurate and useful.

One of my favorite topics is WP-CLI, how to manage WordPress from the command-line. “WP-CLI is the official command-line interface for WordPress. It provides a collection of commands that can be used to perform various WordPress actions such as plugin & theme installations, core updates, configurations or backups without using a browser.”

Another useful talk was Headless WordPress, Gatsby or NextJS, a subject I find attractive and with an interesting future. Just to mention a few details, this portfolio is built using Gatsby and the blog posts are fetched from Headless WordPress which makes my life easier in many ways. First, my website is super fast with a Lighthouse score of 96, and second, writing blog posts in WordPress is as easy as it sounds. It’s a win win situation.

One of the key topics that was discussed was “How contributors make WordPress”. WordPress.org itself has tons of resources in case you want to be involved, which by the way has many options like working with the Core team, the Design team, you can help with translations in your own language/country, Themes, Plugins, Trainings, etc. I believe contributing makes you a better programmer overall because you get to understand how this platform works and it gives you a different perspective whenever thinking on coding.

Thanks to all the sponsors for making this kind of great event possible and for free.

Let’s keep on learning.

How to verify if blog post don’t have any categories in WordPress

In my current work I have to solve many problems for customers with WordPress websites. It’s common to see this scenario:

  • Website running a child theme based on Twenty something WP Theme
  • A thousand plugins installed, half active, half inactive
  • Patches all over functions.php
  • Custom plugins
  • A mess in the way they created .php files

And then they want to implement something which might not be that hard, but it takes 5 hours to find out where is this function calling, which hook is doing what, which file has the content of this nonsense piece of code, etc.

Today I had to face this situation: the customer has a website to display products, most of them are linked to a category, but some of them don’t, and they want to display a generic category under those products.

In order to fix this, I had to implement an if statement to check whether the post has a category or not, in case it has, leave the old code running to display the proper category, and if not, display a custom message with a link to the generic category. Bla bla.

This is the code that allowed me to do that:

<?php

$terms = get_the_term_list( $post->ID, 'categories', '<li class="item">', ' . ', '</li>' );

echo $terms;

if(empty($terms)){
echo '<li class="collection_item"><a href="#">Generic Title</a></li>';
}

?>

I just had to use the get_the_term_list(); WP function explained here.

Freelance developer in Mexico

I live in a small town in the south of Mexico called San Cristobal de las Casas, Chiapas. Internet connection is not that good, but good enough to work as a programmer.

I prefer to work from home, I don’t attracted to the coworking environment. Maybe the goal of this places is to flirt while drinking lactose free, gluten free, sugar free, decaf machiato and to take selfies for Instagram. I don’t mind people doing that, but it’s not my thing.

Modern times allow us to work from almost anywhere, which is good. And Mexico being a place with almost no restrictions because of the virus, then why not travel around.

I’ve meet interesting people on the road, some of them became customers and friends. It’s such a nice feeling moving around with light luggage, you realize you really don’t need that much in order to feel good and being able to work and survive.

Living in a comfortable place close to downtown with an active job and my guitar by my side makes life a nice experience. For now I’ve been working as a freelance developer in Mexico, but planing to start searching for 100% remote jobs abroad.

CMS WordPress

The blog posts of this page are fetched via WordPress and GraphQL.

There are three options as to where to write and manage blog posts when building a Gatsby website:

  1. Markdown.md, which for now is my second favorite.
  2. Contenful, not my favorite. Interface is nice and easy to use, but maybe I am more used to the WordPress experience.
  3. WordPress as a headless CMS. Currently working with this version. My favorite.

Something I don’t like about Contenful is that links cannot use target=”_blank” and that is a must, in part to help SEO and it’s also a personal taste. I don’t like when you click on a link and it opens on the same tab.

Let’s try with WordPress.


This personal portfolio is alive thanks to Gatsby, Contentful, Netlify and Andrew Mead wonderful tutorial.

FizzBuzz (Solution)

A couple of weeks ago I was reading a great book called Eloquent Javascript which is a great source of knowledge. One of the first exercises is called FizzBuzz and the goal is to:

  • Write a program that prints each number from 1 to 100 on a new line.
  • For each multiple of 3, print “Fizz” instead of the number.
  • For each multiple of 5, print “Buzz” instead of the number.
  • For numbers which are multiples of both 3 and 5, print “FizzBuzz” instead of the number.

At first I felt overwhelmed, I didn’t even try. I just kept reading the book and doing some local testing with loops and so. I read somewhere that this was a job interview question, which at the time seemed a bit too much.

But suddenly, after hours of digging into programming concepts and literature, one day it just came as if it was the most logical thing to think. What if…

My solution

Using: If else

'use strict';

for (let i = 1; i <= 100; i++) {
  if (i % 3 == 0 && i % 5 == 0) {
    console.log('FizzBuzz');
  } else if (i % 3 == 0) {
    console.log('Fizz');
  } else if (i % 5 == 0) {
    console.log('Buzz');
  } else {
    console.log(i);
  }
}