0

i wrote a little plugin, who displays a custom form in frontend and store entered data in local DB with connection to MailChimp. With a shortcode.

Now i´m getting an error "headers already sent..", everytime when the post or site be saved with this shortcode. In frontend the plugin works well.

See code and screens below:


register_activation_hook(FILE, 'on_activate');

function on_activate() { global $wpdb;

/// $table_name = $wpdb->prefix . "subscribe_record"; /// $sql = "DROP TABLE IF EXISTS $table_name"; // $wpdb->query($sql); $create_table_query = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}subscribe_record ( id INTEGER NOT NULL AUTO_INCREMENT, surename VARCHAR(100), name VARCHAR(100), email VARCHAR(100), gender VARCHAR(3), street VARCHAR(500), zip VARCHAR(20), city VARCHAR(200), package VARCHAR(6), subscribe INTEGER(11), PRIMARY KEY (id))"; require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); dbDelta($create_table_query);

}

add_action('wp_ajax_insert_subcriber_record', 'insert_subcriber_record'); add_action('wp_ajax_nopriv_insert_subcriber_record', 'insert_subcriber_record');

function insert_subcriber_record() { global $wpdb;

$tablename = $wpdb->prefix . 'subscribe_record';
$surename = isset($_REQUEST['subcriber_surename']) ? $_REQUEST['subcriber_surename'] : '';
$name = isset($_REQUEST['subcriber_name']) ? $_REQUEST['subcriber_name'] : '';
$email = isset($_REQUEST['subcriber_email']) ? $_REQUEST['subcriber_email'] : '';
$gender = isset($_REQUEST['subcriber_gender']) ? $_REQUEST['subcriber_gender'] : 'm';
$street = isset($_REQUEST['subcriber_street']) ? $_REQUEST['subcriber_street'] : '';
$zip = isset($_REQUEST['subcriber_zip']) ? $_REQUEST['subcriber_zip'] : '';
$city = isset($_REQUEST['subcriber_city']) ? $_REQUEST['subcriber_city'] : '';
$subscribe = isset($_REQUEST['subcribe']) ? $_REQUEST['subcribe'] : 0;
$package = isset($_REQUEST['package']) ? $_REQUEST['package'] : 'No';
if($package == 'yes'){
    $package = 'Yes';
}
if ($subscribe) {
    $subscribe = 1;
}
$data = array(
    'surename' => $surename,
    'name' => $name,
    'email' => $email,
    'gender' => $gender,
    'street' => $street,
    'zip' => $zip,
    'city' => $city,
    'package' => $package,
    'subscribe' => $subscribe

);
if(checkSubsriberCount() > 0) {
    $wpdb->insert($tablename, $data);
}

$response = array();
if ($subscribe) {
    $response['data'] = 'yes';
} else {
   $response['data'] = 'no';
}
echo  json_encode($response);
exit;

}

/** Step 2 (from text above). */ add_action('admin_menu', 'subscriber_list_menu');

/** Step 1. */ function subscriber_list_menu() { add_options_page('WaterWipes Export', 'WaterWipes Export', 'manage_options', 'subscriber_list', 'subscriber_list_func'); }

/** Step 3. */

