How to Hide Spoilers in Your Reviews (Blogger & WordPress!)

This week’s Bitchin’ Book Blog question is:

Hey Ashley,
I noticed that you have the ability to hide your spoilers in your reviews, I’m guessing by using a code. [..] I’m wondering if you can post a tutorial on how to go about doing this on blogger.
Thanks in an advance, you’ve been so helpful!

No problem!! I will show you guys how to implement this, but use it at your own risk. Spoilers are made possible by using jQuery/JavaScript, but JavaScript is deactivated in e-mails and RSS readers, which means the spoilers will be rendered out in plain text. But if you are using WordPress, the Ultimate Book Blogger Plugin does have spoiler functionality already built-in, and those spoilers are completely removed in RSS feeds/e-mails. 🙂 Unfortunately it’s not possible to do that in Blogger, but you could always manually make the text white or something so that it becomes “invisible” in e-mails/RSS readers (which usually have white backgrounds).

With that in mind, let’s get started!

Here’s an example of what the output will look like:

This is some normal text, but omg Check this out, it’s a SPOILER Hehe.

WordPress

NOTE: This method requires editing your theme files, which means if you have a pre-made theme and have to update it, your changes could be overwritten.

Go to your blog admin panel and navigate to Appearance » Editor. On the right-hand side, look for the file functions.php (or Theme Functions) and click on it. After the <?php tag, paste this bit of code:

// START EDITING jQUERY
/* If you do not have jQuery already on your blog, uncomment this area
$url = 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'; // the URL to check against  
$test_url = @fopen($url,'r'); // test parameters  
if( $test_url !== false ) { // test if the URL exists  

    function load_external_jQuery() { // load external file  
        wp_deregister_script( 'jquery' ); // deregisters the default WordPress jQuery  
        wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'); // register the external file  
        wp_enqueue_script('jquery'); // enqueue the external file  
    }  

    add_action('wp_enqueue_scripts', 'load_external_jQuery'); // initiate the function  
}
*/
// END EDITING jQUERY

