Wordpress函数检查表是否为空

时间:2022-09-15 16:29:57

I am running a WP blog on MediaTemple WP Hosting. They provide a built-in function to integrate Google Analytics code. As I need to make some changes to the GA tracking code in order to anonymize the users IP, I want to disable that function and replace it with custom one.

我在MediaTemple WP Hosting上运行WP博客。它们提供了集成Google Analytics代码的内置功能。由于我需要对GA跟踪代码进行一些更改以便对用户IP进行匿名化,因此我想禁用该功能并将其替换为自定义功能。

Here is the MT built-in function:

这是MT内置功能:

    public function add_ga_tracking_snippet_to_head()
{
    if ( get_option( $this->plugin->name . '_web_property_id_head' ) !== false ) {
        $web_property_id = get_option( $this->plugin->name . '_web_property_id_head' );
?>
<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', '<?php echo $web_property_id ?>', 'auto');
  ga('send', 'pageview');
</script>
<?php
    }
}

and here is the screenshot of the db table where GA ID is stored: Wordpress函数检查表是否为空

以下是存储GA ID的db表的屏幕截图:

Now, if I delete the ID inside the option_value table, the function should not print any <script>...</script>, because of the conditional statement if ( get_option( $this->plugin->name . '_web_property_id_head' ) !== false ) but it doesn't work this way.

现在,如果我删除了option_value表中的ID,该函数不应该打印任何

Here's what I get instead:

这是我得到的东西:

Wordpress函数检查表是否为空

P.S. I've even tried to alter the function with isset and !empty with no success. Where am I wrong?

附:我甚至试图用isset和!空来改变函数,但没有成功。我哪里错了?

2 个解决方案

#1


The line get_option( $this->plugin->name . '_web_property_id_head' ) must be false for that conditional to not execute.

对于不执行的条件,行get_option($ this-> plugin-> name。'_ web_property_id_head')必须为false。

Empty strings and NULL in MySQL aren't boolean values so they're never going to !== false.

MySQL中的空字符串和NULL不是布尔值,因此它们永远不会发生!== false。

#2


Unfortunately, MT core plugin needs to be revised in order to work fine. Here's the fix:

不幸的是,MT核心插件需要修改才能正常工作。这是修复:

if ( get_option( $this->plugin->name . '_web_property_id_head' ) !== '' ) {

I've opened a ticket on MT support where I asked them to officially fix that function.

我在MT支持上开了一张票,我要求他们正式修复这个功能。

#1


The line get_option( $this->plugin->name . '_web_property_id_head' ) must be false for that conditional to not execute.

对于不执行的条件,行get_option($ this-> plugin-> name。'_ web_property_id_head')必须为false。

Empty strings and NULL in MySQL aren't boolean values so they're never going to !== false.

MySQL中的空字符串和NULL不是布尔值,因此它们永远不会发生!== false。

#2


Unfortunately, MT core plugin needs to be revised in order to work fine. Here's the fix:

不幸的是,MT核心插件需要修改才能正常工作。这是修复:

if ( get_option( $this->plugin->name . '_web_property_id_head' ) !== '' ) {

I've opened a ticket on MT support where I asked them to officially fix that function.

我在MT支持上开了一张票,我要求他们正式修复这个功能。