If you’ve poked around my site at all, it’s no secret: I’m a big fan of the Genesis Framework for WordPress. After getting a fresh install up and running, one of the first plugins I download is Genesis Simple Hooks, which is, as the name suggests, an easy way to tap into the Genesis Framework and modify the output with your own bits of code.
Now, some of what you could accomplish with Simple Hooks could also just be written into your functions.php file, but here’s why I prefer Simple Hooks: Miss one single quote or semi-colon in functions.php and you can kill your entire site. You’ll have to re-upload a fresh copy of functions.php via FTP to restore your site to its buttery goodness. Simple Hooks, on the other hand, is a little more forgiving. A syntax error in your code can result in an ugly error, but it won’t sink the ship.
Of course, there are instances when it’s better to use functions.php (like writing actual functions!), but that’s not the main focus of this post. So getting back to the topic at hand – What are some easy things you can do with Simple Hooks?
**Disclaimer** I’m no code pro. Everything I’ve learned about Genesis and WordPress has been the result of bootstrapping my way around the web and support forums. Below are examples that have worked for me, but I would welcome the input of more advanced developers to suggest more elegant ways of doing things!
3 Things you can do with Genesis Simple Hooks
Number 1: Install Google Analytics.
Of course there are plugins out there that do this for you, but it’s such a simple snippet of code, why use one more plugin when you can simplify?
From your WordPress Admin dashboard go to Genesis > Simple Hooks and use a quick Ctrl + F to search for the genesis_after hook, located in the Document Hooks section.
Anything you put in this section will execute on the page immediately before the closing </body> tag, which is where I like to stash my Google Analytics. Drop in the script provided from your Google Analytics account and click Save Changes. Voila! Tracking is installed throughout your site!
Number 2: Place an ad after the first post on your blog archive page.
Oh how I love conditional statements! WordPress is chalk full of built-in functional references that you can use to know “where” you are in the code. Am I on a page? Do this! Am I on a post? Do that!
You can use Simple Hooks to insert these little “where am I” checks into The Loop and modify your output accordingly. For instance, let’s say you want to show a single ad only after your first post on an archive page.
Get back into your Simple Hooks settings and do a search for genesis_after_post_content. It’s in the Post/Page Hooks section.
What we’re doing here is using a conditional statement to check two things:
- We’re on the first post in the loop ($wp_query->current_post == 0) and (&&)
- We’re on an archive page (is_archive)
If both those statements ring true, we want to show our ad, which I’ve wrapped in it’s on CSS class so we can style. Note that I’ve checked Execute PHP since I’m including PHP code. Here’s the code in the screenshot above in case you’d like to use it:
<?php
global $wp_query;
if( ($wp_query->current_post == 0) && is_archive() ) {
?>
<div>
// insert your ad code here
</div>
<?php
}
?>
Number 3: Use a Custom Field in the Post Info.
Let’s say you’re a travel blogger and like to include a custom field called “Location” in each of your posts. (psst…Here’s a quick “how-to” for creating custom fields)
In this example, we’ll go ahead and add a function to functions.php to let us grab the Location whenever we want. Here’s the code for that.
function getLocation($thePostID) {
$custom_fields = get_post_custom($thePostID);
if (array_key_exists("Location",$custom_fields)) {
$locale = $custom_fields['Location'][0];
}
else {
$locale = "Nowhere";
}
return $locale;
}
Next, let’s head over to our Genesis Simple Hooks settings and find the genesis_before_post_content hook in the Post/Page Hooks section.
Let’s unhook genesis_post_info() since we don’t want any of the default post info to print. Also, we’re only using our custom Location field on posts, not pages, so we’ll use a conditional statement to figure out where we are.
<?php if (!is_page()) { //Use a core WP function to Make sure it's NOT a page
$thePostID = get_the_ID(); // core WP function used within the Loop to grab a post ID
echo getLocation($thePostID) ." - ". the_date(); // pass the post ID to our getLocation function, add the date, and print!
}
?>
Lastly, don’t forget to check the Execute PHP box since we’re including PHP in our hook! Voila! Now our location and date will print on single post pages, between the title and the content.
Wrap-up
Alright, so there’s a few ways you can use Genesis Simple Hooks to customize your website. Don’t forget to check the plugin forum or the StudioPress forums and search for other ways you can use Simple Hooks. Have a question or want to suggest a better way to accomplish the above examples? Leave a comment!






