Created
October 20, 2021 22:24
-
-
Save samiff/f8c6e39734afb50bfd6e089fab044cde to your computer and use it in GitHub Desktop.
Jetpack Publicize custom message
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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