【问题标题】:Wordpress... delete post if external post is deletedWordpress...如果外部帖子被删除,则删除帖子
【发布时间】:2019-09-28 17:50:07
【问题描述】:

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

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 failed: Name or service not known in /home/cornwal2/public_html/listings/wp-content/plugins/insert-php/includes/class.execute.sn-p.php (390) : eval()'d 代码在第 12 行

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

警告:无法修改标头信息 - 标头已发送(输出开始于 /home/cornwal2/public_html/listings/wp-content/plugins/insert-php/includes/class.execute.sn-p.php(390 ) : eval()'d code:12) in /home/cornwal2/public_html/listings/wp-includes/pluggable.php 在第 1251 行

警告:无法修改标头信息 - 标头已发送(输出开始于 /home/cornwal2/public_html/listings/wp-content/plugins/insert-php/includes/class.execute.sn-p.php(390 ) : eval()'d code:12) 在 /home/cornwal2/public_html/listings/wp-includes/pluggable.php 第 1254 行

【问题讨论】:

标签: wordpress


【解决方案1】:

所以我想通了... 前 2 个警告与无效的外部页面有关。虽然警告是合法的,但在这种情况下,我不想看到它,所以我只是关闭了警告。至于最后的警告,我使用 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' );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-15
    • 2021-07-09
    • 2020-06-11
    • 1970-01-01
    相关资源
    最近更新 更多