Add Custom PHP Functions

Applies to Version(s): Thesis 2

Attention! This page needs to be updated for Thesis 2.1.

Thesis 2 has improved on the “old” Thesis 1.x method of using custom_functions.php to add PHP snippets of code to your site.

In Thesis 2, you are able to create your own “hooks” — areas where your PHP can execute code — giving you control over precisely where a given action occurs.

For quick reference, the file path to the Thesis 2 custom.php file is:

“Site” → wp-content → thesis → skins → classiccustom.php

The above assumes by “Site” you are entering via your control panel or FTP at a level at or above /wp-content, and also that the active skin is “Classic” (as shown in bold). If another skin is active, then “classic” will instead be the name of your active skin.

Box Types and Creating a Unique Hook Name

In the Thesis Skin Editor, you can add Unique Hook Names to both existing boxes and to boxes you’ve added that are of these types: Container, Post Box, or Query Box.

These box types are shown in the context of their “Add Boxes” dropdown below:

Thesis Boxes than can be assigned Unique Hooks

Whether you’re working with an existing box, or one you’ve created, if the box type allows for the assignation of a Unique Hook Name, begin by clicking the “gear” icon (next to the name of the box you wish to edit) to pop-open a window.

Here, you can add your Unique Hook Name near the bottom:

Populating the Unique Hook Name field

As a best practice, make your Unique Hook Name short, lowercase, descriptive, unique — and use underscores instead of space characters: like_this.

Once you’ve added a Unique Hook Name to a Thesis 2.x Box, you can use this hook as an insertion point for code added to the active skin’s custom.php file.

Adding PHP Functions to Boxes with Unique Hook Names

Let’s take a simple goal and make it a function for a specific box in our active skin (for demonstration purposes, the Thesis “Classic” skin).

To keep the PHP function itself manageable, our goal will be to add “text to the bottom of all our posts” using a PHP function and a Unique Hook Name.

To add text to the bottom of posts, we visit the HTML section of the Thesis Skin Editor, and on our Single core template, we expand WP Loop and click the “gear” icon of the Single Post Box.

In the pop-up, add a Unique Hook Name, for this demo, add_paragraph — then close the pop-open panel, and (off-screen in the below) click the green “Save Template” button to finalize this change to our Single post template:

Add a Unique Hook Name to the Existing Single Post Box

Our Unique Hook Name is ready — now to find, edit, and save the PHP we want to add to the custom.php file for the active skin; again, that’s the Classic skin in this example.

Locate, Edit, Save, and Upload your Custom.php File

Your web host’s Control Panel or an FTP client such as Filezilla, combined with a local syntax editor — NotePad++ for Windows or TextWrangler for Mac OS are solid — makes editing the custom.php file simple.

With FTP connected to the hosting provider’s server, navigate along this path:

Your Connection → wp-content → thesis → skins → classic → custom.php, where this is the file to edit if the “Classic” skin is the active one, as it is for our demo.

Bring the custom.php file to your desktop, and open it with your choice of syntax editor.

For our example, we’re adding text to the bottom of our posts, so our function might look like this:

function my_last_paragraph() { ?>
<div class="my-post-ending">
	<p>Text at the bottom of all my posts.</p>
</div> 
	<?php
}
add_action('thesis_hook_post_box_add_paragraph_bottom', 'my_last_paragraph');

The Unique Hook Name of add_paragraph is included as part of a “larger” hook.

Note the “bottom” at the end of our function’s hook.

The positional options for each Unique Hook you create are these — to see these options without referring back to this tutorial, note that hovering your mouse on the [+] when adding a Unique Hook Name shows the same information below:

  • thesis_hook_before_container_{name}
  • thesis_hook_container_{name}_top
  • thesis_hook_container_{name}_bottom
  • thesis_hook_after_container_{name}

Where {name} is replaced with the Unique Hook Name you provided — which is how we arrived at thesis_hook_post_box_add_paragraph_bottom.

After adding our “demo” function, save custom.php, then upload that same file to the original location on your host server.

You’re done!

Unique Hook Names + Boxes = Awesome!

Clearly, an easy example on the function side — but this same process can be used to do far more than add a line of text to the bottoms of your posts.

With the ability to create an infinite number of Unique Hook Names, and the ability to access these hooks in custom.php, you can add and create any kind of function to make your site shine!