WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 6 years ago

Last modified 5 years ago

#6970 closed defect (bug) (fixed)

Add_feed - bug and fix - Replacing callback function

Reported by: programming.has.no.com Owned by: ryan
Milestone: 2.6 Priority: high
Severity: normal Version: 2.5.1
Component: General Keywords: add_feed
Focuses: Cc:

Description

When adding a feed using add_feed (wp-includes/rewrite.php) you can not replace the callback function with a one with a different name, as this will try to remove a callback that doesn't exist, thus remove_action fails. Adding a $remove_function to the function variables and checking for its presence and removing the correct callback function fixes this.

buggy version
function add_feed($feedname, $function) {

global $wp_rewrite;
if (!in_array($feedname, $wp_rewrite->feeds)) { override the file if it is

$wp_rewrite->feeds[] = $feedname;

}
$hook = 'do_feed_' . $feedname;
remove_action($hook, $function, 10, 1);
add_action($hook, $function, 10, 1);
return $hook;

}

fixed version
function add_feed($feedname, $function, $remove_function="") {

global $wp_rewrite;
if (!in_array($feedname, $wp_rewrite->feeds)) { override the file if it is

$wp_rewrite->feeds[] = $feedname;

}
$hook = 'do_feed_' . $feedname;
if($remove_function != "") remove_action($hook, $remove_function, 10, 1);
else remove_action($hook, $function, 10, 1);
add_action($hook, $function, 10, 1);
return $hook;

}

Change History (5)

comment:1 DD326 years ago

Code:

 buggy version
 function add_feed($feedname, $function) {
         global $wp_rewrite;
         if (!in_array($feedname, $wp_rewrite->feeds)) { //override the file if it is
                 $wp_rewrite->feeds[] = $feedname;
         }
         $hook = 'do_feed_' . $feedname;
         remove_action($hook, $function, 10, 1);
         add_action($hook, $function, 10, 1);
         return $hook;
 }
 
 
 
 fixed version
 function add_feed($feedname, $function, $remove_function="") {
         global $wp_rewrite;
         if (!in_array($feedname, $wp_rewrite->feeds)) { //override the file if it is
                 $wp_rewrite->feeds[] = $feedname;
         }
         $hook = 'do_feed_' . $feedname;
         if($remove_function != "")
                 remove_action($hook, $remove_function, 10, 1);
         else
                 remove_action($hook, $function, 10, 1);
         add_action($hook, $function, 10, 1);
         return $hook;
 }

comment:2 follow-up: ryan6 years ago

How about always removing the default function for that feed?

comment:3 in reply to: ↑ 2 programming.has.no.com6 years ago

Replying to ryan:

How about always removing the default function for that feed?

Yer that would work, and save changing the function arguments... How about this version?

function add_feed($feedname, $function) {

global $wp_rewrite;
if (!in_array($feedname, $wp_rewrite->feeds)) { override the file if it is

$wp_rewrite->feeds[] = $feedname;

}
$hook = 'do_feed_' . $feedname;
remove_action($hook, $hook, 10, 1);
add_action($hook, $function, 10, 1);
return $hook;

}

comment:4 programming.has.no.com6 years ago

  • Owner changed from anonymous to ryan

comment:5 ryan6 years ago

  • Resolution set to fixed
  • Status changed from new to closed

(In [8179]) Remove default feed function when adding new function to hook. fixes #6970

Note: See TracTickets for help on using tickets.