0

I'm learning to develop a plugin in Wordpress.

I would render my custom debug info inside page and not before the head (so I think it's a hook issue?).

This is my plugin code:

if (is_admin()) {

    require_once SVERBA__PLUGIN_DIR.'class.sverba-admin.php';
    add_action( 'init', array( 'SverbaAdmin', 'init' ) );


    if (SVERBA__DEBUG > 0) {

        add_action( 'init', array( 'SverbaAdmin', 'printDebug' ) );

    }

}

And this is the class.sverba-admin.php code (only a part, not all):

public function printDebug() {

    echo '<div class="wrap">';
        echo '<h1>Variabili</h1>';
        echo '<ul>';
            echo '<li>'.var_dump(self::$initiated).'</li>';
        echo '</ul>';
    echo '</div>';

}

It works, but debug info are printed BEFORE the head. I want them inside the page, for example in the div with class "wrap"

enter image description here

  • You have to create an admin screen and display your data or hook your function to your desired admin page. init hook fires after WordPress has finished loading but before any headers are sent, meaning runs before hmtl rendering. Also you could use <li><pre> . var_dump(self::$initiated) </pre></li> for better formatting. – Laxmana Mar 14 '17 at 14:01
0

Don't print any debug info into the page directly. WordPress offers a wp-content/debug.log file which you can use.

Enable debug and debug_log in your wp-config.php

define( 'WP_DEBUG', true ); // turn debug mode on
define( 'WP_DEBUG_DISPLAY', false ); // do not display errors
define( 'WP_DEBUG_LOG', true ); // save errors to wp-content/debug.log
define( 'SAVEQUERIES', true ); // save queries for debug

Add this handy function to your functions.php or plugin file

/**
 * Simple debug trace to wp-content/debug.log
 * @usage _log( $var );
 */
if ( ! function_exists( '_log' ) ) {
    function _log( $log ) {

        if ( true !== WP_DEBUG ) {
            return;
        }

        if ( is_array( $log ) || is_object( $log ) ) {
            error_log( print_r( $log, true ) );
        } else {
            error_log( $log );
        }

    }
}

Use it in your code whenever you need to print some debug info

_log( $my_var );

Open wp-content/debug.log and see you data printed there. I usually keep it as a pinned file in my IDE (PHPStorm).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.