Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+1 vote
653 views
in Plugins by

I am making a plugin for Q2A but due to some reason I am getting these warnings. 


Warning: Cannot modify header information - headers already sent by (output started at /home/name/public_html/test/qa-plugin/welcome-widget/qa-welcome-widget.php:2) in /home/name/public_html/test/qa-include/qa-page.php on line 767

Warning: Cannot modify header information - headers already sent by (output started at /home/name/public_html/test/qa-plugin/welcome-widget/qa-welcome-widget.php:2) in /home/name/public_html/test/qa-include/qa-page.php on line 220

Warning: Cannot modify header information - headers already sent by (output started at /home/name/public_html/test/qa-plugin/welcome-widget/qa-welcome-widget.php:2) in /home/name/public_html/test/qa-include/qa-page.php on line 377

The plugin I am making is available at Github here. If you can tell what's the issue that's causing these warning then, it would be great help. Although I have searched about it but, I am still unable to point out the mistake I am doing.

Q2A version: 1.7.4

1 Answer

+4 votes
by

At first sight you seem to be generating output inside a PHP class file:

https://github.com/gurjyot/Welcome-Widget/blob/5c0e86ea9f7414962add7210a5a3799ff6f4571a/qa-welcome-widget.php#L1

That should be avoided (always).

If you want to add HTML when you output a widget then you should use the $themeobject parameter or pass by reference the &$qa_content parameter. The problem is that widgets are output AFTER the theme has processed the theme head() function. That means if you edit the content in the widget the content array will be updated but that HTML won't be output.

There isn't much to do here unless the widget loading receives a kind of initialization($qa_content) function that would run before the output_widget function, just after the theme initialization. However, the core doesn't have that (and most likely it should).

So widgets can't solve the issue on their own. They need a layer. As layers literally extend the theme they can hook (override) any method and edit the content array. I usually prefer the latter. This means you could just add an initialize() function to the layer and change $this->content['css_src']) and $this->content['script'] accordingly. Instead of 'script' you can also use 'body_footer' for the JS.

The problem now is that the new HTML is output all the time. You can add some conditions to try to avoid this by processing $this->content['widgets'] in the layer but might complicate things too much. Probably, it is better (easier) to output the CSS all the time and, in the case of the JS, add it to the footer so that you can add it in the output_widget function directly.

by
Thanks a lot for this explained answer Gabriel. Sometimes you give better answers than I can find of SO. ;)
But, this also means that this plugin is going to get much more tough for me to crack.
...