admin管理员组

文章数量:1336660

Hello I found the below code on a old post. I have opened up using ftp and created a new plugin folder called my first plugin and created it in the plugins folder - I have opened up my first plugin folder and created a new file called my first plugin.php and added the code below to it - refreshed ftp and ended the session - went into plugins on my wordpress site and activated the newly created plugin and then searched my site/feed - but it is still showing feed and not my custom 404 page. Could anyone help me to rectify this please so all feeds return my custom 404 page. All help is much appreciated. Many thanks

/**
 * Plugin Name: T5 404 Feed
 * Description: Sends a 404 status code for all feeds and loads the <code>404.php</code> template.
 */

add_action( 'template_redirect', 't5_404_feed', 1 );
function t5_404_feed()
{
    if ( is_feed() )
    {
        status_header( '404' );
        locate_template( array ( '404.php', 'index.php ' ), TRUE, TRUE );
        exit;
    }
}

Hello I found the below code on a old post. I have opened up using ftp and created a new plugin folder called my first plugin and created it in the plugins folder - I have opened up my first plugin folder and created a new file called my first plugin.php and added the code below to it - refreshed ftp and ended the session - went into plugins on my wordpress site and activated the newly created plugin and then searched my site/feed - but it is still showing feed and not my custom 404 page. Could anyone help me to rectify this please so all feeds return my custom 404 page. All help is much appreciated. Many thanks

/**
 * Plugin Name: T5 404 Feed
 * Description: Sends a 404 status code for all feeds and loads the <code>404.php</code> template.
 */

add_action( 'template_redirect', 't5_404_feed', 1 );
function t5_404_feed()
{
    if ( is_feed() )
    {
        status_header( '404' );
        locate_template( array ( '404.php', 'index.php ' ), TRUE, TRUE );
        exit;
    }
}
Share Improve this question edited May 18, 2020 at 16:58 Ted Stresen-Reuter 6924 silver badges15 bronze badges asked May 18, 2020 at 16:07 gcdecgcdec 37 bronze badges 2
  • Hi gcdec. Welcome to StackExchange :-). Are you sure template_redirect() is called on feeds? – Ted Stresen-Reuter Commented May 18, 2020 at 16:29
  • Thank you for your reply. I found this post wordpress.stackexchange/questions/21916/… and just followed instructions. - I want to disable all wordpress feed - feeds and return my custom 404.php page – gcdec Commented May 18, 2020 at 21:21
Add a comment  | 

1 Answer 1

Reset to default 0

Your plugin file should consist of exactly this code:

<?php
/**
 * Plugin Name: T5 404 Feed
 * Description: Sends a 404 status code for all feeds and loads the <code>404.php</code> template.
 */

add_action( 'template_redirect', 't5_404_feed', 1 );
function t5_404_feed()
{
    if ( is_feed() )
    {
        status_header( '404' );
        header('Content-Type: text/html', true);
        locate_template( array ( '404.php', 'index.php' ), TRUE, TRUE );
        exit;
    }
}

Without the header() function, the page is being delivered as an RSS feed (content type of application/rss+xml) and you need it delivered as HTML (content type of text/html).

If you are getting errors when installing / activating, then something else may be interfering. I tried this code as a theme modification and it works fine. I have not tried it as a plugin. Often, there is a php_error.log file somewhere on your server where you can see the exact error that's occurring. If you find it, please post it here.

HTH

本文标签: phpRemove all feeds and return custom 404 page