acctman
acctman

Reputation: 4349

Wordpress var_dump in functions.php

I need to do a var_dump in a custom function filter in WP but, where is the results shown? The code is working because I can see the search result structure difference from when the code is present and not

    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;
}

Upvotes: 14

Views: 36700

Answers (3)

Ivijan Stefan Stipić
Ivijan Stefan Stipić

Reputation: 6668

Here is something good and clean:

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;

It's working similar like var_dump() but is more cleaner and formatted.

You can pass unlimited number of arguments to test and also it's separated betwean each dumps.

Upvotes: 1

Narek Malkhasyan
Narek Malkhasyan

Reputation: 1442

shutdown hook can be used, add this code to 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>";
  });
}

Upvotes: 3

taxicala
taxicala

Reputation: 21759

Try killing the flow right after the var_dump, that ussually helps me debug easier:

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

That way if something after your var_dump is rendering on top of the var dump it wont get covered by it.

Upvotes: 18

Related Questions