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' );
This entry was posted in Code snippets and tagged , , , , . Bookmark the permalink.

Have a note to contribute?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Say Hello to 200 Themes

Jetpack Professional now bundles 200+ Premium WordPress themes,
alongside business class security tools, and marketing automation.

Use code "PRO" at checkout for 50% introductory discount.