Search code examples
phpwordpresscurlwordpress-hook

remote request from functions.php is not working


I am writing a custom function for a post_updated action in wordpress like below:

function post_update_trigger($post_ID, $post_after, $post_before){

    if($post_after->post_status == "publish" || $post_after->post_status == "trash" ){

        $url="https://myremoteurl.com/feed/blogAPI";

       $response = wp_remote_post($url,array(
                         'method' => 'POST',
                         'timeout' => 45,
                         'redirection' => 5,
                         'httpversion' => '1.0',
                         'blocking' => true,
                         'headers' => array(),
                         'body' => $postFields,
                         'cookies' => array()
                       ));

           if ( is_wp_error( $response ) ) {
                    $error_message = $response->get_error_message();
                    echo "Something went wrong: $error_message";exit;
            } else {
                    echo 'Response:<pre>';
                   print_r( $response );exit;
                   echo '</pre>';
    }

    }

}

add_action( 'post_updated', 'post_update_trigger', 10, 3 );

I tried the post request from postman. everything seems to be fine and working. other than wp_remote_post, i tried CURL too.

What i am doing wrong.

see my post man request:

postman request image

PS: The blog is present in sub folder of the project root. is this causing the problem?


Solution

  • the error is due to requesting same origin as remote, i.e my controller is at https://dev.kidengage.com/feed/blogAPI.

    But my wordpress is in subfolder of root directory which is at https://dev.kidengage.com/blog.

    so using wp_remote_post will result in funky response.

    I ended up storing the array using like below directly from wordpress to another db which my root is using.

            global $wpdb;
    
            $newdb = new wpdb('username','password','dagtabasename','localhost');
            $newdb->show_errors();
    
            $postExist= $newdb->get_row("select * from blogMaster where postID=$post_ID");
    
            if(empty($postExist)){
    
                $insert=array(
                    "postID" => $post_ID,
                    "postTitle" => $post_after->post_title,
                    "postContent" => $post_after->post_content ,
                    "postExcerpt" =>  $post_after->post_excerpt,
                    "postType" =>  $post_after->post_type,
                    "postGuid" => $post_after->guid ,
                    "postPermLink" =>  $permLink,
                    "postThumb" =>  $thumbLarge,
                    "postAuthorName" => $authorName,
                    "postAuthorPic" => $authorURL ,
                    "postStatus" => $post_after->post_status,
                    "postDate" =>  $post_after->post_date,
                    "postModified" =>  $post_after->post_modified
                );
    
    
                $newdb->insert("blogMaster",$insert);    
    
            }