Suporte » Desenvolvendo com WordPress » publish_post calling twice

  • I created a function to automatically post an updated or published post to Facebook. The code as it is works exactly as expected, see:

    function enviar_post_facebook() {
        
            global $post; 
        
            $ch = curl_init();
        
            $url = 'https://graph.facebook.com/v19.0/adoromaquiagemweb/feed';
        
            $access_token = '22222222222';
            
            
            $intro = get_post_meta($post->ID, '_yoast_wpseo_metadesc', true);
            
            $link = get_permalink($post->ID);
        
            $options = array(
                CURLOPT_URL => $url,
                CURLOPT_POST => true,
                CURLOPT_POSTFIELDS => 'message='.$intro.'&link='.$link.'&access_token='.$access_token,
                CURLOPT_RETURNTRANSFER => true
            );
        
            curl_setopt_array($ch, $options);
        
            $response = curl_exec($ch);
            
        
            curl_close($ch);
        
        
    }
    
    add_action( 'publish_post', 'enviar_post_facebook' );

    However, if I insert additional text in the $intro variable, for example: $intro = get_post_meta($post->ID, '_yoast_wpseo_metadesc', true). '#hashtag1 #hashtag2 #hashtag3';

    If I do this, WordPress calls twice, so that the first time it sends a publication to Faebook with only the values of “#hashtag1 #hashtag2 #hashtag3” and the second time it sends it correctly with the meta description and hashtags.

    I can’t understand why this is happening. I’ve already done several tests to check that the meta description is not empty before calling the Facebook API. Change “publish_post” to “save_post”.

    Anyway, I just add text to my $intro variable that triggers two publications on Facebook.

    Can someone help me ?

  • Você deve estar conectado para responder a este tópico.