Carrie,
Great article. There are some cool features in Genesis that can simplify your code. For example, when working with custom fields, you can skip the part where you get the post ID and such by using genesis_custom_field. So your code looks like:
<?php
if( ! is_page() ) {
$location = genesis_get_custom_field( ‘location’ ) ? genesis_get_custom_field( ‘location’ ) : ‘Nowhere’;
echo $location . do_shortcode( ‘ [post_date]‘ );
}
?>
[/php]
I’m showing the post date short code there, this has some extra options that can be added. You can read up on that here.
http://www.studiopress.com/tutorials/genesis/shortcode-reference#post-date-shortcode
Awesome – thanks, Nick!
Interesting. I have been using Simple Hooks for a while but with the latest Genesis I find it easier to simply insert the Analytics code in the footer Theme Settings page. The Simple Edits plugins fills out my other needs. Do you think Simple Hooks should be used instead of Simple Edits? Sometimes I think we have a boatload of plugins installed…..
Mromero, good suggestion on Google Analytics. Simple Edits is a great plugin, too, and if it does all you want, leave it at that – the interface is certainly easier!
However, if you needed the extended functionality of Simple Hooks, I’d ditch Simple Edits and just use the one plugin. That’s just my opinion – I don’t like too many plugins if I can help it.
Hi, Can you please let me know how to add a banner after 5 posts in the blog page, I have total 10 posts in the blog page.
Thanks
Frank
Hey Frank, the logic above should work ($wp_query->current_post == 0). Try changing the current_post == 5 instead of 0 and see if that does the trick.
Good use for hooks at no 3)
Hi Carrie,
Wow, love your posts/info. Very helpful.
Hey, I’m trying to do the same thing for post_date as you’ve shown for location– that is to have the date appear for only one category of posts.
I’ve unhooked genesis_post_info() from the genesis_post_content hook (because I don’t want 90% of it! And I’ve re-located the author to another location– for all posts).
I think I could use most of your code above, but I’m not quite sure how to “re-word” it for the post date. And would it make a difference if I put instruction for this in Simple Hooks, or (with code adjusted) do it in my functions.php? I feel like I’m setting myself up for a conflict by removing, then adding the same function on the same hook!
Hi Lorin,
Glad you found it helpful! You can use the [post_date] shortcode for just displaying the date.
< ?php if (is_category('your-cat-id-or-slug')) {
echo [post_date];
}
?>
I haven’t tested that code, but that’s the idea.
You can check the Conditional Tag Codex for categories to make sure your conditional statement is right for whatever category you’re calling out.
As to your question of Simple Hooks vs functions.php, this article might be helpful: http://www.studiopress.com/tutorials/customize-post-info
If you do it in Simple Hooks, just the conditional code and the print statement will work (as in the example above). If you decided to add it to your functions.php, you’ll need to wrap that code in a function (a la that StudioPress link).
Hi Carrie, Thanks so much for your quick response!
Unfortunately, WP didn’t like that — I guess– because I unhooked the post_info at the genesis_before_post_content hook and maybe Genesis now can’t grab the [post-date] because there’s no post_info from which to grab it??
Should I:
add the post_info first up at genesis_after_post_title hook where I want it, then follow with your code? Just not sure I know the syntax to do that in simple hooks.
??
Or should I scrap this and leave the post_info intact (in Simple Hooks) and just go to my functions.php and try a conditional filter:
/** Show only author on ALL posts except Diary category, in which case show author and date*/
add_filter (‘genesis_post_info’, ‘post_info_filter’);
function post_info_filter($post_info) {
if (is_category( ‘diary’) {
$post_info = ‘[post_author_posts_link][post_date] ‘;
} else if {
$post_info = ‘[post_author_posts_link]‘;
return $post_info;
}
}
(The above code returns an error in my Dreamweaver, but I can’t figure out why.) : (
Sorry. I’m just learning WP and Genesis!
Ah, okay. I missed your before content / after content bit in your first comment.
If you take the Simple Hooks route, you’ll want to Unhook genesis_post_info() from the genesis_before_post_content Hook. THEN you add in your conditional statement in the genesis_after_post_content Hook.
If you go the functions.php route, do the code above, but first you’ll need to unhook it:
/** Remove the post info function */
remove_action( ‘genesis_before_post_content’, ‘genesis_post_info’ );
/** and then hook your custom function in AFTER the post */
add_action (‘genesis_after_post_content’, ‘your_custom_function’);
function your_custom_function($post_info) {
if (is_category( ‘diary’) {
$post_info = ‘[post_author_posts_link][post_date]‘;
} else if {
$post_info = ‘[post_author_posts_link]‘;
}
return $post_info;
}
As for syntax, beware those quote marks when copy/pasting. I think your problem was a misplaced closing bracket though.
Thank you SO much. Yes, I found that missing closing bracket, but it still returned an error!
I found this somewhere and adapted it:
function post_info_filter($post_info) {
if (is_category(‘diary’)) {
$post_info = ‘[post_author_posts_link][post_date] ‘;
return $post_info;
}
elseif(!is_category(‘diary’)){
$post_info = ‘[post_author_posts_link]‘;
return $post_info;
}
else {
}
}
add_filter( ‘genesis_post_info’, ‘post_info_filter’ );
Seems it wants me to have that last line about the filter!
You’ve been so generous with your time (and blog comments space!).
To sum up, I think I mistakenly thought of post_info as having or being a short code when it is it is the parts of it that do. Secondly, if I UNhook the post_info from Simple Hooks default position, and want to REplace it at a position that occurs BEFORE that: genesis_after_post_title, is there a one-line action or function IN SIMPLE HOOKS that will do that trick? And does it matter if I’m placing it at a point BEFORE the default?
Thanks so much in advance. I’ll let you off the ‘hook’ after this!
; )
In my fourth full day of trying to solve this one problem,
Lorin
Okay Lorin, you’re funny and you get bonus points for that.
A couple of things. Take a look at the Genesis Visual Hook Guide to get a feel for where those hooks come into play.
As long as something happens within the loop (i.e. post_info) you can move it/access it anywhere within the loop. You just can’t access it OUTSIDE the loop. So you can safely put it after the post title or after the post content – it’s alllll in the loop.
As for the magic short codes (here’s a list), you can pop them right in to Simple Hooks! Just be sure you check the “Execute Shortcodes on this hook” option. You can let it fly solo, but in your case you’re needing the conditional PHP statement, so be sure you’re also checking the “Execute PHP” option.
Where would I add code for an ad if I wanted it to appear above the header? genesis_before Hook or genesis_before_header Hook? Here’s my site {it’s not live yet}:http://69.89.31.77/~momontim
Hi Trish,
Try genesis_before_header to hook in. This is an awesome plugin to use during development to see where the hooks actually fall within your design: http://wordpress.org/extend/plugins/genesis-visual-hook-guide/.
Your site’s looking good!
Carrie
Great info and tips in this post! I’m a big fan of this plugin. I was just trying to put some PHP into a hook and was worried that it would break my site if there were any syntax errors as is the case with custom functions. Good to hear that this is more forgiving.
Hey Andrew,
Thanks for stopping by. Glad it was helpful!
Carrie
Hi Carrie. How do I make a text-link ad appear ONLY on the homepage, and not on archived pages, etc? Please help
thanks
Hey Pepper,
You’ll want to use the is_home() or is_front_page() conditional instead of is_archive(). So, if you wanted to change the code above to show an ad on the home page after the first post, you’d do:
if( ($wp_query->current_post == 0) && is_home() )
Note: If is_home() doesn’t work, try is_front_page() instead. Here’s a good article on the difference between the two.
Cheers,
Carrie
Thanks for your reply, Carrie. But where exactly do I type in that code?
thanks
Are you using the Simple Hooks plugin? If so, insert it in the genesis_after_post_content code block shown in the example above) but with the modified code.
If you’re going to edit directly in your functions.php file (be careful if you do this – a typo can take your site down!), then use this bit of code:
function cd_ad_after_first_post() {
global $wp_query;
if( (0 == $wp_query->current_post) && is_home() )
echo ‘This is an ad!’ //Replace this with your ad code
}
add_action( ‘genesis_after_post_content’, ‘cd_ad_after_first_post’ );
Well, my feedback towards this article is really awesome… Thanks for sharing all these great uses of Genesis simple hooks plugin with us @Carrie. Check out my recent article because I’ve implemented some codes there in my blog using the same plugin.
http://softstribe.com/wordpress/how-to-add-social-sharing-buttons-below-post-info-in-genesis
What would be the best way if I want to place text before_post_content based on the custom post class?
A custom post class relates to using custom CSS on a post, so it doesn’t have any bearing on adding some text before your content. What are you wanting to do?