WordPress的...删除帖子,如果外部帖子被删除

时间:2019-05-10 19:52:14

标签: wordpress

帖子创建后,我的用户会提供一个外部链接。如果外部链接被删除,则本地帖子也应被删除。为此,我尝试运行此代码...

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' );

...但是我得到这些错误...

  

警告:get_headers():php_network_getaddresses:getaddrinfo失败:在/home/cornwal2/public_html/listings/wp-content/plugins/insert-php/includes/class.execute.snippet.php(390中未知的名称或服务):第12行上eval()的代码

     

警告:get_headers(http://fdsafdsfasd.ca):无法打开流:php_network_getaddresses:getaddrinfo失败:/ home / cornwal2 / public_html / listings / wp-content / plugins / insert-php / includes中未知的名称或服务/class.execute.snippet.php(390):第12行上eval()的代码

     

警告:无法修改标头信息-已发送的标头(输出始于/home/cornwal2/public_html/listings/wp-content/plugins/insert-php/includes/class.execute.snippet.php(390):第1251行的/home/cornwal2/public_html/listings/wp-includes/pluggable.php中的eval()代码:12)

     

警告:无法修改标头信息-已发送的标头(输出始于/home/cornwal2/public_html/listings/wp-content/plugins/insert-php/includes/class.execute.snippet.php(390): 1254行/home/cornwal2/public_html/listings/wp-includes/pluggable.php中的eval()代码:12)

1 个答案:

答案 0 :(得分:0)

所以我想通了... 前两个警告与无效的外部页面有关。尽管警告是合法的,但在这种情况下,我不想看到它,因此我只是关闭了警告。至于最后的警告,我使用javascript重定向而不是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' );
相关问题