Edit the default rss url of wordpress - php

I need to change the default rss url of my website:
from example.com/feed to example.com/MyfeedName
Update:
what i tried so far is to create another Url feed but i need to remove firstexample.com/feed:
add_action( 'init', function()
{
add_feed( 'secretfeed', 'do_feed_rss2' );
});
add_action( 'pre_get_posts', function( \WP_Query $q )
{
if( $q->is_feed( 'secretfeed' ) )
add_filter( 'option_rss_use_excerpt', '__return_false' );
} );
do you have any idea how to just edit example.com/feed or how to delete it without losing rss functions ?

I found my answer here :
https://wordpress.stackexchange.com/a/214883/71314
function remove_feed( $feedname ) {
global $wp_rewrite;
if ( in_array( $feedname, $wp_rewrite->feeds ) ) {
$wp_rewrite->feeds = array_diff( $wp_rewrite->feeds, array( $feedname ) );
}
$hook = 'do_feed_' . $feedname;
// Remove default function hook
remove_all_actions( $hook );
add_action( $hook, $hook );
return $hook;
}
Usage:
remove_feed( 'feed' );

Related

Remove cpt slug and add custom taxonomy in url

I need to change the custom post type url, this code works it but is also affecting other cpts causing 404 in all other cpt, how can I make it only for the one I need.
Here is my code
// Rewrite urls of resources and put category on url
function change_custom_post_link( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_object( $post ) ){
$terms = get_the_terms( $post->ID, 'resource_type' );
if(get_post_type($post->ID) == 'resource' ) {
if( $terms ){
return home_url( "/".$terms[0]->slug."/".$post->post_name );
}else{
return home_url( "/resource/".$post->post_name );
}
}
}
return $post_link;
}
add_filter( 'post_type_link', 'change_custom_post_link', 1, 3 );
and this is the rewrite
function resource_rewrite_rules() {
add_rewrite_rule(
'^(.*)/(.*)/?$',
'index.php?post_type=resource&name=$matches[2]',
'top'
);
}
add_action( 'init', 'resource_rewrite_rules' );
Why are you doing this? That's too tricky.
You may use this plugin to create and modify CPT: CPT plugin WordPress
Or refer to docs: https://developer.wordpress.org/reference/functions/register_post_type/

add_rewrite_rule - suddenly stopped working (wordpress)

I have a url
example.com/sample-page/?amount=567
It initially worked ok, it redirects to:
example.com/sample-page/567
But after few days, i noticed its not working any more.
Its now redirecting to post without query var or get var
What could be the reason?? Is there any solution??
I used below codes in my functions.php file:
& I already flushed permalinks.
I am using custom post type
I have removed custom post type base from url using a plugin.
UPDATE
I just noticed Its due to empty post id/pagename.
So now, Question is how to get page name of current post...inside init function dynamically??
//https://developer.wordpress.org/reference/functions/wp_redirect/#comment-4109
add_action( 'init', function() {
global $wp;
$pagename = $wp->request;
add_rewrite_rule( ''.$pagename.'/([0-9]+)[/]?$', 'index.php?pagename='.$pagename.'&amount=$matches[1]', 'top' );
} );
add_filter( 'query_vars', function( $query_vars ) {
$query_vars[] = 'amount';
return $query_vars;
} );
function wpb_change_search_url() {
global $wp;
if ( ! empty( $_GET['amount'] ) ) {
wp_redirect( get_permalink( home_url( $wp->request ) ) . urlencode( get_query_var( 'amount' ) ) );
exit();
}
}
add_action( 'template_redirect', 'wpb_change_search_url' );

How To Disable (or Remove) “All Comments, Published, and Trash” in WP Dashboard for non admins

So I have found the way to remove all of that in Posts (for non admins) with the following line:
/**
* Remove the 'all', 'publish', 'future', 'sticky', 'draft', 'pending', 'trash'
* views for non-admins
*/
add_filter( 'views_edit-post', function( $views )
{
if( current_user_can( 'manage_options' ) )
return $views;
$remove_views = [ 'all','publish','future','sticky','draft','pending','trash' ];
foreach( (array) $remove_views as $view )
{
if( isset( $views[$view] ) )
unset( $views[$view] );
}
return $views;
} );
Now I want to remove all of those in Comments as well.
I can't find the answer.
Any help would be appreciated.
As said in the comments, just add another add_filter with "views_edit-comments".
To always show only own comments to a non-admin user, use the follow code:
add_action( 'current_screen', 'wp_66446729_filter_comments', 10, 2 );
function wp_66446729_filter_comments( $screen )
{
if ( current_user_can('administrator') )
return;
add_action( 'pre_get_comments', 'wp_66446729_list_own_comments_only', 10, 1 );
}
function wp_66446729_list_own_comments_only( $clauses )
{
$user_id = get_current_user_id();
if ($user_id) {
$clauses->query_vars['user_id'] = $user_id;
}
}

