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
|
1 Answer
Reset to default 0Your 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
版权声明:本文标题:php - Remove all feeds and return custom 404 page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742418855a2471248.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
template_redirect()
is called on feeds? – Ted Stresen-Reuter Commented May 18, 2020 at 16:29