Skip to content

Instantly share code, notes, and snippets.

@samiff
Created October 20, 2021 22:24
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 samiff/f8c6e39734afb50bfd6e089fab044cde to your computer and use it in GitHub Desktop.
Save samiff/f8c6e39734afb50bfd6e089fab044cde to your computer and use it in GitHub Desktop.
Jetpack Publicize custom message
<?php
/**
* NOTE: the following is a rough example only that would need refinement:
* - `_wpas_mess` (wp_postmeta > post_id) will only be set when a custom Publicize message is manually entered.
* - It doesn't take into account the max characters for a custom Publicize message.
*/
/**
* Customizes the Publicize message excerpt. In this example the post author is prefixed.
*
* The `publicize_save_meta` hook fires before a post is processed for Publicize.
* Hook reference: https://developer.jetpack.com/hooks/publicize_save_meta/
*/
function custom_action_publicize_save_meta( $submit_post, $post_id, $service_name, $connection ) {
$previous_message = get_post_meta( $post_id, '_wpas_mess', true );
$author = get_user_by(
'ID',
get_post_field( 'post_author', $post_id )
);
if ( ! $author || ! $author->display_name ) {
return;
}
$updated_message = "By: $author->display_name - " . $previous_message;
update_post_meta( $post_id, '_wpas_mess', $updated_message );
}
/**
* We only want to customize the Publicize message when a post is being published,
* so we can hook into `transition_post_status` and check the post statuses.
*/
function maybe_add_custom_action_publicize_save_meta( $new_status, $old_status, $post ) {
if ( 'publish' === $new_status && 'publish' !== $old_status && 'post' === $post->post_type ) {
add_action( 'publicize_save_meta', 'custom_action_publicize_save_meta', 10, 4 );
}
}
add_action( 'transition_post_status', 'maybe_add_custom_action_publicize_save_meta', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment