WordPress

WordPress TutorialRemove Version from WordPress and StylesheetsRun WordPress local with XAMPPSecurity in WordPress - EscapingSecurity in WordPress - SanitizationThe Loop (main WordPress loop)Update WordPress ManuallyWordPress Actions and FiltersWordPress add action()WordPress add editor style()WordPress add menu page()WordPress Add ShortcodeWordPress add submenu page()WordPress add theme support()WordPress Add/remove contact info for users with user contactmethods filter hookWordPress Admin Dashboard WidgetsWordPress AJAXWordPress Alternating main loop (pre get posts filter)WordPress Child Theme BasicsWordPress Create a Post ProgrammaticallyWordPress Create Template for Custom Post TypeWordPress Creating a custom templateWordPress Custom exerpts with excerpt length and excerpt moreWordPress Custom Post TypesWordPress Customizer Basics (Add Panel, Section, Setting, Control)WordPress Customizer Hello WorldWordPress DebuggingWordPress Enqueuing scriptsWordPress Enqueuing StylesWordPress Function : wp trim words()WordPress Function: add action()WordPress get bloginfo()WordPress get home path()WordPress get option()WordPress get permalink()WordPress get template part()WordPress get the category()WordPress get the title()WordPress home url()WordPress How Can I integrate Markdown editor with Advance Custom Fields repeater Add-on.WordPress initWordPress Installation and ConfigurationWordPress Making network requests with HTTP APIWordPress Meta BoxWordPress Options APIWordPress Plugin creationWordPress Plugin developmentWordPress Post FormatsWordPress Querying postsWordPress Remove Auto Line Breaks from Content and ExcerptWordPress REST APIWordPress Secure your installationWordPress ShortcodeWordPress Shortcode with attributeWordPress ShortcodesWordPress SidebarsWordPress Site MigrationWordPress TaxonomiesWordPress Template hierarchyWordPress template includeWordPress the $wpdb ObjectWordPress the Admin Bar (aka The Toolbar)WordPress the title()WordPress theme and child-theme developmentWordPress ThemesWordPress wp get current user()WordPress WP Query() LoopWordPress WP-CLIWordPress WP-Cron



WordPress Debugging

From WikiOD

https://codex.wordpress.org/Debugging_in_WordPress

Debugging PHP code is part of any project, but WordPress comes with specific debug systems designed to simplify the process as well as standardize code across the core, plugins and themes.

Remarks[edit | edit source]

Plugins for debugging in WordPress:

Example wp-config.php and good practices for Debugging[edit | edit source]

The following code, inserted in your wp-config.php file, will log all errors, notices, and warnings to a file called debug.log in the wp-content directory. It will also hide the errors so they do not interrupt page generation.

 // Enable WP_DEBUG mode
define( 'WP_DEBUG', true );

// Enable Debug logging to the /wp-content/debug.log file
define( 'WP_DEBUG_LOG', true );

// Disable display of errors and warnings 
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );

// Use dev versions of core JS and CSS files (only needed if you are modifying these core files)
define( 'SCRIPT_DEBUG', true );

Good practice If you want add custom messages to debug log add folowing code in your plugin or theme.

//Checking is function_exists  
    if ( !function_exists( 'print_to_log' ) ) {
            //function writes a message to debug.log if debugging is turned on.
            function print_to_log( $message )
            {
                if ( true === WP_DEBUG ) {
                    if ( is_array( $message ) || is_object( $message ) ) {
                        error_log( print_r( $message, true ) );
                    } else {
                        error_log( $message );
                    }
                }
            }
        }

SAVEQUERIES[edit | edit source]

The SAVEQUERIES definition saves the database queries to an array and that array can be displayed to help analyze those queries. The constant defined as true causes each query to be saved, how long that query took to execute, and what function called it. NOTE: This will have a performance impact on your site, so make sure to turn this off when you aren't debugging.

define( 'SAVEQUERIES', true );

The array is stored in the

global $wpdb->queries;

See logs in a separate file[edit | edit source]

When you have an ajax call, it's extremely difficult to get a log from inside of the callback function. But if you enable the debugging

define('WP_DEBUG', true);

and then after that add

ini_set('log_errors',TRUE);
ini_set('error_reporting', E_ALL);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');

you will have an error.log.txt file in your root folder where all your logs are located. you can even log them with

error_log( print_r( 'what I want to check goes here', true) );

inside your code. This will make your life a lot easier.

WP_DEBUG_LOG[edit | edit source]

WP_DEBUG_LOG is a companion to WP_DEBUG that causes all errors to also be saved to a debug.log log file inside the /wp-content/ directory. This is useful if you want to review all notices later or need to view notices generated off-screen (e.g. during an AJAX request or wp-cron run).

//enable
define( 'WP_DEBUG_LOG', true );

//disable
define( 'WP_DEBUG_LOG', false );

WP_DEBUG_DISPLAY[edit | edit source]

WP_DEBUG_DISPLAY is another companion to WP_DEBUG that controls whether debug messages are shown inside the HTML of pages or not. The default is 'true' which shows errors and warnings as they are generated. Setting this to false will hide all errors. This should be used in conjunction with WP_DEBUG_LOG so that errors can be reviewed later. Note: for WP_DEBUG_DISPLAY to do anything, WP_DEBUG must be enabled (true).

//enable
define( 'WP_DEBUG_DISPLAY', true );

//disable
define( 'WP_DEBUG_DISPLAY', false );

SCRIPT_DEBUG[edit | edit source]

SCRIPT_DEBUG is a related constant that will force WordPress to use the "dev" versions of core CSS and JavaScript files rather than the minified versions that are normally loaded. This is useful when you are testing modifications to any built-in .js or .css files. Default is false.

//enable
define( 'SCRIPT_DEBUG', true );

//disable
define( 'SCRIPT_DEBUG', false );

WP_DEBUG[edit | edit source]

WP_DEBUG is a PHP constant (a permanent global variable) that can be used to trigger the "debug" mode throughout WordPress. It is assumed to be false by default and is usually set to true in the wp-config.php file on development copies of WordPress.

define( 'WP_DEBUG', true );
define( 'WP_DEBUG', false );

Credit:Stack_Overflow_Documentation