Increase the number of fields you can use in the Contact Form builder

By default, you can only add up to 9 fields to a contact form you create with Jetpack’s Contact Form feature. Once you reach that number, the link to add new fields disappears from the form builder.

You can use the grunion_max_new_fields filter to change that number, like so:

function jeherve_more_contact_fields() {
    return 20;
}
add_filter( 'grunion_max_new_fields', 'jeherve_more_contact_fields' );

This can be useful on sites where folks need to create advanced forms, with multiple options.

Posted in Code snippets | Tagged , , | Comments Off on Increase the number of fields you can use in the Contact Form builder

SEO: Create a description meta tag based on Jetpack’s Open Graph Meta tags

Jetpack includes Open Graph Meta tags. We add these tags whenever you activate the Sharing module or the Publicize module.

These tags allow Social Networks like Facebook, LinkedIn, Google+, or Pinterest to gather information about a post and build a post preview. Without those tags, Social Networks like Facebook will in some cases reject a post that’s been posted via an app, like Publicize.

We also add Twitter Meta Cards tags, since Twitter requires its own set of tags.

These Social Networks do not rely on the description meta tag, though. That’s why we didn’t add it to the list of tags added by Jetpack. Since this tag is mostly useful for Search Engines, it’s often included in Search Engine Optimization plugins, like Yoast SEO. If you’re interested in adding the tag to your site, you could use this plugin.

Another alternative would be to extend Jetpack’s default list of tags, and add your own, based on the existing og:description tag. To do so, you can paste the following in your theme’s functions.php file, or in a functionality plugin:

/**
 * Add a new tag, named description, matching the existing og:description tag.
 *
 * If og:description doesn't exist for a post/page, do not add any new tag.
 *
 * @filter jetpack_open_graph_tags
 * @uses isset
 */
function jeherve_custom_desc_tag( $tags ) {
	if ( isset( $tags['og:description'] ) ) {
		$tags['description'] = $tags['og:description'];
	}
	return $tags;
}
add_filter( 'jetpack_open_graph_tags', 'jeherve_custom_desc_tag' );

/**
 * Replace property="description" by name="description" in the new tag.
 *
 * Jetpack uses property="thing" as template for its Meta tags,
 * because it's built to output OG Tags. However, we we want to output a general tag here.
 *
 * @filter jetpack_open_graph_output
 * @uses str_replace
 */
function jeherve_desc_tag_output( $og_tag ) {

	// Replace property="description" by name="description"
	$og_tag = str_replace( 'property="description"', 'name="description"', $og_tag );

	return $og_tag;
}
add_filter( 'jetpack_open_graph_output', 'jeherve_desc_tag_output' );
Posted in Code snippets | Tagged , , , , , | Comments Off on SEO: Create a description meta tag based on Jetpack’s Open Graph Meta tags

Sharing: do not display sharing buttons on Mobile

Whether your site uses Jetpack’s Mobile Theme or a responsive theme, it’s sometimes useful to remove sharing buttons on mobile. Since most mobile phones include built-in options to share a link on popular social networks, removing those buttons from your site can be a real performance gain on mobile.

To completely remove the buttons, we’ll use 2 Jetpack features: the sharing_show filter, and a function named jetpack_is_mobile(). That function allows you to detect whether your reader browses your site using a mobile device (with a Mobile User Agent).

// Check if we are on mobile
function jetpackme_is_mobile() {

    // Are Jetpack Mobile functions available?
    if ( ! function_exists( 'jetpack_is_mobile' ) ) {
        return false;
    }

    // Is Mobile theme showing?
    if ( isset( $_COOKIE['akm_mobile'] ) && $_COOKIE['akm_mobile'] == 'false' ) {
        return false;
    }

    return jetpack_is_mobile();
}

// Let's remove the sharing buttons when on mobile
function jetpackme_maybe_add_filter() {

    // On mobile?
    if ( jetpackme_is_mobile() ) {
        add_filter( 'sharing_show', '__return_false' );
    }
}
add_action( 'wp_head', 'jetpackme_maybe_add_filter' );
Posted in Code snippets | Tagged , , , , | Comments Off on Sharing: do not display sharing buttons on Mobile

Top Posts & Pages Widget: Remove all Popular posts that weren’t published within the last month

Top Posts & Pages Widget: Remove all Popular posts that weren’t published within the last month

By default, the Top Posts widget includes all popular posts on your site in the past 2 days, even if those posts were published a long time ago. The jetpack_widget_get_top_posts filter allows you to change that, and remove all Popular posts that weren’t published within the last month.

/**
 * Remove all Popular posts that weren't published within the last month
 */
function jeherve_remove_old_top_posts( $posts, $post_ids, $count ) {
    foreach ( $posts as $k => $post ) {
        // Date when the post was published
        $post_date = strtotime( get_the_date( 'Ymd', $post['post_id'] ) );

        // If the post was published more than 30 days ago, let's remove it from the Top Posts widget
        if ( $post_date < strtotime( '-30 days' ) ) {
            unset( $posts[$k] );
        }
    }
    return $posts;
}
add_filter( 'jetpack_widget_get_top_posts', 'jeherve_remove_old_top_posts', 10, 3 );
Posted in Code snippets | Tagged , , , , | Comments Off on Top Posts & Pages Widget: Remove all Popular posts that weren’t published within the last month

Sitemaps: add Custom Post Type support

By default, Jetpack’s new Sitemaps module supports Posts and Pages. It also supports Jetpack’s Custom Content Types if you use them on your site.

If you’d like to add support for another Custom Post Type, you can use the jetpack_sitemap_post_types filter, like so:

function jeherve_add_cpt_sitemaps( $post_types ) {
	$post_types[] = 'your_post_type';
	return $post_types;
}
add_filter( 'jetpack_sitemap_post_types', 'jeherve_add_cpt_sitemaps' );

Fore more details, see the page on how to use filters to customize Jetpack.

Posted in Code snippets | Tagged , , , | Comments Off on Sitemaps: add Custom Post Type support