Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Wordpress简单数据层插件。未定义数据层时出错_Php_Wordpress_Google Datalayer - Fatal编程技术网

Php Wordpress简单数据层插件。未定义数据层时出错

Php Wordpress简单数据层插件。未定义数据层时出错,php,wordpress,google-datalayer,Php,Wordpress,Google Datalayer,我的主要目标是构建一个非常简单的datalayer插件,将简单数据推送到一个名为“datalayer”的窗口对象中,当我去输入window.datalayer时,我应该看到的是 我对PHP相当陌生,遇到了一些障碍 我找到了一个数据层实现的代码引用(),这似乎是一个不错的方法,所以我开始分解它,剥离出除了基本要素之外我不需要的所有东西 我的代码到目前为止 <?php /** * * Plugin Name: Ultimate DataLayer * Plugin URI: https

我的主要目标是构建一个非常简单的datalayer插件,将简单数据推送到一个名为“datalayer”的窗口对象中,当我去输入window.datalayer时,我应该看到的是

我对PHP相当陌生,遇到了一些障碍

我找到了一个数据层实现的代码引用(),这似乎是一个不错的方法,所以我开始分解它,剥离出除了基本要素之外我不需要的所有东西

我的代码到目前为止

<?php
/**
 *
 * Plugin Name: Ultimate DataLayer
 * Plugin URI: https://google.com.au
 * Version: 0.0.1
 * Author: James LeBoeuf
 * Author URI: https://google.com.au
 * Description: Pushes stuff to datalayer
 * Requires at least: 4.0
 * Tested up to: 4.8
 *
 *
 * @package Ultimate Datalayer
 * @category Datalayer
 * @author James LeBoeuf
*/

class DataLayer {
  protected static $_instance = null;

  public $dataLayer = [];

  public function __construct() {
    console_log('inside __construct function');
    add_action('wp_head', [ $this, 'init' ], 5);
  }

  public function init() {
    console_log('inside init function');
    $this->setupDataLayer();
    $this->addToWPHead();
  }

  public static function instance() {
    console_log('inside instance function');
    if (is_null(self::$_instance)) {
      self::$_instance = new self();
    }

    return self::$_instance;
  }

  public function output() {
    console_log('inside output function');
    $dataLayer = apply_filters('ultimate_datalayer', $this->dataLayer);
    console_log('inside output function $dataLayer', $dataLayer);
    if (!empty($dataLayer)) {
      $encodedDataLayer = json_encode($dataLayer);

      console_log($encodedDataLayer);

      $scriptTag = '<script data-cfasync="false" type="text/javascript">dataLayer.push( %s );</script>';

      console_log($scriptTag);

      echo sprintf($scriptTag, $encodedDataLayer);
    }
  }

  private function setupDataLayer() {
    console_log('inside setupDataLayer function');
    $this->dataLayer['test'] = 'test';
  }

  private function addToWPHead() {
    console_log('inside addToWPHead function');
    add_action('wp_head', [ $this, 'output' ], 30);
  }

}


function dataLayer() {
  console_log('inside dataLayer function');
  return DataLayer::instance();
}


function console_log( $data ) {
  echo '<script>';
  echo 'console.log('. json_encode( $data ) .')';
  echo '</script>';
}


add_action('init', 'dataLayer');

?>

似乎正在发生的是,数据层似乎没有被定义,并且在chrome inspector控制台屏幕上抛出了一个控制台错误。请查看附件中的截图


通过在
公共函数输出()的脚本中添加一个catch来解决

if(!window.dataLayer)window.dataLayer=[]

完整示例

public function output() {
  console_log('inside output function');
  $dataLayer = apply_filters('ultimate_datalayer', $this->dataLayer);
  console_log('inside output function $dataLayer', $dataLayer);
  if (!empty($dataLayer)) {
    $encodedDataLayer = json_encode($dataLayer);

    console_log($encodedDataLayer);

    $scriptTag = '<script data-cfasync="false" type="text/javascript"> if (!window.dataLayer) window.dataLayer = []; dataLayer.push( %s );</script>';

    console_log($scriptTag);

    echo sprintf($scriptTag, $encodedDataLayer);
  }
}
公共函数输出(){
控制台日志(“内部输出功能”);
$dataLayer=apply_filters('ultimate_dataLayer',$this->dataLayer);
控制台日志($dataLayer'内部输出函数,$dataLayer);
如果(!empty($dataLayer)){
$encodedDataLayer=json_encode($dataLayer);
控制台日志($encodedDataLayer);
$scriptTag='如果(!window.dataLayer)window.dataLayer=[];dataLayer.push(%s);';
控制台日志($scriptTag);
echo sprintf($scriptTag,$encodedDataLayer);
}
}