• Thanks to the developers for updating the plugin.

    Now we are waiting for the addition of other social networks, for example: Medium, Reddit, Telegram, Discord. I use other plugins for these services, but I would like to have everything in one plugin.

Viewing 1 replies (of 1 total)
  • Thread Starter ruzammarketing

    (@ruzammarketing)

    Here is the code I wrote for discord, you can use it:

    <?php
    /*
     * Plugin Name: Post to Discord
     * Plugin URI: https://win-win-trade.com/
     * Description: Announces new WordPress posts on Discord.
     * Version: 1.0
     * Author: Win-win Trade
     * Author URI: https://win-win-trade.com/
    */
    
    function post_to_discord($new_status, $old_status, $post) { 
        if (get_option('discord_webhook_url') == null) 
            return;
          
        if ($new_status != 'publish' || $old_status == 'publish' || $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 "<p>A valid Discord Webhook URL to the announcements channel is required.";
    }
     
    function post_to_discord_input_callback() {
        echo '<input name="discord_webhook_url" id="discord_webhook_url" type="text" value="' . get_option('discord_webhook_url') . '">';
    }
     
    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');
    
Viewing 1 replies (of 1 total)
  • The topic ‘Thanks to the developers’ is closed to new replies.