// jQuery Spoiler Function
function add_spoilers() {
	echo '
		<script type="text/javascript">
		$(document).ready(function() { 
			
			$("span.spoiler").hide();
			
			 $(\'<a class="reveal">View Spoiler &raquo;</a> \').insertBefore(\'.spoiler\');

			$("a.reveal").click(function(){
				$(this).parents("p").children("span.spoiler").fadeIn(100);
				$(this).parents("p").children("a.reveal").fadeOut(100);
			});

		}); 
		</script>

		<style>
			/* If you want you can add custom CSS here for the spoilers!  Below is what I use on my blog. */
			a.reveal:hover { cursor:pointer; }
			span.spoiler { background-color:#FCE586; }
		</style>
	';
}
add_action('wp_head', 'add_spoilers');

// Spoiler Shortcode
function spoiler_shortcode( $atts, $content = null ) {
	
	$output = '<span class="spoiler">';
	$output .= do_shortcode($content);
	$output .= '</span>';
	
	return $output;
}

add_shortcode('spoiler', 'spoiler_shortcode');

At the top, I have included a way to add jQuery to your blog. By default, it is commented out, because there’s a good chance you might already have jQuery on your blog! But if you don’t, then un-comment the jQuery area.

Basically what we’re doing here is dding our own custom bit of jQuery script, which is what creates the spoilers! I also added in a spot where you can add some custom CSS if you want to style the spoilers output. I included the CSS I use on my blog, but of course you can change it. We then take it a step further, and register the spoiler syntax as a shortcode, so that you don’t have to use HTML.

Now, to test this out in a post, create a new post and type in something like this:

This is your normal text here [spoiler]but the text inside these tags is a spoiler![/spoiler]. And then you can keep writing over here.

Blogger

Go to your blog admin panel, click on Layout, and Add a Gadget. Select “HTML/JavaScript”, and this is where we will paste our code. Leave the title blank! Then, paste this code inside the content box:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function() { 
	
	$("span.spoiler").hide();
	
	 $('<a class="reveal">View Spoiler &raquo;</a> ').insertBefore('.spoiler');

	$("a.reveal").click(function(){
		$(this).parents("p").children("span.spoiler").fadeIn(100);
		$(this).parents("p").children("a.reveal").fadeOut(100);
	});

}); 
</script>

Then, to add this to a post, go create a new one. Make sure you’re on “HTML” instead of the visual editor, and use this syntax:

This is your normal text here <span class="spoiler">but the text inside these tags is a spoiler!</span>.  And then you can keep writing over here.

I have found that sometimes this spoiler method gets a bit wonky if you have any kind of HTML in the middle of it. But for most quick snippets, it works wonders! But next week I’ll look at an alternative method, which is perfect for large chunks of spoiler text (and putting things like images inside spoiler tags). We’ll be using jQuery’s toggle, which is what I used to create those click-able/toggle-able “WordPress” and “Blogger” buttons!

Photo of Ashley
I'm a 30-something California girl living in England (I fell in love with a Brit!). My three great passions are: books, coding, and fitness. more »

Don't miss my next post!

Sign up to get my blog posts sent directly to your inbox (plus exclusive store discounts!).

You might like these

26 comments

      1. What text did you put inside? Because as I said, sometimes it gets wonky depending on what you put inside, which is why I have the seconds method.

    1. Definitely give the spoiler thing a try! 🙂 There’s a shortcode enabled and you should be able to do:

      [spoiler]spoiler here[/spoiler]

  1. Hey Ashley, I know this is quite late, but maybe you could help me? I tried doing everything you said (Blogger), but it doesn’t hide anything behind a spoiler tag – its just shows me the text regularly. Do you have any idea why it might be so and how can I fix it?
    Thanks in advance and sorry for the trouble!

    Nitzan Schwarz recently posted: Review: Blood Promise by Richelle Mead
        1. Okay I see the code on your site now. Can you link me to a post where you’re actually using the spoiler?

          It could be because you have a funky JavaScript error on your blog (and if you have one error, that can kill all other JS). But I need to see where you’re using it in order to confirm that.

          1. here: http://drugscalledbooks.blogspot.co.il/2012/09/review-for-need-by-carrie-jones_14.html
            (I didn’t actually hide anything, cause the spoiler isn’t working, so I just wrote “trying” in the spoiler tag. It’s right after the “The (many) Parts of the Story that Bothered me” part.)

            That would kind of suck cause I’m not the one who touched those (the designer did) and I won’t know how to find/fix those problems xD And I was really happy to find such an easy spoiler tag (I really need those), but now it seems I won’t be able to use it after all :/

            I really want to thank you, Ashley, for caring and helping me even though this must take from your time and it’s probably quite bothersome. So, thanks! 🙂

            Nitzan Schwarz recently posted: Who Wore it Better: Graceling by Kristin Cashore
            1. Yeah I think the problem is because of this error on your site: http://i.imgur.com/Uk6LxoU.png That’s a JavaScript error. Unfortunately I can’t give any guidance on how to fix it. 🙁 But as I mentioned before, one single JS error can cause all other JS that follows to stop working. I believe that’s what’s happening here!

              1. Thanks for checking it out, anyways, Ashley! You’re the absolute best! 🙂
                I’ll have to make do with my current way of hiding spoilers, as I won’t know to fix the problem (nor do I think it’ll be wise for me to mess around that… I might accidentally destroy the world…)
                One day, I’ll both move my blog to WordPress AND get a new design (hopefully from you :P) and then everything shall be fixed! LOL

                Again, thank you so much!!

                Nitzan Schwarz recently posted: Review: Split Second by Kasie West
  2. This was great! Thanks. However, when I view my website on the mobile side, it doesn’t work. :-/ Any tips or advice? I’m using Blogger.

    1. Sorry, I was testing trying to hide content in the comments. You see, I want to comment on a WordPress blog but I’m not sure if the owner has it enabled for spoiler tags. So is there anything we commenters can use when we comment? Any HTML code we can put out there?

  3. Hi Ashley! This is the most perfectly timed post I have ever found – I have spent hours and hours pointlessly researching WordPress plugins and trying to find code to do in-line spoilers for my book reviews (everyone only seems to do separate paragraph spoilers or horribly formatted buttons). You solved all my problems!

    I did have to tweak one thing as my text was not hiding behind View Spoiler, it was just reformatting as if it had already been shown. I’m not sure if WordPress released an update since you wrote this, but everywhere you had a “$”, I had to replace with “jQuery” (capitalized like that) in the // jQuery Spoiler Function (not the // Spoiler Shortcode section). I found that solution on this website: http://www.pauljoyceuk.com/codex/2011/jquery-is-not-a-function/#comment-362579.

    Not sure if this might help anyone else! But thank you so so so much for publishing this code.

Recent Posts

    Random Posts