WordPress.org

Make WordPress Core

Opened 2 months ago

Closed 2 months ago

Last modified 2 months ago

#52725 closed defect (bug) (invalid)

Redirect in admin submenu page: Warning: Cannot modify header information

Reported by: mitfi Owned by:
Milestone: Priority: normal
Severity: normal Version: 5.7
Component: Administration Keywords:
Focuses: administration Cc:

Description (last modified by SergeyBiryukov)

Dear WordPress Development Team,

only in WP5.7 ( and NOT in WP5.6.2), I receive the following warnings:

Warning: Cannot modify header information - headers already sent by (output started at /.../wp-admin/includes/template.php:2526) in /.../wp-includes/pluggable.php on line 1329

Warning: Cannot modify header information - headers already sent by (output started at /.../wp-admin/includes/template.php:2526) in /.../wp-includes/pluggable.php on line 1332

Background:
In my plugin, I define the following submenu entries:

function add_emergency_management_menu_items()
{
  $mfem_admin_page = add_submenu_page(
                       'users.php', // related menu
                       'Emergency Management', // page title
                       'EM - Emergency Management', // submenu title
                       'manage_options', // required capability (or role)
                       'mfem-emergency-management', // page slug
                       'mfem_emergency_management_main' // callback function, the main plugin function
                     );
  $mfem_title = __('Reset SALTs & Sessions','emergency-management');
  add_submenu_page(
    'users.php', // related menu
    $mfem_title, // page title
    'EM - '.$mfem_title, // submenu title
    'manage_options', // required capability (or role)
    'mfem-warning-saltsform-handler-pre', // page slug (here, we do not have a page but just a function...)
    'mfem_warning_saltsform_handler_pre' // callback function (pre to call mfem_warning_saltsform_handler)
  );
  
  add_action( 'load-'.$mfem_admin_page, 'mfem_add_help' ); // Add the help tabs callback function...
}
add_action('admin_menu','add_emergency_management_menu_items');

Any call to mfem-emergency-management (to the related main admin page) and further calls from there to other functions with redirects back to "admin.php?page=mfem-emergency-management" are working fine.

However, a call to mfem-warning-saltsform-handler-pre, with some activity done there (regardless whether I do a screen output or not) and a redirect to "admin.php?page=mfem-emergency-management", will fail with the above warning as soon as the redirect is executed.

This behavior is not to be observed with WP5.6.2.

Thanks for looking into that.

Best regards

Michael

Attachments (1)

52725-5.6.2.gif (1.7 MB) - added by hellofromTonya 2 months ago.
"Cannot modify header information" warning with Emergency Management plugin running on WordPress 5.6.2

Download all attachments as: .zip

Change History (10)

This ticket was mentioned in Slack in #core by hellofromtonya. View the logs.


2 months ago

#2 follow-ups: @hellofromTonya
2 months ago

  • Keywords reporter-feedback added; dev-feedback removed

Hello @mitfi,

Welcome to WordPress Core Trac. Thank you for your ticket!

However, a call to mfem-warning-saltsform-handler-pre, with some activity done there (regardless whether I do a screen output or not) and a redirect to "admin.php?page=mfem-emergency-management", will fail with the above warning as soon as the redirect is executed.

  • Can you share the redirect code in the mfem_warning_saltsform_handler_pre callback?

I attempted to reproduce the problem using the code in the description as well as adding this redirect in that callback:

<?php
function mfem_warning_saltsform_handler_pre() {
        wp_redirect( admin_url( 'users.php?page=mfem-emergency-management' ) );
}

Navigating to the sub page, the Warning: Cannot modify header information appeared on both WP 5.6.2 and 5.7 RC2.

  • Also, can you share the enter warning back grabbing a screenshot?

Why? The callstack in the PHP warning information can help with further investigation.

#3 @SergeyBiryukov
2 months ago

  • Component changed from General to Administration
  • Description modified (diff)

#4 follow-up: @SergeyBiryukov
2 months ago

Warning: Cannot modify header information - headers already sent by (output started at /.../wp-admin/includes/template.php:2526) in /.../wp-includes/pluggable.php on line 1329
Warning: Cannot modify header information - headers already sent by (output started at /.../wp-admin/includes/template.php:2526) in /.../wp-includes/pluggable.php on line 1332

Just noting that these warnings are caused by calling wp_redirect() after the output has already started from the _wp_admin_html_begin() function.

As noted above, this does not seems specific to WordPress 5.7. A solution would be to perform the redirects as soon as possible, before any output.

#5 in reply to: ↑ 2 @mitfi
2 months ago

Replying to hellofromTonya:

Hello @hellofromtonya,

thanks for your response.

I can reassure that my plug-in works under 5.6.2 but not under 5.7.
There is no (not willingly) output generated prior redirect in this case.

The code „Emergency Management“ is available in trac under https://plugins.trac.wordpress.org/browser/emergency-management/

Best regards

Michael

Hello @mitfi,

Welcome to WordPress Core Trac. Thank you for your ticket!

