1

In my plugin, I have this statement

add_filter( 'comment_edit_redirect', 'mcd_return_link');

and this function

function mcd_return_link {
   return "edit-comments.php";
}

Inside the comments.php (core file), is this section of code (within the section to edit comments, around line 310) (version 4.720

$location = ( empty( $_POST['referredby'] ) ? "edit-comments.php?p=$comment_post_id" : $_POST['referredby'] ) . '#comment-' . $comment_id;

/**
 * Filters the URI the user is redirected to after editing a comment in the admin.
 *
 * @since 2.1.0
 *
 * @param string $location The URI the user will be redirected to.
 * @param int $comment_id The ID of the comment being edited.
 */
$location = apply_filters( 'comment_edit_redirect', $location, $comment_id );
wp_redirect( $location );

The intent of the add_filter is to change the $location value in the comments.php file to return back to the comment lists screen (in admin).

So my plugin uses a shortcode that is entered on a page

  • the shortcode creates a list of comments, each comment with a link to that comment's edit screen (it does)
  • have links to the edit comments screen for a specific comment (it does)
  • clicking on that link gets me to the comment's edit screen (it does)
  • clicking on the 'update' button should return me to the edit-comments.php page (it doesn't).

To check if the filter is being applied, on my test system, I added code before and after the apply_filters line in core comments.php to echo the $location value.

Both values (before and after) show the calling page (my page that displays comments with edit links). It does not redirect to the 'edit-comments.php' page, so it appears that the filter is not being applied.

I have tried changing the priority of the add_filter to 7 and 15, with no effect.

I have also dumped the $wp_filter global variable to ensure that the filter was recognized (it was).

Why is the filter not being applied?

** (Edited 6 Feb after suggested answers) **

I added an echo statement and a wp_die() inside the filter function. I also did further testing of this issue on my test multisite.

  • The shortcode that the plugin uses is on a page on the main (site 0) subsite of the multisite test system.
  • links on all comments go to the editing url of the comment
  • any comments on the site 0 subsite (where the page is that displays the list of comments of all sites) bring you to the comment edit screen, and then "update" will return you to the page defined by the filter (as intended).
  • any comment links from site-1 or other non-site-0 sites bring you to the comment edit screee, and then "update" will reload the calling page (the comment list page (not as intended)
  • with the wp_die() inserted into the filter function (the function that sets up the desired return page, overriding the $location setting in comments.php), the site-0 editing/update process shows the 'wp_die' message. The site-1 or site-2 editing/update process does not show the 'wp_die' message.

From this, I conclude that for some reason, the add-filter is not called by any link to a comment on site-1 or site-2, but works if called from links to a comment on site-0. Again, the page that displays the comments is on site-0.

So, why doesn't the add-filter work on any link that goes to the comment editing screen for site-1 or site-2, when it works properly on site-0?

  • The following things should be questioned: Could it be, that you added the filter too late? Could it be, that you added the filter from where you added the shortcode? Could it be, that your POST-target link does not have the filter that you found in core? – kaiser Feb 8 '17 at 15:09
1

A reminder, try to avoid this kind of add_filter;

add_filter( 'comment_edit_redirect', 'mcd_return_link');

try to always set the number of params, else default is one.

add_filter( 'comment_edit_redirect', 'mcd_return_link', 10 , 2);
  • Interesting. It worked for me without the parameters. – Nathan Johnson Feb 5 '17 at 17:31
  • @NathanJohnson, good to know, I haven't actually tested, but just provided this little feedback. In this case, it may not be needed, but many times I saw problems when not using the full add_filter with the number of the parameters at the end. – prosti Feb 5 '17 at 17:42
  • See additional information/test results added in a 6 Feb edit of the question. Thanks for your comments, but still puzzled.. – Rick Hellewell Feb 6 '17 at 20:25
  • There are many plugins that do this trick can you check ktai-style and also check this : wordpress.stackexchange.com/questions/82317/… – prosti Feb 6 '17 at 22:21
1

Why is the filter not being applied?

Hard to say. Try adding the following code and then try editing a comment. This is basically the same thing you have already, but using an anonymous function with the two parameters.

add_filter( 'comment_edit_redirect', function( $location, $comment_id ) {
  wp_die( 'I\'m dead.' );
}, 10, 2 );

I tried, and got the expected result or WordPress dying.

When I change wp_die( 'I'm dead.' ); to return 'edit-comments.php';, I get redirected to the edit comments page after editing a comment, as expected.

If the callback is getting called correctly (the wp_die test) and it still appears that your filter isn't being applied, then there must be some other plugin/theme functionality that is interfering. Try changing the priority to something really big like 999999. If that doesn't work, try deactivating all other plugins while using a default theme.

  • See additional information/test results added in a 6 Feb edit of the question. Thanks for your thoughts, but still puzzled. – Rick Hellewell Feb 6 '17 at 20:24
0

An answer that brings up another question...

I had a sudden epiphany ... if the plugin is not active on site-1 or site-2, and you click on a link to a comment for site-2, you are taken to the comment editing screen.

But, since the plugin is not active on site-1/site-2 (even though 'network activated' in the multisite admin), the shortcode doesn't work on a site-1/2 page, and therefore the add_filter isn't executed.

So there must be some 'filter-of-the-filter' that only runs the add_filter if for comment links if the plugin is enabled on the site that 'has' the comment.

But ... this doesn't explain why a very similar plugin I have (that shows all media on all sites, called 'Multisite Media Display') allows you to:

  • run the page with the shortcode on site-0
  • display all media for all subsites
  • allows you to edit any media on any subsite
  • clicking the 'x' button on the Media edit screen to get back to the media list will show you the Media 'list' screen.

I suspect that the 'x' on Media Edit does not have code to specify what page you want to return to (the $location value in comments.php around line 310).

But, why doesn't the comment editor 'see' the add_filter command for a site-1 when the shortcode is run on site-0?

So, an answer that brings up another question....

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.