Updated the code a bit, added image and excerpt of the post
post_type != 'post')
return;
$webhookURL = get_option('discord_webhook_url');
$id = $post->ID;
$author = $post->post_author;
$authorName = get_the_author_meta('display_name', $author);
$postTitle = $post->post_title;
$permalink = get_permalink($id);
$excerpt = $post->post_excerpt;
// Get the featured image URL
$featuredImage = get_the_post_thumbnail_url($post, 'full');
// Create the payload for the message
$payload = array(
'content' => "@Everyone " . $authorName . " just posted \"" . $postTitle . "\" - " . $permalink . "",
'embeds' => array(
array(
'image' => array(
'url' => $featuredImage
),
'description' => $excerpt
)
)
);
send_webhook_message($webhookURL, $payload);
}
function send_webhook_message($webhookURL, $payload) {
$curl = curl_init($webhookURL);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($payload));
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($curl);
$errors = curl_error($curl);
log_message($errors);
curl_close($curl);
}
function log_message($log) {
if (true === WP_DEBUG) {
if (is_array($log) || is_object($log)) {
error_log(print_r($log, true));
} else {
error_log($log);
}
}
}
add_action('transition_post_status', 'post_to_discord', 10, 3);
function post_to_discord_section_callback() {
echo "
A valid Discord Webhook URL to the announcements channel is required."; } function post_to_discord_input_callback() { echo ''; } function post_to_discord_settings_init() { add_settings_section( 'discord_webhook_url', 'Post to Discord', 'post_to_discord_section_callback', 'general' ); add_settings_field( 'discord_webhook_url', 'Discord Webhook URL', 'post_to_discord_input_callback', 'general', 'discord_webhook_url' ); register_setting('general', 'discord_webhook_url'); } add_action('admin_init', 'post_to_discord_settings_init');