The Divi theme is a popular WordPress theme that offers a wide range of customization options, including blog post sharing options. By default, Divi includes social sharing buttons for popular social media platforms such as Facebook, Twitter, and Pinterest. However, if you want to add additional sharing options or customize the existing ones, you can use the et_social_share filter provided by Divi.
To add custom sharing options to Divi’s blog posts, you can use the following code snippet:
function wpsnippets_add_custom_share_options($share_options) {
// Add a custom sharing option for LinkedIn
$share_options['linkedin'] = array(
'title' => 'LinkedIn',
'icon' => 'et-pinterest',
'url' => 'https://www.linkedin.com/shareArticle?url=%url%',
);
// Add a custom sharing option for WhatsApp
$share_options['whatsapp'] = array(
'title' => 'WhatsApp',
'icon' => 'et-whatsapp',
'url' => 'https://api.whatsapp.com/send?text=%url%',
);
return $share_options;
}
add_filter('et_social_share', 'wpsnippets_add_custom_share_options');
In this code snippet, we define a custom function wpsnippets_add_custom_share_options that takes the existing share options as a parameter. Inside the function, we add two custom sharing options: LinkedIn and WhatsApp. For each option, we specify the title, icon class, and the URL template. The %url% placeholder will be replaced with the actual URL of the blog post when the sharing button is clicked.
Finally, we use the add_filter function to hook our custom function to the et_social_share filter, which allows us to modify the share options provided by Divi.
With this code snippet, you can easily add custom sharing options to Divi’s blog posts, giving your visitors more ways to share your content on social media platforms like LinkedIn and WhatsApp.
Examples
Example 1: Adding Custom Sharing Options to Divi Blog Posts
This example demonstrates how to add custom sharing options to Divi blog posts using the et_pb_postinfo_meta hook. The code adds a new sharing option for LinkedIn to the default sharing options provided by Divi.
function wpsnippets_add_linkedin_sharing_option() {
echo '<a href="https://www.linkedin.com/shareArticle?url=' . get_permalink() . '" target="_blank" rel="nofollow noopener noreferrer" class="et-share-linkedin">LinkedIn</a>';
}
add_action( 'et_pb_postinfo_meta', 'wpsnippets_add_linkedin_sharing_option' );
The code above adds a new sharing option for LinkedIn by appending a custom HTML link to the et_pb_postinfo_meta section of Divi’s blog post meta. The link includes the current post’s permalink as the URL parameter.
Example 2: Customizing the Order of Sharing Options
This example demonstrates how to customize the order of sharing options in Divi’s blog post meta. The code reorders the default sharing options provided by Divi to display Twitter before Facebook.
function wpsnippets_reorder_sharing_options( $sharing_options ) {
$new_order = array( 'twitter', 'facebook', 'pinterest', 'linkedin', 'reddit', 'email' );
$reordered_options = array();
foreach ( $new_order as $option ) {
if ( isset( $sharing_options[ $option ] ) ) {
$reordered_options[ $option ] = $sharing_options[ $option ];
unset( $sharing_options[ $option ] );
}
}
return array_merge( $reordered_options, $sharing_options );
}
add_filter( 'et_pb_postinfo_sharing_options', 'wpsnippets_reorder_sharing_options' );
The code above uses the et_pb_postinfo_sharing_options filter to reorder the sharing options. It creates a new array with the desired order of options and iterates through it, moving the corresponding options from the original array to the new array. Finally, it merges the new array with the remaining options.
Example 3: Removing Sharing Options
This example demonstrates how to remove specific sharing options from Divi’s blog post meta. The code removes the Pinterest and Email sharing options from the default options provided by Divi.
function wpsnippets_remove_sharing_options( $sharing_options ) {
unset( $sharing_options['pinterest'] );
unset( $sharing_options['email'] );
return $sharing_options;
}
add_filter( 'et_pb_postinfo_sharing_options', 'wpsnippets_remove_sharing_options' );
The code above uses the et_pb_postinfo_sharing_options filter to remove specific sharing options. It unsets the ‘pinterest’ and ’email’ keys from the $sharing_options array, effectively removing them from the list of available options.
