Add Widget Before Post Content In Any WordPress Theme

Paste this PHP code at the end of your child themes functions.php file.

You’ll find a new widget which displays before the content on single posts and pages.

The code has been tested on the Twenty Eleven default theme for WordPress as well as the Genesis 2.0 Sample child theme.

Add Widget Before Single Posts & Pages In Genesis

Its preferable to use one of the code snippets on this page when using child themes built on the Genesis theme framework which are using the old XHTML markup and loop hooks.

Here’s an example of what works in themes running Genesis 2.0 with the new HTML 5 loop hooks & markup:

//* Register widget areas
genesis_register_sidebar( array(
	'id'          => 'before-content',
	'name'        => __( 'Before Content', '$text_domain' ),
	'description' => __( 'Before Content widget On Single Posts & Pages Only', '$text_domain' ),
) );


add_action( 'genesis_entry_content', 'add_widget_before_content', 5 );

function add_widget_before_content() {

	if( is_singular(array('post', 'page')) && is_active_sidebar('before-content') ) {
	
		genesis_widget_area( 'before-content', array(
			'before' => '<div class="before-content" class="widget-area">',
			'after'	 => '</div>',
		) );

}}

Style your widget using the .before-content class in your child themes style sheet.

Add New Widget Before Loop On Home Page Of Woo Theme

woo_main_before hook in Canvas theme

Paste this code in the custom code section of the Canvas themes functions.php file or your child themes functions file:

Need to change position?

Here’s a list of all Woo Hooks you can use in any theme.

Positioning Priority

Simply change the 3rd parameter in the action for positioning priority if needed.

Related Posts


Comments

16 responses to “Add Widget Before Post Content In Any WordPress Theme”

  1. I tried to do this, but when I did, it cleared out all of the content from my pages…any idea how to get it to work?

    1. Brad Dalton Avatar
      Brad Dalton

      Hello Sherry

      It depends on what theme you are using as all themes are coded differently.

      Try removing the 3rd parameter from the filter so the last line of code reads:
      [code]
      add_filter( ‘the_content’, ‘before_post_widget’ );
      [/code]

  2. Colin Crawford Avatar
    Colin Crawford

    Hi Brad
    I have had some help from Pippin and I think I have solved it…well it works anyway. You need to use the Output Buffer as it normally just echo’s out.

    Can show you the code but don’t know how to on here. I can tweet it to you and see what you think.

    Colin

      1. Colin Crawford Avatar
        Colin Crawford

        It is just a bit more added code to yours to make it work…simple when you know how

        1. Brad Dalton Avatar
          Brad Dalton

          Yes i do know how to code widgets. Just interested in how you would do it?

          1. Colin Crawford Avatar
            Colin Crawford

            Brad I’m not a WordPress programmer or a PHP programmer but I can read through someone elses code and get a general idea or understanding from what that code is suppose to do.

            I was a little bit lost why it was adding it to the beginning of the post and why it wasn’t adding it to the end.

            The extra code is simple if you know how and when to use it and in no way I’m I up to your standard.

            I’ve sent the code via Twitter, it’s no big deal and I’m not trying to undermine you in anyway.

            It was a challenge to me to try and solve it even though I needed help elsewhere.

            Colin

          2. Brad Dalton Avatar
            Brad Dalton

            Everything’s cool Colin.

            Stay positive as its all good and constructive.

  3. Colin Crawford Avatar
    Colin Crawford

    Thanks for the code Brad, I understand the first function to create the widget area but how does it add the widgets before the post, can we add after the post as well.

    Can’t seem to work the second function out.

    Cheers

    Colin

    1. Brad Dalton Avatar
      Brad Dalton

      The code is a working example of what you can use to add a widget before post content in any WordPress theme not just Genesis.

      If you’re wanting code for after post widget i have written a post about 5 ways to code after widgets in Genesis.

      Has your child theme been built on Genesis or another parent theme framework?

      1. Colin Crawford Avatar
        Colin Crawford

        Hi Brad
        Just trying to do the same but after the post and at the moment I’m using twentyeleven theme.

        I’ve added the widget area with no problems and have adjusted the before_post_widget function to after_post_widget. I have read that I need to append this code to the $content but with no luck.

        I just thought it would be useful after the Post content as well or on it’s own.

        Thanks

        Colin

        1. Brad Dalton Avatar
          Brad Dalton

          Are you blogging about WordPress Colin?

          1. Colin Crawford Avatar
            Colin Crawford

            I use WordPress to put websites together and want to learn how it works so I follow people like you to show me how it works and how to do it properly.

            I understand you have to create a child theme and not to touch any original theme files so adding extra functionality to the new functions.php is the way to go.

            I just feel having the widget after the post might also be useful as my customers will be able to alter the widgets there or any content. I know you can add extra content there but it has to be placed in functions.php and my customers won’t have a clue how to alter it.

            Widgets is the way to go, once set up.

            Colin

          2. Brad Dalton Avatar
            Brad Dalton

            I will write a post about how to add a widget after posts soon.

  4. This is a great way to add pretty much anything before your blog post. I think the best serves AdSense placements. I’ve seen some blogs put in there even related posts.

    1. Brad Dalton Avatar
      Brad Dalton

      Should work with any theme as well because it uses a WordPress hook.

Leave a Reply

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.