Register More Social Networks

In the example below we add Pinterest and Google social networks to MashShare:

// Load only when MashShare is loaded and network add-on is NOT loaded
if ( prefix_can_use_plugin( 'mashsharer/mashshare.php' ) && ! prefix_can_use_plugin( 'mashshare-networks/mashshare-networks.php' ) ) {
 add_action( 'init', 'prefix_mashshare_register_new_networks' );
 add_filter( 'mashsb_array_networks', 'prefix_mashshare_array_networks' );
}

/**
 * Check whether the plugin is active and theme can rely on it
 *
 * @param string $plugin Base plugin path.
 * @return bool
 */
function prefix_can_use_plugin( $plugin ) {
 // Detect plugin. For use on Front End only.
 include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
 return is_plugin_active( $plugin );
}

/**
 * Register custom networks
 */
function prefix_mashshare_register_new_networks() {
 $networks = get_option( 'mashsb_networks' );
 // These networks can be already added by addon.
 if ( ! in_array( 'Pinterest', $networks, true ) ) {
 $networks[] = 'Pinterest';
 }
 if ( ! in_array( 'Google', $networks, true ) ) {
 $networks[] = 'Google';
 }
 update_option( 'mashsb_networks', $networks );
}

/**
 * Add custom networks to MashShare
 * 
 * Pinterest & Google
 *
 * @param array $networks       Available network list.
 *
 * @return array
 */
function prefix_mashshare_array_networks( $networks ) {
	$image     = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'medium' );
	$image_url = $image ? $image[0] : '';
	if ( ! isset( $networks['pinterest'] ) ) {
		$networks['pinterest'] = 'https://www.pinterest.com/pin/create/bookmarklet/?pinFave=1&url=' . $networks['url'] . '&media=' . $image_url . '&description=' . $networks['title'];
	}
	if ( ! isset( $networks['google'] ) ) {
		$networks['google'] = 'https://plus.google.com/share?url=' . $networks['url'];
	}
	return $networks;
}


/**
 * Register custom networks
 */
function prefix_mashshare_register_new_networks() {
 $networks = get_option( 'mashsb_networks' );
 // These networks can be already added by addon.
 if ( ! in_array( 'Pinterest', $networks, true ) ) {
 $networks[] = 'Pinterest';
 }
 if ( ! in_array( 'Google', $networks, true ) ) {
 $networks[] = 'Google';
 }
 update_option( 'mashsb_networks', $networks );
}

Explanation:

At the beginning we check if MashShare Core plugin is loaded and the network add-on is not installed. The filter should be loaded only when the mashshare-network add-on is not used! Otherwise the custom data is overwriting the network add-on data. We use the small helper function prefix_can_use_plugin() for doing so.

For registering the networks we have to modify the array in two different places. At first, we register the custom networks and update the db table mashsb_options with the names of the new networks.

Than we we build the unique share link and add this data with the filter  mashsb_array_networks  to the registered networks. 

(You find all different url's for references in the file /includes/template-functions.php in the network-addon)

These functions should be used in the activation hook of your theme, e.g.  switch_theme


Still need help? Contact Us Contact Us