How to hook into the_title() with a filter

Sometimes you may need to “filter” the value of the_title(). This can be done with the_title filter. Below are a few examples on how it can be used:

The WordPress Way

<?php
function custom_title_filter($title) {
  // prepend "foo" to title string
  $title = "foo" . $title;

  // return updated title string
  return $title;
}
add_filter('the_title', 'custom_title_filter');

The OOP Way

<?php
class Custom_Filters {

  function __construct() {
    add_filter('the_title', [$this, 'custom_title_filter']);
  }

  function custom_title_filter($title) {
    // prepend "foo" to title string
    $title = "foo" . $title;

    // return updated title string
    return $title;
  }
}
new Custom_Filters();

The Anonymous Way

<?php
add_filter('the_title', function($title) {
  // prepend "foo" to title string
  $title = "foo" . $title;

  // return updated title string
  return $title;
});

All three of the above options do the same thing, there’ just implemented with different styles. If you are not sure which style to go with I recommend using option one: The WordPress Way. When you get into plugin development you will find many developers opt for the OOP Way.

WP Tips Bare Bones Starter Theme

A very bare bones WordPress theme. This is just a simple example of a small theme you could use to get started a little faster. Checkout the repo here.

.
├── README.md
├── functions.php
├── index.php
├── screenshot.png
├── style.css
└── template-parts
    ├── content-404.php
    └── content.php

1 directory, 7 files

This starter theme is super basic and is a great place to start when building a new WordPress theme. Have ideas for improvement? Let me know and open a PR.

Programmatically create an admin user in WordPress

Sometimes you may need to add a new user to the WordPress database via code. Below is a copy and paste snippet… just remember to swap out the username, password and email for your own.

// funcitons.php
/**
 * Create an admin user silently
 */
function my_add_user() {
    $username = 'username123';
    $password = 'pasword123';
    $email = 'drew@example.com';

    // Create the new user
    $user_id = wp_create_user( $username, $password, $email );

    // Get current user object
    $user = get_user_by( 'id', $user_id );

    // Remove role
    $user->remove_role( 'subscriber' );

    // Add role
    $user->add_role( 'administrator' );
}
add_action('init', 'my_add_user');

Hide WP Admin Bar on the Front-End

This is a simple WordPress filter that enables you to hide the wp-admin bar on the front end when logged in. Check a couple different ways you could do this below.

The WordPress Way

This is how you might see functions defined and called in many open source projects. Especially ones that have been around for awhile. Still it works and does the same thing as the styles to follow. A good rule of thumb is if you are not sure what style to go with… ALWAYS go with the WordPress style.

// define function that returns a truthy boolean value
function my_hide_admin_bar_func() {
    return true;
}
// hook into the hide_admin_bar filter and use our custom function as the callback
add_filter('hide_admin_bar', 'my_hide_admin_bar_func');

The Anonymous Way

// here we skip using a named callback in favor of an anonymous function.
add_filter('hide_admin_bar', function() {
    return true;
});

This options is nice as it cleans things up a bit. That said it comes with pros and cons so make sure you understand how filters and named functions work in WordPress (assuming you do otherwise you have some more homework).

The Simple Way

A super clean way is to use one of WordPress’s built in “helper” functions. There are a few and the one we are looker for is: __return_true()

Below you can see how we can plug that in as the callback like we did in the WordPress way.

add_filter('hide_admin_bar', '__return_true');

The PHP 7.4 Way

add_filter('hide_admin_bar', fn() => true);

Over the last year I have really jumped into Javascript development and Arrow functions have been amazing! When we got there is PHP I was like:

So thats it. Just a couple different ways to do the same thing.