0

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;
    }
}
  • Hi gcdec. Welcome to StackExchange :-). Are you sure template_redirect() is called on feeds? – Ted Stresen-Reuter May 18 at 16:29
  • Thank you for your reply. I found this post wordpress.stackexchange.com/questions/21916/… and just followed instructions. - I want to disable all wordpress feed - feeds and return my custom 404.php page – gcdec May 18 at 21:21
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

| improve this answer | |
  • The code I sent is for use in a plugin. If you want to use it in functions.php, then you need to remove the first line that starts with <?php – Ted Stresen-Reuter May 20 at 20:28
  • Fantastic thank you so much - I removed the first line <?php - and added the rest of the code to my themes function.php file at the end of the file and saved it - searched for my site/feed and it returns my custom 404 page - I really cannot thank you enough - I appreciated all the help you have given me to sort this issue out - Thank you – gcdec May 20 at 21:37
  • If this answers your question, please remember to select it at the answer. Glad you got this working. – Ted Stresen-Reuter May 21 at 8:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.