However, a call to mfem-warning-saltsform-handler-pre, with some activity done there (regardless whether I do a screen output or not) and a redirect to "admin.php?page=mfem-emergency-management", will fail with the above warning as soon as the redirect is executed.

  • Can you share the redirect code in the mfem_warning_saltsform_handler_pre callback?

I attempted to reproduce the problem using the code in the description as well as adding this redirect in that callback:

<?php
function mfem_warning_saltsform_handler_pre() {
        wp_redirect( admin_url( 'users.php?page=mfem-emergency-management' ) );
}

Navigating to the sub page, the Warning: Cannot modify header information appeared on both WP 5.6.2 and 5.7 RC2.

  • Also, can you share the enter warning back grabbing a screenshot?

Why? The callstack in the PHP warning information can help with further investigation.

#6 in reply to: ↑ 4 @mitfi
2 months ago

Replying to SergeyBiryukov:

Dear @SergeyBiryukov,

as stated already, my code works with 5.6.2.
There is no willingly output generated prior redirect in this case.
On the other side, with a start from my main admin page, which obviously has an output, a redirect back to itself is not generating any issue.

Please allow one remark: you all are doing a great job, providing such a wonderful environment for generating online sites.
Thanks for all your efforts!

Best regards

Michael

Warning: Cannot modify header information - headers already sent by (output started at /.../wp-admin/includes/template.php:2526) in /.../wp-includes/pluggable.php on line 1329
Warning: Cannot modify header information - headers already sent by (output started at /.../wp-admin/includes/template.php:2526) in /.../wp-includes/pluggable.php on line 1332

Just noting that these warnings are caused by calling wp_redirect() after the output has already started from the _wp_admin_html_begin() function.

As noted above, this does not seems specific to WordPress 5.7. A solution would be to perform the redirects as soon as possible, before any output.

#7 in reply to: ↑ 2 @mitfi
2 months ago

Replying to hellofromTonya:

Hello @hellofromtonya,

one additional remark;

Even in 5.7, my code works when called from a function/form in my plug-in admin page. It only fails, when called from the sub-menu.

In my ticket, I provided 100% of the warning texts that appeared on the screen (I only omitted the path details).
Where could I find supplemental information regarding the callstack, if any?

Kind regards

Michael

Hello @mitfi,

Welcome to WordPress Core Trac. Thank you for your ticket!

However, a call to mfem-warning-saltsform-handler-pre, with some activity done there (regardless whether I do a screen output or not) and a redirect to "admin.php?page=mfem-emergency-management", will fail with the above warning as soon as the redirect is executed.

  • Can you share the redirect code in the mfem_warning_saltsform_handler_pre callback?

I attempted to reproduce the problem using the code in the description as well as adding this redirect in that callback:

<?php
function mfem_warning_saltsform_handler_pre() {
        wp_redirect( admin_url( 'users.php?page=mfem-emergency-management' ) );
}

Navigating to the sub page, the Warning: Cannot modify header information appeared on both WP 5.6.2 and 5.7 RC2.

  • Also, can you share the enter warning back grabbing a screenshot?

Why? The callstack in the PHP warning information can help with further investigation.

@hellofromTonya
2 months ago

"Cannot modify header information" warning with Emergency Management plugin running on WordPress 5.6.2

#8 @hellofromTonya
2 months ago

  • Keywords reporter-feedback removed
  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

Hello @mitfi,

Thank you for providing more information including a link to your plugin that has the problem.

The above gif shows the Emergency Management plugin running on WordPress 5.6.2 and PHP 5.6. Notice the "Warning: Cannot modify header information - headers already sent" PHP warning when navigating to Users > EM - Reset SALTs & Sessions submenu.

Why is this warning appearing?

tl;dr

The plugin is attempting to redirect after web page's header and HTML output have already started being sent out to the browser. It's too late to do a redirect.

Longer explanation:

The callback that is registered to the `add_submenu_page`'s function argument is used to output that menu's admin page.

$function
(callable) (Optional) The function to be called to output the content for this page.

By the time the callback runs, the admin area's HTML output around it has already been sent outputted. What admin content? The admin top and sidebar menus. Then the specific menu's content area is rendered by the callback registered to add_submenu_page.

In the plugin, mfem_warning_saltsform_handler_pre is the registered callback to render the submenu's page content. That callback then invokes mfem_warning_saltsform_handler which then invokes wp_redirect. It's too late to do a wp_redirect here because the admin page's header and HTML have already started being outputted for the browser.

The problem experienced is not due to WordPress 5.7 or 5.6.2, but rather because of when the redirect is happening in the plugin.

Closing this ticket as it's not a WordPress core issue. For further help on how to implement the redirect logic, please try the support forums: https://wordpress.org/support/forums/

#9 @hellofromTonya
2 months ago

  • Summary changed from WP5.7-RC2-50494 Warning: Cannot modify header information to Redirect in admin submenu page: Warning: Cannot modify header information
Note: See TracTickets for help on using tickets.