Support » Plugin: WP Discord Post » Modified to Handle Scheduled Post Notifications

  • Resolved btreece84

    (@btreece84)


    My team uses post scheduling every day, and for that reason, I wanted to save them having to tell other team members when they’ve scheduled a post. Trying to automate some of these messages to make things a tad easier on them. The plugin gave me an easy way to do that for posts that are published immediately, but I had to add an action call and a function to handle scheduled posts.

    For those interested in doing the same, I’m sharing the code I used (completely without support, mind you). This code works for me, and if you’re applying this update then you should understand how to customize it for your use.

    The addition uses the default plugin Bot settings to connect on the same Discord webhook.

    Open wp-discord-post.php in your preferred text editor.

    On line 32, add add_action( 'draft_to_future', array( $this, 'send_future_post' ), 10, 1 );

    On line 201, add…

    	/**
    	 * Sends the scheduled post to Discord using the specified webhook URL and Bot token.
    	 *
    	 * @param  WP_Post $post The post object.
    	 */
    	public function send_future_post( $post ) {
    		// Check if the post has been already published.
    		if ( ! $this->is_new_scheduled_post( $post->ID ) ) {
    			return;
    		}
    
    		$author = $post->post_author;
    		$author = get_user_by( 'ID', $author );
    		$author = $author->display_name;
    
    		$future_date 	= strtotime( $post->post_date );
    
    		$sched_time 	= date( 'H:i', $future_date );
    		$sched_day 		= date( 'l', $future_date );
    		$sched_date 	= date( 'M j', $future_date );
    
    		$bot_username     = get_option( 'wp_discord_post_bot_username' );
    		$bot_avatar       = get_option( 'wp_discord_post_avatar_url' );
    		$bot_token        = get_option( 'wp_discord_post_bot_token' );
    		$webhook_url      = get_option( 'wp_discord_post_webhook_url' );
    		$mention_everyone = get_option( 'wp_discord_post_mention_everyone' );
    
    		if ( 'yes' === $mention_everyone ) {
    			$author = '@everyone ' . $author;
    		}
    
    		$args = array(
    			'content'    => sprintf( '<code></code>

    Author: %1$s
    Title: %2$s
    Date: %4$s – %5$s
    Time: %3$s
    `’, $author, $post->post_title, $sched_time, $sched_day, $sched_date ),
    ‘username’ => ‘New Post Scheduled!’,
    ‘avatar_url’ => esc_url( $bot_avatar ),
    );

    $args = apply_filters( ‘wp_discord_request_args’, $args );

    $request = array(
    ‘headers’ => array(
    ‘Authorization’ => ‘Bot ‘ . esc_html( $bot_token ),
    ‘Content-Type’ => ‘application/json’,
    ),
    ‘body’ => wp_json_encode( $args ),
    );

    $response = wp_remote_post( esc_url( $webhook_url ), $request );

    if ( ! is_wp_error( $response ) && 200 === wp_remote_retrieve_response_code( $response ) ) {
    add_post_meta( $post->ID, ‘_wp_discord_post_scheduled’, ‘yes’ );
    }
    }

    
    
    Finally, on line 269, add
    

    /**
    * Checks if a post has been published already or not.
    *
    * @param int $post_id The post ID.
    * @return bool
    */
    public function is_new_scheduled_post( $post_id ) {
    return ‘yes’ !== get_post_meta( $post_id, ‘_wp_discord_post_scheduled’, true );
    }
    `

    Look over the code, particularly the $args array around line 232, and adjust as you see fit. I have the output of the message formatted using a code block in Discord. Here is a screenshot of what the above code produces: http://i.imgur.com/iSOyiLl.png

    The plugin author is free to incorporate this into the plugin.

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Modified to Handle Scheduled Post Notifications’ is closed to new replies.