【问题标题】:Wordpress var_dump in functions.phpfunctions.php 中的 Wordpress var_dump
【发布时间】:2015-03-06 17:49:40
【问题描述】:

我需要在 WP 的自定义函数过滤器中执行 var_dump,但是,结果显示在哪里?该代码正在运行,因为我可以看到与代码存在时的搜索结果结构不同

    add_filter('relevanssi_hits_filter', 'products_first');
function products_first($hits) {
    $types = array();

    $types['section1'] = array();
    $types['section2'] = array();
    $types['section3'] = array();
    $types['section4'] = array();

    // Split the post types in array $types
    if (!empty($hits)) {
        foreach ($hits[0] as $hit) {
            array_push($types_1[$hit->post_type], $hit);
        }
    }

    // Merge back to $hits in the desired order
    var_dump($types);
    $hits[0] = array_merge($types['section1'], $types['section2'], $types['section3'], $types['section4']);
    return $hits;
}

【问题讨论】:

  • 检查 html 源代码,浏览器可能将其解释得很奇怪。您可以在它之前放置一个 pre 标签并在之后退出以帮助使其在浏览器中可见

标签: php wordpress


【解决方案1】:

尝试在 var_dump 之后立即终止流,这通常有助于我更轻松地调试:

var_dump($types);
die("products_first_ends");

这样,如果您的 var_dump 之后的某些内容呈现在 var 转储之上,则它不会被它覆盖。

【讨论】:

  • 终于!!谢谢。
【解决方案2】:

shutdown钩子可以用,把这段代码加到functions.php:

function custom_dump($anything){
  add_action('shutdown', function () use ($anything) {
    echo "<div style='position: absolute; z-index: 100; left: 30px; bottom: 30px; right: 30px; background-color: white;'>";
    var_dump($anything);
    echo "</div>";
  });
}

【讨论】:

    【解决方案3】:

    这里有一些好的和干净的东西:

    if(!function_exists('wp_dump')) :
        function wp_dump(){
            if(func_num_args() === 1)
            {
                $a = func_get_args();
                echo '<pre>', var_dump( $a[0] ), '</pre><hr>';
            }
            else if(func_num_args() > 1)
                echo '<pre>', var_dump( func_get_args() ), '</pre><hr>';
            else
                throw Exception('You must provide at least one argument to this function.');
        }
    endif;
    

    它的工作方式类似于 var_dump(),但更简洁和格式化。

    您可以传递无限数量的参数进行测试,并且每个转储之间也可以分开。

    【讨论】:

      猜你喜欢
      • 2018-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-28
      • 1970-01-01
      • 1970-01-01
      • 2015-02-05
      • 2017-03-04
      相关资源
      最近更新 更多