<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Global Variable on Netsensei</title>
    <link>https://www.netsensei.be/tags/global-variable/</link>
    <description>Recent content in Global Variable on Netsensei</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>nl-NL</language>
    <managingEditor>matthias@netsensei.nl (Matthias Vandermaesen)</managingEditor>
    <webMaster>matthias@netsensei.nl (Matthias Vandermaesen)</webMaster>
    <lastBuildDate>Thu, 12 Jun 2008 16:43:37 +0000</lastBuildDate><atom:link href="https://www.netsensei.be/tags/global-variable/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>WP Mollom and WP OpenID</title>
      <link>https://www.netsensei.be/2008/06/12/wp-mollom-and-wp-openid/</link>
      <pubDate>Thu, 12 Jun 2008 16:43:37 +0000</pubDate>
      <author>matthias@netsensei.nl (Matthias Vandermaesen)</author>
      <guid>https://www.netsensei.be/2008/06/12/wp-mollom-and-wp-openid/</guid>
      <description>&lt;p&gt;These two weren’t the best friends over the past couple of weeks. Since
someone notified me they weren’t compatible, it took some time to figure
out what was going wrong. My initial suspects was an icky way of dealing with
the action hooks. Either by my plugin or WP OpenID. But after extensive testing,
I concluded that the order in which the action hooks call the different plugin
functions, wasn’t problem.&lt;/p&gt;
&lt;p&gt;I identified the problem as the comment data getting lost somewhere along the
way. I tested the OpenID plugin and the transition to the Mollom plugin. In the
end, I could narrow the problem down to odd behaviour of global variables in
WordPress. Let’s take a look at this bit of code:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;&amp;lt;br&amp;gt; function dosomething($ds_comment) {&amp;lt;br&amp;gt; global $ds_comment;&amp;lt;br&amp;gt; print_r($ds_comment);&amp;lt;br&amp;gt; return $ds_comment;&amp;lt;br&amp;gt; }&amp;lt;br&amp;gt; add_action(&#39;preprocess_comment&#39;, &#39;dosomething&#39;);&amp;lt;br&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;For brevity’s sake, I ommitted the obligatory WordPress plugin header. But
if you add it, put this bit in a seperate file, upload it to your plugins/
folder and activate. Now you can test if yourself. The idea is that the array
containing the commentdata is shown in your browser just before putting it in
the database (notice that your browser doesn’t redirect to the original
page, but that’s not the issue here). In reality, you’ll get a
blanco page. Meaning the array $ds_comment is in fact empty. Further on,
you’ll just pass empty variables and in the end save an empty record to
your database. The comment got lost into cyberoblivion. Not very nice.&lt;/p&gt;
&lt;p&gt;Now. Just comment out or remove the &lt;code&gt;global $ds_comment;&lt;/code&gt; bit and try again.
Now, if you submit a new comment, the data will be output to the browser nicely.&lt;/p&gt;
&lt;p&gt;Conclusion: If you make the very same variable that was passed as an argument
through the function, global, the data just gets lost. Very odd. Now, if you
create a new, empty, global variable within the function and assign the data
from $ds_comment to it, there is no problem whatsoever.&lt;/p&gt;
&lt;p&gt;I wonder how this could happen…&lt;/p&gt;
&lt;p&gt;Ow. Making a lot of variables global, especially those with sensitive data, is
not really best practice. There are more gracious ways of passing data around
like OO programming design or paying attention to correct function reuse. In a
future incarnation, I’ll try to reduce the amount of globals I use. For
now, I just want the damn thing to behave like it should. 😉&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>