Suporte » Desenvolvendo com WordPress » Erro com o hook save_post

  • Bom dia pessoal,

    Poderiam me ajudar, no caso eu preciso fazer 2 coisas logo após que o post é salvo, é ele chegou a fazer mas ele me aponta erros de JSON INVÁLIDO, e também de parâmetros invalidos.

    No caso, é tem um bloco personalizado que cria alguns spans, com classes diferentes e de acordo com essas classes ele pega e cria o termo/categoria e grava no banco.
    Isso quando verem no código esta funcionando, mas ele da um erro no js que não deixa os campos tickados.

    O segundo é eu preciso enviar um post, para um endpoint externo avisando que foi salvo algo no banco.

    add_action( 'save_post_noticia', 'terms_save');
    
    function terms_save() {
        global $post;
    
        //$str = html_entity_decode($post->post_content);
        $str = !empty(html_entity_decode($post->post_content)) ? html_entity_decode($post->post_content) : 'null';
        $d = new DOMDocument();
        $d->loadHTML( $str );
        $x = new DOMXpath($d);
        $hash = $x->evaluate('//span[contains(@class, "hash")]');
        $tickers = $x->evaluate('//span[contains(@class, "dollar")]');
        $siglas = $x->evaluate('//span[contains(@class, "tag")]');
        //$at = $x->evaluate('//span[contains(@class, "at")]');
      
        //var_dump($hash);
        //var_dump($tickers);
        //var_dump($siglas);
        
        if( count( $hash ) ){
          $array = array();
          $int = count( $hash );
          foreach ( $hash as $el ) {
            $term_name = str_replace('#', '', $el->nodeValue );
      
            for ($i=0; $i < $int; $i++) { 
              array_push($array, $term_name);
            }
      
            if(!term_exists( $term_name, 'tags_notice')){
      
              wp_insert_term($term_name, 'tags_notice');
              wp_set_object_terms( $post->ID, $array, 'tags_notice', false );
      
            }else{
              wp_set_object_terms( $post->ID, $array, 'tags_notice', false );
            }
    
            
          }
        }
      
        if( count( $tickers ) ){
          $array = array();
          $int = count( $tickers );
          foreach ( $tickers as $ticker ) {
            $term_name = str_replace('$', '', $ticker->nodeValue );
      
            for ($i=0; $i < $int; $i++) { 
              array_push($array, $term_name);
            }
      
            if(!term_exists( $term_name, 'ativos_notice')){
      
              wp_insert_term($term_name, 'ativos_notice');
              wp_set_object_terms( $post->ID, $array, 'ativos_notice', false );
              
      
            }else{
      
              wp_set_object_terms( $post->ID, $array, 'ativos_notice', false );
      
            }
      
          }
        }
      
        if(  count( $siglas ) ){
          $array = array();
          $int = count( $siglas );
          foreach ( $siglas as $sigla ) {
            
            for ($i=0; $i < $int; $i++) { 
              array_push($array, $sigla->nodeValue);
            }
      
            if(!term_exists( $sigla->nodeValue, 'siglas_notice')){
      
              wp_insert_term($sigla->nodeValue, 'siglas_notice');
              wp_set_object_terms( $post->ID, $array, 'siglas_notice', false );
      
            }else{
      
              wp_set_object_terms( $post->ID, $array, 'siglas_notice', false );
      
            }
          }
        }
    
        $post_id = $post->ID;
    
        viacurl($post_id);
        
    }
    
    function viacurl($post_id)
    {
    
    	$curl = curl_init();
    
    	curl_setopt_array($curl, array(
            CURLOPT_URL => 'http://localhost/mensageria/wp-json/wp/v2/noticias/'.$post_id.'/?acf_format=standard',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'GET',
    	));
    
    	$response = curl_exec($curl);
    
    	curl_close($curl);
    
      $urlendpoint = get_field('endpoint_url_notices', 'option');
    
      $data = $response;
      $url = $urlendpoint;
    
      httpPost($url, $data);
    
    }
    
    function httpPost($url, $data)
    {
    
        $tokenendpoint = get_field('token_endpoint_notices', 'option');
        $typetoken = get_field('field_635c140300703','option');
    
        $curl = curl_init();
    
        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POSTFIELDS => $data,
            CURLOPT_HTTPHEADER => array($tokenendpoint,$typetoken),
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_RETURNTRANSFER => true,
        ));
    
        $response = curl_exec($curl);
    
        // Further processing ...
        if ($response == "OK") { 
            echo 'sucess api send';
        } else { 
            echo 'api not send';
        }
    
        curl_close($curl);
    
        return $response;
    }

    Por favor se puderem me ajudar.

    A página que eu preciso de ajuda: [fazer login para ver o link]

  • O tópico ‘Erro com o hook save_post’ está fechado para novas respostas.