function checkSubsriberCount(){

global $wpdb;

$table_name = $wpdb->prefix . "subscribe_record";

$subscriber = $wpdb->get_results("SELECT * FROM $table_name");
$mailChimpCounter = get_option('mailChimpCounter');

return (int)($mailChimpCounter - count($subscriber));

} function subscriber_list_func() { if (!current_user_can('manage_options')) { wp_die(__('You do not have sufficient permissions to access this page.')); }

global $wpdb;

$table_name = $wpdb->prefix . "subscribe_record";

$subscriber = $wpdb->get_results("SELECT * FROM $table_name");
$mailChimpCounter = get_option('mailChimpCounter');
?>
<div class="container">
    <h2>Kundenliste</h2>
    <br/><br/>

    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <label class="sr-only" for="pwd">Mail Chimp Counter Number:</label>
                <input type="number" value="<?php echo $mailChimpCounter; ?>" class="form-control" id="txtMailChimp">
            </div>
        </div>
        <div class="col-md-2">
            <button type="button" class="btn btn-default" id="updateCounter">Submit</button>
        </div>


    </div>
    <div class="row">
        <div class="col-md-11">
            <table id="example" class="table table-striped table-bordered" style="width:100%">
                <thead>
                    <tr>
                        <th>Surename</th>
                        <th>Name</th>
                        <th>Email</th>
                        <th>Gender</th>
                        <th>Street</th>
                        <th>Zip Code</th>
                        <th>City</th>
                        <th>Pakcage</th>

                    </tr>
                </thead>
                <tbody>
                    <?php
                    foreach ($subscriber as $list) {

                        echo '<tr>';
                        echo '<td>' . $list->surename . '</td>';
                        echo '<td>' . $list->name . '</td>';
                        echo '<td>' . $list->email . '</td>';
                        if ($list->gender == 'm') {
                            echo '<td>Male</td>';
                        } else {
                            echo '<td>Female</td>';
                        }

                        echo '<td>' . $list->street . '</td>';
                        echo '<td>' . $list->zip . '</td>';
                        echo '<td>' . $list->city . '</td>';
                        echo '<td>' . $list->package . '</td>';
                        echo '</tr>';
                    }
                    ?>
                </tbody>
                <tfoot>
                    <tr>
                        <th>Surename</th>
                        <th>Name</th>
                        <th>Email</th>
                        <th>Gender</th>
                        <th>Street</th>
                        <th>Zip Code</th>
                        <th>City</th>
                        <th>Pakcage</th>
                    </tr>
                </tfoot>
            </table>
        </div>


    </div>




</div>
<link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' type='text/css' media='all' />
<link rel='stylesheet' href='https://cdn.datatables.net/1.10.16/css/dataTables.bootstrap.min.css' type='text/css' media='all' />
<link rel='stylesheet' href='https://cdn.datatables.net/buttons/1.5.1/css/buttons.bootstrap.min.css' type='text/css' media='all' />
<script type='text/javascript' src='https://code.jquery.com/jquery-3.3.1.js'></script>
<script type='text/javascript' src='https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js'></script>
<script type='text/javascript' src='https://cdn.datatables.net/1.10.16/js/dataTables.bootstrap.min.js'></script>
<script type='text/javascript' src='https://cdn.datatables.net/buttons/1.5.1/js/dataTables.buttons.min.js'></script>
<script type='text/javascript' src='https://cdn.datatables.net/buttons/1.5.1/js/buttons.bootstrap.min.js'></script>
<script type='text/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js'></script>
<script type='text/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/pdfmake.min.js'></script>
<script type='text/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/vfs_fonts.js'></script>
<script type='text/javascript' src='https://cdn.datatables.net/buttons/1.5.1/js/buttons.html5.min.js'></script>
<script type='text/javascript' src='https://cdn.datatables.net/buttons/1.5.1/js/buttons.print.min.js'></script>
<script type='text/javascript' src='https://cdn.datatables.net/buttons/1.5.1/js/buttons.colVis.min.js'></script>

<script>
    $('body').on('click', '#updateCounter', function () {
        var get_number = $('#txtMailChimp').val();
        var r = confirm("Möchten Sie den Zählerstand speichern?");
        if (r == true) {
            var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';

            jQuery.ajax({
                url: ajaxurl,
                data: {number: get_number, action: 'saveMailChimpnumber'},
                method: 'post',
                success: function (response) {

                    alert('Neuer Zählerstand gespeichert!')

                },
                cache: false,
                /// contentType: false,
                /// processData: false
            });
        }

    });
    $(document).ready(function () {
        var table = $('#example').DataTable({
            lengthChange: false,
            buttons: ['excel', 'pdf', 'colvis']
        });

        table.buttons().container()
                .appendTo('#example_wrapper .col-sm-6:eq(0)');
    });
</script>

<?php

} add_action('wp_ajax_saveMailChimpnumber', 'saveMailChimpnumber'); add_action('wp_ajax_nopriv_saveMailChimpnumber', 'saveMailChimpnumber'); function saveMailChimpnumber() { $value = isset($_REQUEST['number']) ? $_REQUEST['number'] : 1000; update_option('mailChimpCounter', $value); exit; } function subcriberList_shortCode($atts) { $a = shortcode_atts(array( 'formid' => ''), $atts); $adminUrl = admin_url('admin-ajax.php'); ?> WaterWipes Test jQuery(function ($) { $('body').on('click', '#subcriberSubmitBtn', function () { $('.subcriber-response').html(''); var subcriber_surename = $('#subcriber_surename').val(); var subcriber_name = $('#subcriber_name').val(); var subcriber_street = $('#subcriber_street').val(); var subcriber_zip = $('#subcriber_zip').val(); var subcriber_city = $('#subcriber_city').val(); var subcriber_email = $('#subcriber_email').val(); var error = false; if (subcriber_surename) { $('#subcriber_surename').css('border-color', '#ccc') } else { error = true; $('#subcriber_surename').css('border-color', '#c91d2e') } if (subcriber_name) { $('#subcriber_name').css('border-color', '#ccc') } else { error = true; $('#subcriber_name').css('border-color', '#c91d2e') } if (subcriber_street) { $('#subcriber_street').css('border-color', '#ccc') } else { error = true; $('#subcriber_street').css('border-color', '#c91d2e') } if (subcriber_zip) { $('#subcriber_zip').css('border-color', '#ccc') } else { error = true; $('#subcriber_zip').css('border-color', '#c91d2e') } if (subcriber_city) { $('#subcriber_city').css('border-color', '#ccc') } else { error = true; $('#subcriber_city').css('border-color', '#c91d2e') } if(jQuery("#subcribe").is(":checked")){ if (subcriber_email) { if (!validateEmail(subcriber_email)) { $('#subcriber_email').css('border-color', '#c91d2e') error = true; }else{ $('#subcriber_email').css('border-color', '#ccc') } } else { error = true; $('#subcriber_email').css('border-color', '#c91d2e') } }else{ $('#subcriber_email').css('border-color', '#ccc') } if (error == true) { $('.subcriber-response').html('Bitte alle erforderlichen Felder ausfüllen.'); return false; } else { var elementT = document.getElementById("subcriberForm"); var ajaxurl = ''; var formdata = new FormData(elementT); jQuery.ajax({ url: ajaxurl, data: formdata, method: 'POST', success: function (response) { if (response) { $('.subcriber-response').html('Vielen Dank für deine Nachricht, du erhältst die WaterWipes Probepackung per Post!'); var obj = jQuery.parseJSON(response); console.log(obj.data); $('#subcriber_surename').val(''); $('#subcriber_name').val(''); $('#subcriber_street').val(''); $('#subcriber_zip').val(''); $('#subcriber_city').val(''); $('#subcriber_email').val(''); if (obj.data == 'yes') { $("#customSubcriber").find("input[name='FNAME']").val(subcriber_surename); $("#customSubcriber").find("input[name='LNAME']").val(subcriber_name); $("#customSubcriber").find("input[name='ADDRESS[addr1]']").val(subcriber_street); $("#customSubcriber").find("input[name='ADDRESS[city]']").val(subcriber_city); $("#customSubcriber").find("input[name='ADDRESS[zip]']").val(subcriber_zip); $("#customSubcriber").find("input[name='EMAIL']").val(subcriber_email); $("#customSubcriber form").submit(); } //// location.reload(); } else { alert('Something Went Wrong, Record Not Updated'); } }, cache: false, contentType: false, processData: false }); } }); }); function validateEmail($email) { var emailReg = /^([\w-.]+@([\w-]+.)+[\w-]{2,4})?$/; return emailReg.test($email); }

Hier kannst du noch einen Text aussuchen :) </p> -->

Ich möchte WaterWipes testen und freue mich über eine Probepackung

Herr Frau

                    <p><input type="email" id="subcriber_email" name="subcriber_email" placeholder="E-Mail Adresse" required=""></p>
                    <?php if(checkSubsriberCount() > 0){ ?>
                    <p><input type="checkbox" name="subcribe" id="subcribe" value="yes"><label for="subcribe">Ich möchte den Spinoggelfritz-Newsletter abonnieren</label></p>
                <?php  }else{ ?>
                     <input type="hidden" name="subcribe" value="yes" >
              <?php  } ?>
                    <p><input style="cursor: pointer;" type="button" id="subcriberSubmitBtn" value="Absenden >"></p>
                </ul>
                 <div class="subcriber-response" style="padding-bottom: 4px;padding-top: 10px;"></div>
            </div>
        </div>
    </form>
    <!-- / MailChimp for WordPress Plugin -->
</div>
<div style="display: none;" id="customSubcriber">
    <?php
    $formID = $a['formid'];
    echo do_shortcode("[mc4wp_form id='".$formID."']");
    ?>
</div>

<?php

} add_shortcode('subcribercustomform', 'subcriberList_shortCode');

enter image description here

Thank you for any suggestions.

rgrds Johannes

0

Well, usually the error messages will tell you what line to look at ... look at the part "output started at" to see the culprit.

But, often the cause is extra space characters at the beginning (before the <?php) or at the end of a file (if you use a ?> , which is not recommended, just because of that problem with headers already sent). Those problems might be in your plugin, or in another one.

You might consider the load sequence to your plugin. Perhaps it is not getting loaded at the point in time you think it is. (This is assuming that you rplugin meets the standard plugin structure.)

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.