Adding social media sharing icons to your product page

There are a ton of social media sharing plugins for WordPress. Unfortunately, not all of them work very well with WooCommerce. With this recipe, we'll be installing a plugin and telling the plugin exactly where we want our social media icons so that they look nice on our product page.

Getting ready

You need to have a product in your store.

How to do it…

The first part is installing the plugin. We'll be installing Jetpack, which is built and managed by WordPress.com. After we get it installed, we only need very minor tweaks to make it work well with WooCommerce.

  1. In the WordPress admin, click on the Plugins menu and then on Add New.
  2. Run a search for Jetpack by WordPress.com.
  3. Install and activate the plugin.
  4. In the WordPress admin, click on the newly created Jetpack menu and then on the Settings submenu.

    Note

    Jetpack is known to have a lot of functionality built in. You'll want to look through what's possible and enable it. We only need to turn on the Sharing module.

  5. Enable the Sharing module.
    How to do it…
  6. Now that it's enabled, you'll have to configure it. Scroll down to the same Sharing module, hover over it, and click on Configure.

    At this point, you'll see a settings page with various options. The first step is to select which services you want listed on your site. You can pick as many as you like.

  7. Drag-and-drop services from Available Services to Enabled Services.
    How to do it…
  8. Make sure the social media icons will be shown on the product pages.
    How to do it…
  9. Click on Save Changes.

    If you went to a product page on your site right now, you'd see the social media icons, but they're all the way at the bottom of the product page – even beneath the description and additional information tabs. With a bit of custom coding, we can put them exactly where we want them.

  10. Add the following code to your theme's functions.php file, located under wp-content/themes/your-theme-name/, or your custom WooCommerce plugin:
    add_action( 'woocommerce_share', 'woocommerce_cookbook_social_share_icons', 10 ); 
    function woocommerce_cookbook_social_share_icons() { 
        if ( function_exists( 'sharing_display' ) ) { 
            remove_filter( 'the_content', 'sharing_display', 19 ); 
            remove_filter( 'the_excerpt', 'sharing_display', 19 );
            echo sharing_display(); 
        } 
    }

All done! Load up your product page and see the social media icons right beneath the Add to cart button.

How to do it…

How it works…

The Jetpack plugin handles social media sharing and icons. The code we wrote moves the output from Jetpack to exactly where we want it to be.

The first part of the code adds extra functionality on the WooCommerce product page. Within that code, we remove the old Jetpack output and print out new output. The elegant part of this code is that the Jetpack output only changes on the WooCommerce product pages. You could use the same social sharing functionality for your blog posts and the social sharing buttons will appear at the end of the post, which, of course, is controlled through Jetpack.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset