Free Tutorials Genesis Tutorials

Code Snippets For Adding Custom Functions

Use these code snippets to add custom functions and modify existing functions in your theme.

You can replace the genesis hooks in this code to your own theme specific hooks, change the hooks to another location in your theme or modify the code to create the function you need.

All custom functions and modifications to PHP code should be done in your child themes functions.php file.

Simple Custom Functions

Add text or HTML between the opening and closing div tags. You can use the class .add-text as your selector to style your text or HTML.

Use this code snippet to display text or HTML after all your titles. Use the .title-text class as your selector for styling your content in this area of your theme.

Adding Conditional Tags

If you used any of the above code snippets, you’ll also notice your custom content also displays on all your archive pages like your home, blog, category and author pages. To prevent this you can simply add a conditional tag after the function.

This code displays your content in your header on the home page only.

This code displays your content after your post titles on single posts only. Use the .single-title class as your selector to style the content in this location.

Now that you understand how custom functions work with theme specific hooks and conditional tags, you can take these coding snippets further.

Let’s take a look at some code for registering a widget and creating the function to hook the widget into a location your theme offers hooks.

Multiple Functions Using 1 Hook

This custom functions outputs immediately after the entry content and enables you to add:

  1. a call to action
  2. image gallery
  3. another call to action
add_action( 'genesis_entry_footer', 'your_function', 5 );
function your_function() {

if ( ! is_singular( 'post' ) )

$gallery = genesis_get_custom_field( 'gallery' );

printf( '<a href="%s">' . __( "Call Us For More Information INSERT PHONE NUMBER or Email Us" ) . '</a>',  __( '' ) );
echo '<p>' . do_shortcode( $gallery ) . '</p>';
printf( '<a href="%s">' . __( "Call Us For More Information INSERT PHONE NUMBER or Email Us" ) . '</a>',  __( '' ) );

Plugin Template Tag in Custom Function

If you need to hook in a function generated by a plugin, you can use the following example as a guide and change the template tag, conditional tag and hook:

add_action( 'genesis_entry_footer', 'your_function' );
function your_function() {
if( is_singular( 'post' ) ) {

Replace the above function call your_template_tag(); with the template tag your plugin uses.

Conditional Tags With Arrays

You might also want to display content on multiple pages using conditional tags in a custom function. To do this, you can use an array containing the page i.d’s or names of different pages.

This code will display the text in the Genesis before post position on pages using these 2 i.d’s. You can change the i.d’s to your own and add more i.d’s or page names separated by a comma if you want.

Note the additional bracket which is added when using arrays as compared to basic conditional tags.

Adding Widgets To Hook Locations

There’s 2 steps to creating widgets in WordPress.

  1. Register your widget
  2. Hook your widget into your themes hook locations

Using a theme framework which offers built in hooks makes it easy to display your widgets content in that specific location.

If you’re not using a theme which offers different hook locations, you’ll need to add the widget using a different method and modify your themes template files. This method can cause problems when updating your theme because the template files will be overwritten meaning your custom coding will be lost.

Let’s take a look at using custom functions to add widgets in WordPress themes which use hooks.

Add Widget After Single Posts

You can also use custom functions with your themes hooks to display content in any hook location like you header.

Display Header Image

You’ll find more code snippets which you can use to create custom functions on Github and Pastebin.

One reply on “Code Snippets For Adding Custom Functions”

Leave a Reply

Your email address will not be published. Required fields are marked *