0

I installed a new Wordpress theme today and when I try to post anything I get the following error;

“Notice: Undefined index: action in /home/content/00/00000000/html/sites/website.com/wp-content/themes/bedford/inc/bedford-metaboxes.php on line 601

Warning: Cannot modify header information – headers already sent by (output started at /home/content/00/00000000/html/sites/website.com/wp-content/themes/bedford/inc/bedford-metaboxes.php:601) in /home/content/00/00000000/html/sites/website.com/wp-includes/pluggable.php on line 1228

The code from around 601 is:

add_action( 'admin_init', 'bedford_cmb1_to_cmb2_post_meta' );
function bedford_cmb1_to_cmb2_post_meta(){
    global $pagenow;
    // If screen is not single post edit do nothing.
    if ( 'post.php' === $pagenow && 'edit' === $_GET['action'] && is_numeric($_GET['post'])) {
        $_rpit_meta_fields_repeat_group = get_post_meta( $_GET['post'], '_rpit_meta_fields_repeat_group', true);
        // Remove empty _rpit_meta_fields_repeat_group post meta if exist.
        if( is_array($_rpit_meta_fields_repeat_group) && empty($_rpit_meta_fields_repeat_group) ) {
            print_r('empty array found');
            delete_post_meta($_GET['post'], '_rpit_meta_fields_repeat_group');
        }
    }
}

If anyone could help it would be greatly appreciated.

8
  • Theme author would be a good place to start. Have you deactivated plugins to isolate the theme?
    – jdm2112
    Mar 16, 2016 at 19:39
  • That's a notice. Although it is best practice to eliminate all notices, this is not always possible, and so they are normally turned off for production sites. You need to adjust your error reporting.
    – vancoder
    Mar 16, 2016 at 19:44
  • @vancoder I don't think instructing beginners to ignore errors is a good advice, especially not if a variable is undefined, as it can lead to other, visible errors down the line.
    – benomatis
    Mar 16, 2016 at 19:47
  • @webeno as this is a third party theme, the code cannot be modified directly unless the OP is prepared to make the same modification every time the theme is updated, which is impractical. Many - if not most - third party themes will throw notices. So - either accept that, and ignore the notices, or find a new theme.
    – vancoder
    Mar 16, 2016 at 19:51
  • @vancoder what about removing the "faulty" function and adding the correct one in the child theme...?
    – benomatis
    Mar 16, 2016 at 19:56

1 Answer 1

0

The Cannot modify header error is actually caused by the first error, as the error is being printed on the page invoking a header, therefore any code coming after that trying to modify the header will produce the Cannot modify header error.

As to the Undefined index: action error, apparently the theme author assumes that the action parameter of the superglobal $_GET will always be set on the post page (ie. the URL should have ...post.php?action=something at all times), which appears to be true in most cases, but a smart coder will always prepare for the worst, and check the existence of variables (though it would be interesting to know the full URL you see displayed when you get this error).

To avoid this, they'd need to check whether it exists at all using isset($_GET['action']) (alternatively array_key_exists('action', $_GET), and do the same on $_GET['post'], also assumed to always be set, so the full if condition would look something like this:

// ...
if ( 'post.php' === $pagenow && isset($_GET['action']) && 'edit' === $_GET['action'] && isset($_GET['post']) && is_numeric($_GET['post'])) {
    // ...
}

You have several ways to introduce this solution:

  1. The recommended option would be to contact the theme owner and ask them to address this. While waiting, you could also do...

  2. If you decide to update the original theme, and later on want to upgrade the theme, prepare yourself having to do this again.

  3. Alternatively you could remove the function (remove_action) in your functions.php in your child theme (assuming you're using one) and adding the correct one to replace it using the same hook that was used in add_action of the original theme (in this case it is 'bedford_cmb1_to_cmb2_post_meta').

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.