3 Great Uses for Genesis Simple Hooks Plugin

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.

Genesis Simple Hooks - Document Hooks

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.

Post Meta Hook - Genesis Simple Hooks

What we’re doing here is using a conditional statement to check two things:

  1. We’re on the first post in the loop ($wp_query->current_post == 0) and (&&)
  2. 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)

Custom FieldIn 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!

Comments

  1. says

    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

  2. mromero says

    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…..

    • Carrie Dils says

      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. :)

  3. Frank says

    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

    • Carrie Dils says

      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.

  4. Lorin says

    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!

    • Carrie Dils says

      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).

      • Lorin says

        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!

        • Carrie Dils says

          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. :)

          • Lorin says

            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

    • Carrie Dils says

      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.

  5. says

    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.

    • says

      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

        • says

          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’ );

    • says

      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?

  6. says

    Hi Carrie,

    absolutely great tutorial and I managed to use conditional tags in my genesis hook to place ads conditionally. Thanks.

    I just want to know if it is possible to center my ad when placing the code in hooks plugin? If yes then how?

    • says

      This is how I centered..changed the to :

      Updated, copy and paste…hope it helps!

      current_post == 0) && is_archive() ) {
      ?>

      // insert your ad code here

  7. says

    Aside the above example, you can decide to show for example Adsense code when only a post or page is viewed. like i did in my blog.

    //your adsense code here

  8. says

    I’ve used the code to place an ad under my first post on category pages…what would I need to do to have different ads on different category pages?

    thanks!

    Ray

  9. says

    Got any ideas on how I can use hooks to disable commenting for a certain category? I’m using a Genesis child theme that pulls portfolio items onto the portfolio page via a specific category in the blog. We’re going to utilize the portfolio page as a “meet the staff” page, so I’ve got the category “our staff” and would like to just disable the comments for just this particular category so that, when adding a new staff member, my client doesn’t have to remember to disable comments on those particular posts.

  10. Steve Wharton says

    Hey cool Carrie, you just came up third from the top when I googled “how to use genesis simple edits plugin” just now. And, thanks to you, I’m using the Simple Hooks plugin instead. It’s great! This is a very helpful post–especially the comments exchanges… Thank you. :-)

  11. says

    Carrie,
    Great stuffs you’ve got here with your Simple Hooks plugin, I’m using it in my new site.
    I was searching for the code snippet that could sort out my pain when i found you on the search results..
    I am working on a new site, using genesis + magazine ver2.1. With “Comments” enabled for both “Page” and “Post” and codes for email opt-in form(let’s name this newsletter) insert in “genesis_after_post_content Hook”

    I was going to add a new “page” using magazine’s “landing page” .

    I create the short sales copy within GetResponse’s webform builder(let call this bonus), paste the code to the new page’s “html” text box and hit publish. And the following happens:

    1) Title shows up: landing-bonus

    2) The “enter email here” box, “submit button” and “submit” text are all over the place

    3) The previous “opt-in” form (newsletter) below all “content” after post/page shows up.

    4) Comment box shows up

    My question, I will use “landing” in all future landing pages, but how to remove:

    a)title
    b)”newsletter” opt-in form
    c)”comment box”

    from showing up at the magazine child theme “landing page”?

    How to insert the webform code into magazine’s landing page so that the email box, submit button, submit button’s text shows up the right way it should be?

    Have a great day!

    • says

      Hey Joshua,
      Your questions are a little more in-depth than I can get into off the top of my head in a comment. :) But I think you might want to edit the landing page template directly to get your desired output since you’re going to re-use it with the same format.

      Cheers,
      Carrie

  12. says

    I am sorry but the code above didn’t work on my child theme News pro, paste exactly the code above to “genesis_after_content Hook” and allow Execute PHP but nothing happened.

    Could you please help ?

    Thanks