0

My users provide an external link when the post is created. If the external link is deleted, the local post should also be deleted. To accomplish that, I tried running this code...

function check_external_page_status()
{
if( is_single() )
{
   if(get_field('external_listing_page'))
    {
        $external_url = get_field('external_listing_page');

        function get_http_response_code($external_url) {
            $external_headers = get_headers($external_url);
            return substr($external_headers[0], 9, 3);
        }

        $get_http_response_code = get_http_response_code($external_url);

        if ( $get_http_response_code == 200 ) {
            //echo "OKAY!";
        }
        else
        {
            //echo "Not okay!";
            //echo $get_http_response_code;
            //echo get_the_ID();

            wp_delete_post( get_the_ID(), false );
            wp_redirect( home_url( '/expired-listing/',410 ) );
            exit;
        }       
    }
}
}
add_action( 'template_redirect', 'check_external_page_status' );

...but I get these errors...

Warning: get_headers(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/cornwal2/public_html/listings/wp-content/plugins/insert-php/includes/class.execute.snippet.php(390) : eval()'d code on line 12

Warning: get_headers(http://fdsafdsfasd.ca): failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/cornwal2/public_html/listings/wp-content/plugins/insert-php/includes/class.execute.snippet.php(390) : eval()'d code on line 12

Warning: Cannot modify header information - headers already sent by (output started at /home/cornwal2/public_html/listings/wp-content/plugins/insert-php/includes/class.execute.snippet.php(390) : eval()'d code:12) in /home/cornwal2/public_html/listings/wp-includes/pluggable.php on line 1251

Warning: Cannot modify header information - headers already sent by (output started at /home/cornwal2/public_html/listings/wp-content/plugins/insert-php/includes/class.execute.snippet.php(390) : eval()'d code:12) in /home/cornwal2/public_html/listings/wp-includes/pluggable.php on line 1254

G-J
  • 1,080
  • 2
  • 16
  • 32
  • Possible duplicate of [file\_get\_contents(): php\_network\_getaddresses: getaddrinfo failed: Name or service not known](https://stackoverflow.com/questions/20064372/file-get-contents-php-network-getaddresses-getaddrinfo-failed-name-or-servi) – cabrerahector May 10 '19 at 20:44

1 Answers1

0

So I figured it out... The first 2 warnings relate to the external page that is not valid. Although the warning is legit, in this case I don't want to see it so I simply turned off the warnings. As for the last warnings, I use a javascript redirect instead of the wp_redirect()

function check_external_page_status()
{
if( is_single() )
{
   if(get_field('external_listing_page'))
    {
        $external_url = get_field('external_listing_page');

        function get_http_response_code($external_url) {
           $external_headers = get_headers($external_url);
            return substr($external_headers[0], 9, 3);
        }

        error_reporting(E_ERROR);
        $get_http_response_code = get_http_response_code($external_url);
        //error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
        error_reporting(E_ERROR | E_WARNING | E_PARSE);

        if ( $get_http_response_code == 200 ) {
            //echo "OKAY!";
        }
        else
        {
            wp_die($external_url."<br>Error:".$get_http_response_code);
            //echo "Not okay!";
            //echo $get_http_response_code;
            //echo get_the_ID();

            wp_delete_post( get_the_ID(), false );
            //wp_redirect( home_url( '/expired-listing/',410 ) );
            echo "<script>window.location.replace('".home_url('/expired-listing/')."');</script>";
            exit;
        }       
    }
}
}
add_action( 'template_redirect', 'check_external_page_status' );
G-J
  • 1,080
  • 2
  • 16
  • 32