Meet Speed Bumps, our newest open source release

It’s the summer of open source at Fusion. Maybe you saw Publishing Checklist, which we released in June, or Shortcake Bakery (our Shortcake add-on), which we released just last week. Today we’re putting another major plugin into the fray: Speed Bumps, a tool to intelligently insert speed bumps into site content.

A common business request in publishing is the automated insertion of “speed bumps” within a chunk of content based on defined business logic. A speed bump might be an ad unit, a recirc module, or a Rick Astley video, and the business logic might be “only on posts longer than 20 paragraphs”. Speed Bumps makes problems like this easy to solve with a few lines of WordPress configuration code.

Here’s an example of how it works. A simple, stupid example.

Your publisher bounces into the room with an awful idea – “Let’s Rick Roll our audience!”

No problem though. A few lines of code and voila; you have inserted a beautiful musical video throughout your content.

<?php
register_speed_bump( 'rickroll', array(
'string_to_inject' => function() { return '<iframe width="420" height="315" src="https://www.youtube.com/embed/dQw4w9WgXcQ" frameborder="0" allowfullscreen></iframe>'; },
'minimum_content_length' => false,
'from_start' => false,
'from_end' => false,
));
add_filter( 'the_content', 'insert_speed_bumps', 1 );

But, maybe that’s a little too extreme. You’ve reasoned with the publisher and she’s agreed to only show the video in certain situations, namely, only when the previous paragraph contains the phrase ‘give {something} up’. Here’s how you would achieve that:

<?php
add_filter( 'speed_bumps_rickroll_constraints', 'give_you_up', 10, 4 );
function give_you_up( $can_insert, $context, $args, $already_inserted ) {
if ( ! preg_match( '/give [^ ]+ up/i', $context['prev_paragraph'] ) ) {
$can_insert = false;
}
return $can_insert;
}

A few hours later and you can’t stand it. You need to show Rick the door, but the publisher hasn’t budged (she also swears by pop-up ads and autoplay, maybe you need a new job). It’s hacky, but you decide to improve your personal user experience with another simple filter:

<?php
if ( current_user_can( 'manage_options' ) ) {
add_filter( 'speed_bumps_rickroll_constraints', '__return_false' );
}

What’s next?

We encourage contributions to Speed Bumps – we think it solves a major pain point for publishers. Dig into the repository and find an open issue to hack on, or you can reach out to us in the News Nerdery for an informal chat.

WHERE TO WATCH