Skip to content

Instantly share code, notes, and snippets.

@n7studios
Created January 18, 2024 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n7studios/d1dba356e43ee707c9955d63b5580d12 to your computer and use it in GitHub Desktop.
Save n7studios/d1dba356e43ee707c9955d63b5580d12 to your computer and use it in GitHub Desktop.
WordPress to Buffer Pro: WPML: Prevent Posts sending to Buffer by Language
<?php
/**
* Plugin Name: WP to Buffer Pro: WPML
* Plugin URI: https://www.wpzinc.com
* Version: 0.0.1
* Author: WP Zinc
* Author URI: https://www.wpzinc.com
* Description: Prevent Posts, Pages or Custom Posts sending to Buffer when they match specific WPML languages.
*/
/**
* Determine the language of the Post in WPML, to decide whether to send a status to Buffer
*
* @since 0.0.1
*
* @param array $args API standardised arguments.
* @param WP_Post $post WordPress Post.
* @param string $profile_id Social Media Profile ID.
* @param string $service Social Media Service.
* @param array $status Parsed Status Message Settings.
* @param string $action Action (publish|update|repost|bulk_publish).
*/
add_filter( 'wp_to_buffer_pro_publish_build_args', function( $args, $post, $profile_id, $service, $status, $action ) {
// Define the language codes for Posts you do NOT want to send to Buffer.
// In this example, our site is in English, with French and Spanish configured in WPML as an additional language.
// We do NOT want to send Posts to Buffer that are in French or Spanish.
$excluded_language_codes = array(
'fr',
'es',
);
// Stop editing.
global $sitepress;
// Bail if WPML isn't active.
if ( ! function_exists( 'wpml_get_language_information' ) ) {
return $args;
}
if ( is_null( $sitepress ) ) {
return $args;
}
// Get the Site's Current Language.
$current_language = $sitepress->get_current_language();
// Get the Post's Language.
$post_language = wpml_get_language_information( null, $post->ID );
// Bail if we couldn't fetch language information.
if ( is_wp_error( $post_language ) ) {
return $args;
}
// Bail if WPML hasn't been enabled on the Post Type (language code will be null).
if ( is_null( $post_language['language_code'] ) ) {
return $args;
}
// If the Post is in a language that we do not want to send to Buffer, return an error now.
if ( in_array( $post_language['language_code'], $excluded_language_codes, true ) ) {
return new WP_Error(
'wpml_excluded_language',
sprintf(
'WP to Buffer Pro: WPML has been configured to exclude %s from being sent to Buffer.',
$post_language['language_code']
)
);
}
// Permit the Post to be sent to Buffer.
return $args;
}, 10, 6 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment