error using zebra_form in a wordpress plugin - php

I'm trying to use Zebra_form in a metabox for a Wordpress plugin I'm working on.
For simplicity I reduced the code to this:
public function render_metabox( $post ) {
// instantiate a Zebra_Form object
$form = new Zebra_Form('form');
// the label for the "email" field
$form->add('label', 'label_email', 'email', 'Email');
// add the "email" field
$obj = $form->add('text', 'email', '', array('autocomplete' => 'off'));
// auto generate output, labels above form elements
$form->render();
}
But I get the following error when I try to run it:
Warning: Cannot modify header information - headers already sent by (output started at /Users/dp/Documents/SitesDev/wordpress/wp-admin/includes/template.php:1877) in /Users/dp/Documents/SitesDev/wordpress/wp-content/plugins/my-plugin/lib/Zebra/Zebra_Form.php on line 4052
Fatal error: The library tried to store the CSRF token in a cookie but was unable to do so because there was output already sent to the browser. You should either start a session prior to instantiating the library (recommended), have no output (including and tags, as well as any whitespace) sent to the browser prior to instantiating the library, or turn output buffering on in php.ini. in /Users/dp/Documents/SitesDev/wordpress/wp-content/plugins/my-plugin/lib/Zebra/Zebra_Form.php on line 4052
Any clues? Or Zebra_form cannot be used in this way?

I know it has been a while since you posted this question but here is where I came up with
I got the same error as you so I looked in the Zebra_Form.php and commented out line 4041 to line 4068.
It worked after.

you have to put
session_start();

Related

Find the root cause of already sent headers in php when entire class is referenced

fellow members and visitors.
Please imbue me of your knowledge and allow me to be enlightened by your expertise by answering this question or providing me a path of exploration.
My simple mind cannot has exhausted the possibilities it was aware and would really like to begin to understand what to do in such a situation should it arise again.
Everything begins with a :
Warning: session_start(): Cannot send session cache limiter - headers
already sent (output started at
/path/to/site/wp-content/themes/theme_name/inc/general/class-Upbootwp_Walker_Nav_Menu.php:125)
in
/path/to/site/wp-content/plugins/wp-php-console/includes/class-wp-php-console.php
on line 142
Here is the exact ubootwp_Walker_Nav_menu.php class for reference.
I am used to having a precise line number in these kind of error but this time, I get no such indicator (I just get the last line, but there's nothing there except the end of the class.
From my understanding, something in that class instantiate a session and when the Worpdress php console plugin try to do it, it crashes as headers were already sent by that class.
I did add the following
error_reporting(E_ALL);
ini_set('display_errors', 'On');
to get the error statement, as I would previously only get a 'error 500'.
I cannot see anything wrong with that class (no obvious echo, print, ob_start).
Does some of you have any clue on how I would go in trying to find the root cause of it ?
Any insight will be appreciated.
edit:
ubootwp_Walker_Nav_menu.php is a UTF-8 file (No BOM)
No hidden characters or anything before php opening tag.
No php closing tag at the end of the file
No echo,ob_start,print or anything obvious that I can see that would initiate a session.
Thoughts:
The error message explicitly states that the error occurs in the php console plugin (which effectively wants to start a session) because the ubootwp_Walker_Nav_menu.php has created the session. Is it possible that the error message is not accurate or does that unequivocaly means that this class is the error ?
Edit 2:
Actually, I thought it was not relevant but I had another notice before the header sent which was:
Strict Standards: Declaration of Upbootwp_Walker_Nav_Menu::start_lvl() should be compatible with Walker_Nav_Menu::start_lvl(&$output, $depth = 0, $args = Array) in /home3/i8h1o2r7/public_html/dev/wp-content/themes/axial/inc/general/class-Upbootwp_Walker_Nav_Menu.php on line 130
This was very relevant to the problem as this was the cause of the "header sent". I fixed the declaration from the class which was not causing an issue before and it fixed the PHP console plugin "session already created" issue from the initial message.
The session has to start before ANY html has been sent to the browser. You can turn on output buffering but, it's going to cause problems if run on a server without output buffering enabled ... Doesn't WP start a session anyway?
I found the solution to this problem from Magnus comment reference to another similar question. In my particular case, the header were sent by another notice that was caused by a signature mismatch and outputting headers early.
Strict Standards: Declaration of Upbootwp_Walker_Nav_Menu::start_lvl()
should be compatible with Walker_Nav_Menu::start_lvl(&$output, $depth
= 0, $args = Array) in /home3/i8h1o2r7/public_html/dev/wp-content/themes/axial/inc/general/class-Upbootwp_Walker_Nav_Menu.php
on line 130
Once that specific error was fixed, headers were not outputted anymore and the second error (initial question) vanished completely.

How to output JSON for WordPress search page using Relevanssi?

I'm using the WordPress search plugin Relevanssi.
I'm trying to customise my search.php template to output JSON, like so:
<?php
header('Content-Type: application/json');
$results = array();
if (have_posts()):
while (have_posts()):
the_post();
$results[] = array(
'permalink' => get_permalink(),
'title' => get_the_title()
);
endwhile;
endif;
echo json_encode($results);
die();
?>
However, I'm getting an error that headers are already sent.
Warning: Cannot modify header information - headers already sent by
(output started at
/Applications/MAMP/htdocs/NBC/wp-content/plugins/relevanssi/lib/search.php:554)
in /Applications/MAMP/htdocs/NBC/wp-content/themes/NBC/search.php on
line 3
Is there a better way to do this? The reason I need it as JSON is that I want to consume it with JS.
I have done this before on an older site, so perhaps the issues are with the latest version I'm using? 3.5.11
Note that the query DOES return results, but I get these errors on the page too, which causes me to receive invalid JSON data back.
Thanks!
Relevanssi 3.5.11 has a small bug. It doesn't affect the plugin use in any way, but with WP_DEBUG enabled, it throws a notice for an undefined variable. That's why you're seeing that "headers already sent" error.
If you want to keep WP_DEBUG enabled, you need to fix that bug. The fix is simple: add this to lib/search.php on line 382 to define the variable.
$non_post_post_type = NULL;
This fix will be included in the version 3.5.12.
The issue goes away if I turn off WP_DEBUG in wp-config.php
define('WP_DEBUG', false);
It must be something to do with WordPress internals that I don't understand.

Wordpress Feeds: PHP Warning Cannot modify header information - headers already sent

Every time a RSS feed (ie: (www.example.com/feed/) on my wordpress site is called I get a PHP E_Warning:
Cannot modify header information - headers already sent by (output started at /var/www/vhosts/(...)/httpdocs/wp-includes/functions.php:3464)
I did my research and most advice points towards blank spaces at the beginning and end of PHP files. But this is a wordpress core file.
ob_end_flush happens on line 3464:
function wp_ob_end_flush_all() {
$levels = ob_get_level();
for ($i=0; $i<$levels; $i++)
ob_end_flush();
}
I did noticed that this functions.php did not have a closing ?> but given that it's a core wordpress file I would not question this too far :)
This error is comes because when you are using header function and php get output before that function then php show that warning message.
so check that you are not using "echo" or not call any function before the header function that gives output. if so please remove that.
There is a tip for remove that kind of error.
please remove all the blank lines and spaces before the file. and put
ob_start(); at the beginning of the file.
i hope will work for you.
Thanks!!

Cannot regenerate session id zend framework 2?

I used ZFcuser module for my admin panel . when I login into admin panel its returns error like this :
Warning: session_regenerate_id(): Cannot regenerate session id - headers already sent in /home/public_html/dev/vendor/zendframework/zendframework/library/Zend/Session/SessionManager.php on line 260
This works fine in my local windows system but its returns such warning while run in my linux hosting server.
Need help to solve this issue. Thanks in advance.
You can regenerate your session id with the ZF2 SessionManager:
$manager = new Zend\Session\SessionManager;
$manager->regenerateId();
However, your error is likely not caused by this function, but because of something else. Session management (either session_start() or regenerating session ids) is only possible when you haven't used echo or something similar before that piece of code:
As an example, this will fail:
<?php
echo 'Hello';
session_start();
On the other hand, this will just work:
<?php
session_start();
echo 'Hello';
You have to be very careful with opening and closing php tags. If you have any closing php tag in your classes, remove them. If there is blank line after this, it's interpreted as text which is returned in the response and this causing troubles with sessions. This will fail as well:
<?php
// Do some work
?>
<!-- blank line -->
<?php
session_start();
?>
So: check all your code for echo, print etcetera. Also, check for any closing php tag in your source code. If all above things won't help you, post the piece of code where you are regenerating your session id, that might give some more insights.
This error typically appears when you dump some characters to PHP standard output. This results in sending the HTTP headers to web browser, so you cannot add a session header anymore.
To resolve the issue, ensure you do not echo or printf any character before you start the session. Also ensure that your PHP class files do not have closing ?> tags. If you have some characters after the ?> tags in your PHP class files, those characters will be dumped to stdout, breaking your session.
Please check following
protected function _initSession() {
$config = array(
'name' => 'session',
'primary' => 'id',
'modifiedColumn' => 'modified',
'dataColumn' => 'data',
'lifetimeColumn' => 'lifetime',
'lifetime' => 60*60*24*30,
);
Zend_Session::setSaveHandler(new F_Session_SaveHandler_DbTable($config));
}

Headers already sent error in CakePHP app [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
function new_photo()
{
if( !empty($this->data))
{
$this->data['Photo']['showcase_id'] = $this->Session->read('CurrShowcase.id');
$this->data['Photo']['added'] = date("Y-m-d H:i:s");
$this->Showcase->Photo->save($this->data);
$flasher = 'Photo uploaded successfully';
$flasher .= '<br/><img src="' . $this->data['Photo']['thumbnail_url'] . '"/>';
$this->Session->setFlash($flasher);
//$this->redirect(array('action'=>'sc',));
}
}
I have a Showcase Controller in my CakePHP app, and a new photo form to submit new photos. Whenever I uncomment the last line that redirects after the data is saved, I get this error:
Warning (2): Cannot modify header information - headers already
sent by (output started at D:\.....
Even if I get this error, $this->data still gets saved properly in the database. However, if I comment the redirect line as shown above, everything works fine and error-free. I HAVE checked for blank spaces around the tags, so I'm pretty sure it's not that.
Any ideas?
Edit:
commenting out the setFlash statement does not fix the problem.
Change your debug mode to 0 to make sure it's not a notice/warning being generated prior to the redirect. Also, you might want to tighten up your processing section to (be paranoid and) ensure that it's not using invalid indexes, as well as anywhere else throughout the application flow for that action to make sure you're not getting any ouput (if it works when you change debug to 0).
Is there a debug statement somewhere that you're not showing us?
You may be up against an invisible UTF-8 BOM character somewhere. Check your text editor settings whether it saves your files with BOM or without.
I'd check for whitespace in the models. Anyone of them. That was one of the gotchas I hit.
Either this code outputs something to the browser, or you have a whitespace after ?> in the end of the file (or any other included file). The whitespace is sent to the user thus sending http header.
I'm assuming setFlash outputs something to the browser?
If whitespace before or after your <?php ?> tags isn't your issue you might have to try passing 'null' for the 'layout' parameter of setFlash();
i.e.
$this->Session->setFlash($flasher, null);

Categories