Project

General

Profile

How to use the Cookbook snippets

The snippets found in the Cookbook are PHP source code. You can use this code in the following ways.

Theme's functions.php

The snippets can be added in the currently activated theme's functions.php file.

This is the fastest and easiest way to use the snippets, but there are some shortcomings:

  • Every time the theme is updated, the functions.php file is updated as well, which means all your customizations are lost. So, if you use functions.php for your customizations, make sure to always have a backup of them.
  • Each theme uses its own functions.php file, so every time you activate a different theme, make sure to also move the customizations to the functions.php file of the currently activated theme.

If you frequently change your theme, you should definitely consider the alternative option below.

Custom Plugin

In order to avoid the shortcomings involved with storing the snippets in the functions.php file, you can create a custom plugin for your customizations and add the snippets in there.

Creating a plugin for WordPress is really easy. Add the following in a file at /wp-content/plugins/wordpress-customs.php:

<?php
/*
Plugin Name:  WordPress Customs
Description:  Custom functionality for WordPress plugins and themes.
Version:      1.0.0
Author:       John Smith
Author URI:   http://www.example.com
License:      GNU General Public License v2 or later
License URI:  http://www.gnu.org/licenses/gpl-2.0.html
*/

// Prevent direct access to this file.
if ( ! defined( 'ABSPATH' ) ) {
    header( 'HTTP/1.0 403 Forbidden' );
    echo 'This file should not be accessed directly!';
    exit; // Exit if accessed directly
}

// Add your code below

Add your customizations in this file, upload it at /wp-content/plugins/ and activate the plugin through the WordPress administration interface.