Jetpack: move Jetpack Ads further down in a post

You’ve probably heard about Jetpack’s new Ads module: it allows you to insert ads in your posts, in your sidebar, and in the header of your WordPress site without having to worry about finding the right ad network, managing ad inventory, or worrying about the quality of the ads. It’s available to Jetpack Premium and Professional users.

To use the feature, all you have to do is to click on the toggle under the Jetpack menu in your dashboard. Jetpack takes care of the rest.

However, sometimes you may not be happy with the default placement of the in-post ads: they appear right below the post content. That’s great, but sometimes you may be using other plugins to insert elements in there, like sharing buttons, related posts, subscription forms, …

The code snippet below will allow you to move the ad box exactly where you want it to appear.

Continue reading

Jetpack: control what images are served by Photon

Jetpack flying through the sky (or maybe it's SpaceX)

Jetpack’s Photon module is a free image CDN that doesn’t require any configuration; as soon you activate it, all the images in your posts and pages will be downloaded, optimized, cached, and served from WordPress.com’s CDN. Magic!

Photon is applied to all images on your site, including images that may hosted somewhere else; if you one day added an image to one of your posts without actually uploading it to your Media Library, that image will be served through Photon too.

However, sometimes you’d rather not serve external images through Photon. After all, these images may already be served via another CDN! Luckily, Jetpack includes a filter to allow you to control which images get served by Photon. In the example below, we’ll only use Photon for images belonging to our site, and make sure none of the images that are not hosted on our site get processed by Photon.

Continue reading

Jetpack and Open Graph Meta Tags: change a post’s Media Type to Video

Customize the way your post previews look like on Facebook

When sharing posts on Facebook, you may have realized that sometimes you can play a video right on Facebook, without having to click and go to another site.

Videos on Face

This is possible thanks to Open Graph Meta Tags. When you share a post on Facebook, or when Jetpack Publicize publishes a post to your Facebook page, Facebook crawls the page and looks for Open Graph meta tags in the head to build a complete post preview. That post preview will often include an image, a title, a description, and sometimes a video like on the screenshot above.

Jetpack automatically creates these Open Graph Meta tags for you unless you already use another Open Graph plugin, in which case we let the other plugin handle things.

Let’s learn how to customize the type of tags that Jetpack can add to your posts.

Jetpack: Add Related Posts to your RSS feed

I got an interesting question in the Jetpack support forums today. Levy wanted to display Related Posts in their RSS feed.

Jetpack displays Related Posts at the bottom of single posts by default, but like with other modules, you can customize Related Posts. In this post, we’ll use the the_content filter and the raw Jetpack Related Posts class to build our own unordered list of Related Posts, and add it to the bottom of the post content in RSS feeds.

Quick note before we start:

  • You’ll want to add that code to a functionality plugin like this one.
  • Nothing will happen if you’re not using Jetpack and its Related Posts module.
/**
 * Add Jetpack Related Posts to RSS feed.
 *
 * @see https://wordpress.org/support/topic/2927523
 */
function jeherve_related_posts_feed( $content ) {
	// Return early if we're not in the RSS feed.
	if ( ! is_feed() ) {
		return $content;
	}

	// If Jetpack and Related Posts are active, let's get started.
	if ( class_exists( 'Jetpack_RelatedPosts' ) && method_exists( 'Jetpack_RelatedPosts', 'init_raw' ) ) {
		$related = Jetpack_RelatedPosts::init_raw()
			->set_query_name( 'jetpackme-shortcode' ) // Optional, name can be anything
			->get_for_post_id(
				get_the_ID(),
				array( 'size' => 3 )
			);

		if ( $related ) {
			$related_list = '';

			foreach ( $related as $result ) {
				// Get the related post IDs
				$related_post_id = get_post( $result[ 'id' ] );

				/**
				 * From there you can do just about anything, using the post IDs.
				 *
				 * In this example, we'll build an unordered list.
				 */
				$related_list .= sprintf(
					'<li><a title="%1$s" href="%2$s">%3$s</a></li>',
					esc_attr( get_the_title( $related_post_id ) ),
					get_permalink( $related_post_id ),
					get_the_title( $related_post_id )
				);
			}

			/**
			 * Let's wrap all those related posts in ul tags, and add that list to the end of our post content.
			 *
			 * We will also add a headline, but only if it was set to be displayed in your Jetpack Related Posts settings.
			 */
			$related_options = Jetpack_Options::get_option( 'relatedposts' );
			if ( $related_options['show_headline'] ) {
				$headline = sprintf(
					'<h3 class="jp-relatedposts-headline"><em>%s</em></h3>',
					esc_html__( 'Related', 'jetpack' )
				);
			} else {
				$headline = '';
			}

			return sprintf(
				'%1$s%2$s<ul class="jp-relatedposts">%3$s</ul>',
				$content,
				apply_filters( 'jetpack_relatedposts_filter_headline', $headline ),
				$related_list
			);
		}

		return $content;
	}

	// Last fallback, just in case Jetpack and Related Posts aren't there anymore.
	return $content;
}
add_filter( 'the_content', 'jeherve_related_posts_feed' );

Jetpack: how to deal with missing images in the Related Posts module

Jetpack Related Posts are great. You should use them!

They are also customizable; we added a lot of filters to allow you to customize the output of the Related Posts. You can find a lot of examples here: Customize Related Posts (Kudos to Xiao for all the great work he’s done with Jetpack Related Posts).

The module also includes an option to “use a large and visually striking layout”. When that option is active, Jetpack will look for a large image (at least 350*200px) in your posts, and use that image when displaying Related Posts. It looks great, and I use it on this site:

Related Posts

What doesn’t look that good, however, is when some of the Related Posts don’t have any image.

Missing Related Image

There are 2 ways to solve this small issue.

Continue reading

Hide and seek with WordPress menus

WordPress Menus' Screen options

WordPress 3.0 introduced a very useful feature: custom menus: if your theme handles it, you can add menus in different areas of your theme, without having to dive into code or install any plugin.

There is only one limitation to this: your menu is often added to your theme’s header or footer and the menu items remain the same on all pages of your site.

Let’s see how to work around this problem, and have different menu items depending on the page you’re on.

Let’s dig into this small tutorial!

Discover Custom user taxonomies with Justin Tadlock

When WordPress 2.8 came out, we all discover the wonders of taxonomies, and how we could add extra information to our posts and custom post types really easily.

However, it turns out taxonomies have existed for a long time and are not just available for posts, but for all object types. I for one had no idea about this!

Luckily, Justin Tadlock is here! In this tutorial, he covers the basics and examples of what you can do with custom user taxonomies in WordPress; a must-read, as with all tutorials from Justin.

Read the tutorial here: Custom user taxonomies in WordPress.