How can I add a route to the index in my WordPress plugin using generate_rewrite_rules?

I'm trying to add the route /.well-known/webfinger to WordPress in a plugin, e.g. http://exampleblog.com/.well-known/webfinger. I'm using the generate_rewrite_rules, parse_request, and query_vars hooks to load up some code that should run when the URL is matched. Here's what I've got:
// includes/server/api.php
namespace api;
function generate_rewrite_rules( $wp_rewrite ) {
$dot_well_known = array(
'.well-known/webfinger' => 'index.php?well-known=webfinger'
);
$wp_rewrite->rules = $dot_well_known + $wp_rewrite->rules;
}
function check_flush_rules() {
$rules = get_option( 'rewrite_rules' );
if ( ! isset( $rules['.well-known/webfinger'] ) ) {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
}
function parse_request( $req ) {
if ( ! array_key_exists( 'well-known', $req->query_vars ) ) {
return;
}
if ( $req->query_vars['well-known'] === 'webfinger' ) {
do_action( 'well_known_webfinger', $req->query_vars );
}
}
function query_vars( $query_vars ) {
$query_vars[] = 'well-known';
return $query_vars;
}
// includes/init.php
namespace init;
require_once plugin_dir_path( __FILE__ ) . 'server/api.php';
add_action( 'my_plugin_load', function() {
add_action( 'generate_rewrite_rules', '\api\generate_rewrite_rules' );
add_action( 'parse_request', '\api\parse_request' );
add_filter( 'query_vars', '\api\query_vars' );
\api\check_flush_rules();
} );
// my_plugin.php (plugin entrypoint)
require_once plugin_dir_path( __FILE__ ) . 'includes/init.php';
function my_plugin_load() {
do_action( 'my_plugin_load' );
}
add_action( 'plugins_loaded', 'my_plugin_load' );
However, when I run a local WordPress instance (via php -S localhost:8080) I'm getting a 404 Not Found status when I attempt to visit http://localhost:8080/.well-known/webfinger.
What am I doing wrong?
It turns out the problem was that I was running WordPress via php -S localhost:8080. When I ran a proper Apache webserver locally generate_rewrite_rules worked as it was supposed to.

Wordpress: Include new template for a page using query var with rewrite endpoint

I am trying to load a new page template when a query var is appended at the end of my page url:
Original url: example.com/testpage/
with variable added to the end: example.com/testpage/amp
Then it would load up a custom php template.
This seems like a straight forward operation, but I cannot get it to work.
The url loads with the /amp variable at the end, but the template does not load. If I remove the condition "get_query_var('amp')" then it loads up the template no problem. What am I missing? Thanks :)
Here is my working code:
add_filter( 'query_vars', 'register_query_var' );
function register_query_var( $vars ) {
$vars[] = 'amp';
return $vars;
}
add_rewrite_endpoint( 'amp', EP_PAGES );
add_filter( 'template_include', 'use_amp_template', 99 );
function use_amp_template( $template ) {
global $wp_query;
if ( get_query_var( 'amp' ) && is_page() ) {
$new_template = locate_template( array( 'amptemplate.php' ) );
if ( '' != $new_template ) {
return $new_template;
}
}
return $template;
}
Found a good fix on my own. Here is the code if it would help anyone.
Adding 'amp' after a page or post will load up different templates for amp versions of the page.
example.com/samplepage/amp
or
example.com/samplepost/amp
add_filter( 'query_vars', 'register_query_var' );
function register_query_var( $vars ) {
$vars[] = 'amp';
return $vars;
}
add_rewrite_endpoint( 'amp', EP_PAGES | EP_PERMALINK );
add_filter( 'template_include', 'use_amp_template', 99 );
function use_amp_template( $template ) {
global $wp_query;
if(isset( $wp_query->query['amp'] ) && is_page()){
$new_template = locate_template( array( 'amppagetemplate.php' ) );
if ( '' != $new_template ) {
return $new_template;
}
}
if(isset( $wp_query->query['amp'] ) && is_single()){
$new_template = locate_template( array( 'ampposttemplate.php' ) );
if ( '' != $new_template ) {
return $new_template;
}
}
return $template;
}

Categories