So I'm getting the dreaded
Warning: Cannot modify header information - headers already sent
error message when trying to redirect to a success page on my Wordpress plugin.
The issue is, I'm using wp_safe_redirect
as instructed in the Codex like this:
wp_safe_redirect(admin_url("admin.php?page=bv_before_after&msg=4"));
exit;
But what's frustrating, is the files that seem to be having the issue, are both Wordpress core files:
(output started at /var/www/html/wp-includes/formatting.php:4586) in /var/www/html/wp-includes/pluggable.php on line 1228
Lots of tutorials talk about OB_flush(), but there are two sides of every thing I've found so far. There doesn't seem to be anything that seems to explain what's happening in my situation, and why.
I don't think it's a whitespace issue in my plugin file, as I've removed all of that. I'm not too good on creating WP plugins, this is my first or second probably. Any ideas as to what I can do?
EXCERPT: This is a portion from the start of the file, to the end of the function that includes the first redirect
<?php
/*
Plugin Name: BellaVou Before & After Manager
Description: Admin interface for managing Before & After photo sets.
Version: 1.0
Author: Lee Collings
*/
function registration_scripts() {
wp_register_style('style', plugins_url('style.css', __FILE__));
wp_enqueue_style('fontAwesome','https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css');
wp_enqueue_style('style');
}
add_action( 'admin_enqueue_scripts', 'registration_scripts' );
add_action('admin_menu','bv_before_after_menu');
function bv_before_after_menu() {
add_menu_page( 'BV Before & Afters', 'BV Before & Afters', 'manage_options', 'bv_before_after', 'test_init' );
}
function test_init(){
global $wpdb;
$msg = isset($_GET['msg']) ? $_GET['msg'] : '';
$dateFormat = 'd/M/y';
// Edit URL param
$editID = isset($_GET['edit']) ? $_GET['edit'] : '';
$addItem = isset($_GET['add']) ? $_GET['add'] : '';
// SQL call if editID present
if ($editID != '') {
$editItem = $wpdb->get_row("SELECT * FROM wp_before_after WHERE ID = $editID");
}else{
$editItem = null;
}
// Delete Item
if(isset($_POST['deleteItem'])) {
$result = $wpdb->delete( 'wp_before_after', array( 'ID' => $editID ) );
if($result) {
wp_safe_redirect(admin_url("admin.php?page=bv_before_after&msg=4"));
exit;
} else {
$msg = array('danger','The entry could not be deleted');
echo '<div class="notice notice-'.$msg[0].'"><p>'.$msg[1].'</p></div>';
}
}