SlideShare a Scribd company logo
Debugging
Figuring It Out Yourself
1
Learning to investigate and fix issues
Every time a change is made to a website there is a risk that something will
break.
I will help you learn how to investigate the problem and fix it.
This will be quicker than posting to a Facebook group or other forum, and it will
help you understand WordPress and your site better.
2
WordPress components
WordPress uses PHP and MySQL to deliver HTML, CSS and Javascript to the
browser.
You use different tools to debug the back end (PHP and MySQL) and front end.
3
Firstly...
4
Firstly...
5
What changed?
If it was working, you changed something and now it's broken then the problem
is likely in the changed code but...
Often we change a lot of things or the error appears in something that didn't
change.
6
Source control for incremental development
Using source control like Git, Subversion etc can help with debugging.
If you commit your changes regularly you can review the changes in the edited
files to help narrow down the source of an error.
Maybe do a commit after you test a change.
This applies to CSS files too - a small syntax error can dramatically change how
a site looks.
7
Frontend issues
The backend is a bit of a black box where the request for a page goes in and
HTML, CSS and Javascript come out.
Let's look at the frontend first as it's often easier to work with.
While browsers are very tolerant of bad HTML markup use a validator to find
issues here and fix them where possible.
https://validator.w3.org/ and https://jigsaw.w3.org/css-validator/
8
Debugging CSS
Browsers now include built-in tools to view information about the current web
page.
For example, you can see all the images, CSS, and Javascript files downloaded
to make up the page. You can see if any failed to download or if Javascript
code has caused any errors.
For frontend debugging, you can see the CSS that is applied to each page
element and temporarily change styles and see the result.
9
Debugging CSS - Inspect Element
10
Debugging CSS - Inspect Element
11
Debugging CSS - other tools
The browsers have additional features to help, for example,
● If you edit some CSS then Firefox will show you the changed
styles so you know which ones to copy back to your
stylesheet to paste into a text editor.
● Chrome allows you copy the styles of an element.
● Both will list all the files downloaded and how long each
took. You can even throttle the download to mimic a slow
network.
12
Moving on to
debugging the
backend...
13
Tools to find the error
While viewing your site you will see that there is an error but not what file or line
is causing it. Let's get some tools in to help us.
In simpler applications, like a command line program, we could add 'echo' or
'print' or similar calls but WordPress is quite large and you need to narrow
things down first. Furthermore, you should never edit WordPress core code.
Even before using Xdebug and an IDE it is useful to narrow down where to look.
14
Debug Mode with WP_DEBUG
wp-config-sample.php has the following line:
define( 'WP_DEBUG', false );
A comment above that line says:
It is strongly recommended that plugin and theme
developers use WP_DEBUG in their development environments.
15
Debug Mode with WP_DEBUG
if ($_SERVER['REMOTE_ADDR'] == '12.34.56.78') {
define('WP_DEBUG', true);
// errors written to /wp-content/debug.log
define('WP_DEBUG_LOG', true); // Or set a filename.
define('WP_DEBUG_DISPLAY', false); // Errors to file.
@ini_set('display_errors', 0);
} else {
define('WP_DEBUG', false);
}
Here I have conditionally enabled debug mode for the specified IP address.
This limits error messages to those generated by my site visits.
16
Install and activate Query Monitor plugin
From the website:
It enables you to debug database queries, PHP errors, hooks and actions, block
editor blocks, enqueued scripts and stylesheets, rewrite rules, HTTP API calls,
and much more.
https://wordpress.org/plugins/query-monitor/
17
Reload the problem page
Now, with debug mode enabled and Query Monitor activated, reload the page
with the error.
This will write the error message to the wp-content/debug.log file and Query
Monitor will provide more information.
18
Read (and decipher) the error message
The error message will list the file and line number where the error occurred but
that doesn't always tell you where the problem is!
19
Sample error messages - major error
This is a serious error and cannot be ignored.
The site admin will also get an email. This is a new feature introduced in
WordPress 5.2 in May 2019. It's much better than a 500 error page (a White
Screen of Death - WSOD)!
20
Sample error messages - major error
PHP Parse error: syntax error, unexpected '$screen_id'
(T_VARIABLE) in /wp-content/plugins/wr.php on line 15
Here's lines 12 - 16 from wr.php:
12 add_action( 'admin_head', 'require_weight_field' );
13 function require_weight_field() {
14 $screen = get_current_screen()
15 $screen_id = $screen ? $screen->id : '';
16 if ( $screen_id == 'product' ) {
21
Sample PHP Notice message
Query Monitor didn't run because the previous error was occurred before
WordPress could fully run.
When it does run, the Query Monitor admin bar item will change colour - brown
for notices and red for errors.
22
More information from Query Monitor
23
More information from Query Monitor
In wp-content/debug.log:
PHP Notice: Undefined variable: screenid in
/wp-content/plugins/wr.php on line 16
PHP Notice: Undefined variable: variable_never_used_before in
/wp-content/plugins/wr.php on line 17
24
Fix this problem
Here's the code from that file:
14 $screen = get_current_screen();
15 $screen_id = $screen ? $screen->id : '';
16 $new_variable = $screenid;
17 $another_var = $variable_never_used_before;
On line 16 it is a typo $screenid should be $screen_id.
On line 17 it's a variable that was not set.
Notice that the message (undefined variable) is the same for both
situations. You might see this if accessing a global variable without using 'global
$variable' first.
25
A less serious error (QM didn't run)
PHP Fatal error: Uncaught Error: Call to a member
function get_image_id() on null in
/plugins/wcd-product-image-url.php:13
Here's lines 12 - 14 from the file:
12 global $product;
13 $post_thumbnail_id = $product->get_image_id();
14 $image_info = wp_get_attachment_image_src(
$post_thumbnail_id, 'full' );
26
Running in the right place
The function get_image_id() function is valid but the variable is null.
The problem is that the code is being run in the wrong place or without
appropriate checks.
The solution could be to call is_null($product) or is_product() first or
ensure that the function is only called on a page where product objects are
used.
27
error_log() and var_export()
Sometimes the code looks right but is not working correctly.
error_log() and var_export() are very helpful here.
error_log('This text will be in /wp-content/debug.log.');
To see readable versions of complex variables or objects use:
error_log('Variable: ' . var_export( $variable, true ));
28
error_log() and var_export()
Example usage:
add_action( 'wp_footer', 'dcwd_product_image' );
function dcwd_product_image() {
global $product;
$post_thumbnail_id = $product->get_image_id();
error_log( 'Thumbnail ID: ' . $post_thumbnail_id );
$image_info = wp_get_attachment_image_src(
$post_thumbnail_id, 'full' );
error_log( 'Image info: ' .
var_export( $image_info, true ) );
}
29
error_log() and var_export()
30
error_log() and var_export()
debug.log contents:
Thumbnail ID: 22
Image info: array (
0 =>
'http://localhost/wp-content/uploads/2018/03/tshirts.jpg',
1 => 801,
2 => 801,
3 => false,
)
31
Theme template files
If you are adding a template file to a theme you will need to know the Template
Hierarchy. Or cheat by getting the info from Query Monitor.
WordPress will look for different theme files depending on what is requested - a
page, post, archive etc. The Template Hierarchy codex page lists the files that it
will look for and their order.
Query Monitor will just tell you
32
Theme template files - what should I call it?
In the Query Monitor menu you
will see a 'Template' item.
Click it.
33
Theme template files - what should I call it?
Here I am viewing a page. I selected a Page Template when editing it.
The theme is a child theme and that's where the Page Template file is.
Page ID is 19 and slug is 'file-upload'.
34
Beyond runtime PHP errors
When the PHP engine reports the error (or warning or notice) it can generally be
fixed without the need for a debugger. What do we do when the issue is related
to the algorithm or program logic? Or in a section of code that does not have
any do_action() or apply_filters() calls?
We need a source code debugger - Xdebug and a debugging environment.
35
Xdebug & an IDE
Using error_log() and var_export() can only show the data at that point
in time.
Xdebug is a bridge between your site and your IDE (e.g. Microsoft Visual Studio
Code or PhpStorm or even Notepad++).
Combined with an IDE you will be able to watch variables as you step through
each line of code. You may also be able to change values.
36
Visual Studio Code - variables
37
Visual Studio Code - source code
38
Sometimes you need help...
If you are still stuck after trying each of the debugging methods it's time to ask
for help.
A fresh set of eyes can be very helpful.
Now you have to ask the right question and provide enough information...
39
How to ask for help
When you post on Facebook or StackExchange or a forum you will need to:
● demonstrate that you have tried to solve the problem (list the things you've
tried)
● link to the code involved
● clearly state what you are trying to achieve
● include a complete description of your setup (e.g. theme, plugins).
Sometimes writing this out will inspire you!
40
41
Good Luck
“If you could see the you that I see
When I see you seeing me
You'd see yourself so differently
Believe me.”
- Henry Rollins
42
Thanks!
Damien Carbery
damien@damiencarbery.com
www.damiencarbery.com
@daymobrew
43

More Related Content

What's hot

Front End Development: The Important Parts
Front End Development: The Important PartsFront End Development: The Important Parts
Front End Development: The Important Parts
Sergey Bolshchikov
 
Magento Product Types Demystified
Magento Product Types DemystifiedMagento Product Types Demystified
Magento Product Types Demystified
AOE
 
WCLA12 JavaScript
WCLA12 JavaScriptWCLA12 JavaScript
WCLA12 JavaScript
Jeffrey Zinn
 
WordPress Admin UI - Future Proofing Your Admin Pages
WordPress Admin UI - Future Proofing Your Admin PagesWordPress Admin UI - Future Proofing Your Admin Pages
WordPress Admin UI - Future Proofing Your Admin PagesBrandon Dove
 
Fast by Default
Fast by DefaultFast by Default
Fast by Default
Abhay Kumar
 
End-to-end testing with geb
End-to-end testing with gebEnd-to-end testing with geb
End-to-end testing with geb
Jesús L. Domínguez Muriel
 
Diversified application testing based on a Sylius project
Diversified application testing based on a Sylius projectDiversified application testing based on a Sylius project
Diversified application testing based on a Sylius project
Łukasz Chruściel
 
Pragmatic Parallels: Java and JavaScript
Pragmatic Parallels: Java and JavaScriptPragmatic Parallels: Java and JavaScript
Pragmatic Parallels: Java and JavaScript
davejohnson
 
Zepplin_Pronko_Magento_Festival Hall 1_Final
Zepplin_Pronko_Magento_Festival Hall 1_FinalZepplin_Pronko_Magento_Festival Hall 1_Final
Zepplin_Pronko_Magento_Festival Hall 1_FinalMax Pronko
 
Modern Web Technologies
Modern Web TechnologiesModern Web Technologies
Modern Web Technologies
Perttu Myry
 
Even Faster Web Sites at jQuery Conference '09
Even Faster Web Sites at jQuery Conference '09Even Faster Web Sites at jQuery Conference '09
Even Faster Web Sites at jQuery Conference '09
Steve Souders
 
Build your first rpa bot using IBM RPA automation
Build your first rpa bot using IBM RPA automationBuild your first rpa bot using IBM RPA automation
Build your first rpa bot using IBM RPA automation
Winton Winton
 
JUG Utrecht 2013 - Have you tried turning it off and on again? Problemen oplo...
JUG Utrecht 2013 - Have you tried turning it off and on again? Problemen oplo...JUG Utrecht 2013 - Have you tried turning it off and on again? Problemen oplo...
JUG Utrecht 2013 - Have you tried turning it off and on again? Problemen oplo...
Peter Martin
 
Form Validation NG
Form Validation NGForm Validation NG
Form Validation NGjoaopmaia
 
2012.sandiego.wordcamp
2012.sandiego.wordcamp2012.sandiego.wordcamp
2012.sandiego.wordcampBrandon Dove
 
Menu bars and menus
Menu bars and menusMenu bars and menus
Menu bars and menusmyrajendra
 
Troubleshooting Joomla! problems - Joomladay Germany 2014
Troubleshooting Joomla! problems - Joomladay Germany 2014Troubleshooting Joomla! problems - Joomladay Germany 2014
Troubleshooting Joomla! problems - Joomladay Germany 2014
Peter Martin
 
Secure WordPress Development Practices
Secure WordPress Development PracticesSecure WordPress Development Practices
Secure WordPress Development Practices
Brandon Dove
 

What's hot (20)

Front End Development: The Important Parts
Front End Development: The Important PartsFront End Development: The Important Parts
Front End Development: The Important Parts
 
Magento Product Types Demystified
Magento Product Types DemystifiedMagento Product Types Demystified
Magento Product Types Demystified
 
WCLA12 JavaScript
WCLA12 JavaScriptWCLA12 JavaScript
WCLA12 JavaScript
 
WordPress Admin UI - Future Proofing Your Admin Pages
WordPress Admin UI - Future Proofing Your Admin PagesWordPress Admin UI - Future Proofing Your Admin Pages
WordPress Admin UI - Future Proofing Your Admin Pages
 
Fast by Default
Fast by DefaultFast by Default
Fast by Default
 
End-to-end testing with geb
End-to-end testing with gebEnd-to-end testing with geb
End-to-end testing with geb
 
Diversified application testing based on a Sylius project
Diversified application testing based on a Sylius projectDiversified application testing based on a Sylius project
Diversified application testing based on a Sylius project
 
Pragmatic Parallels: Java and JavaScript
Pragmatic Parallels: Java and JavaScriptPragmatic Parallels: Java and JavaScript
Pragmatic Parallels: Java and JavaScript
 
Zepplin_Pronko_Magento_Festival Hall 1_Final
Zepplin_Pronko_Magento_Festival Hall 1_FinalZepplin_Pronko_Magento_Festival Hall 1_Final
Zepplin_Pronko_Magento_Festival Hall 1_Final
 
Zend Framework
Zend FrameworkZend Framework
Zend Framework
 
Modern Web Technologies
Modern Web TechnologiesModern Web Technologies
Modern Web Technologies
 
Even Faster Web Sites at jQuery Conference '09
Even Faster Web Sites at jQuery Conference '09Even Faster Web Sites at jQuery Conference '09
Even Faster Web Sites at jQuery Conference '09
 
Build your first rpa bot using IBM RPA automation
Build your first rpa bot using IBM RPA automationBuild your first rpa bot using IBM RPA automation
Build your first rpa bot using IBM RPA automation
 
JUG Utrecht 2013 - Have you tried turning it off and on again? Problemen oplo...
JUG Utrecht 2013 - Have you tried turning it off and on again? Problemen oplo...JUG Utrecht 2013 - Have you tried turning it off and on again? Problemen oplo...
JUG Utrecht 2013 - Have you tried turning it off and on again? Problemen oplo...
 
Form Validation NG
Form Validation NGForm Validation NG
Form Validation NG
 
2012.sandiego.wordcamp
2012.sandiego.wordcamp2012.sandiego.wordcamp
2012.sandiego.wordcamp
 
Menu bars and menus
Menu bars and menusMenu bars and menus
Menu bars and menus
 
Makezine
MakezineMakezine
Makezine
 
Troubleshooting Joomla! problems - Joomladay Germany 2014
Troubleshooting Joomla! problems - Joomladay Germany 2014Troubleshooting Joomla! problems - Joomladay Germany 2014
Troubleshooting Joomla! problems - Joomladay Germany 2014
 
Secure WordPress Development Practices
Secure WordPress Development PracticesSecure WordPress Development Practices
Secure WordPress Development Practices
 

Similar to Debugging - Figuring it out yourself (WordCamp Dublin 2019)

WordPress basic fundamental of plugin development and creating shortcode
WordPress basic fundamental of plugin development and creating shortcodeWordPress basic fundamental of plugin development and creating shortcode
WordPress basic fundamental of plugin development and creating shortcode
Rakesh Kushwaha
 
Write your first WordPress plugin
Write your first WordPress pluginWrite your first WordPress plugin
Write your first WordPress pluginAnthony Montalbano
 
Plug in development
Plug in developmentPlug in development
Plug in developmentLucky Ali
 
Wordpress as a framework
Wordpress as a frameworkWordpress as a framework
Wordpress as a framework
Aggelos Synadakis
 
Diagnosing WordPress: What to do when things go wrong
Diagnosing WordPress: What to do when things go wrongDiagnosing WordPress: What to do when things go wrong
Diagnosing WordPress: What to do when things go wrong
WordCamp Sydney
 
Creating Openbravo Workspace Widgets
Creating Openbravo Workspace WidgetsCreating Openbravo Workspace Widgets
Creating Openbravo Workspace Widgets
Rob Goris
 
Getting started with WordPress development
Getting started with WordPress developmentGetting started with WordPress development
Getting started with WordPress development
Steve Mortiboy
 
WordCamp Greenville 2018 - Beware the Dark Side, or an Intro to Development
WordCamp Greenville 2018 - Beware the Dark Side, or an Intro to DevelopmentWordCamp Greenville 2018 - Beware the Dark Side, or an Intro to Development
WordCamp Greenville 2018 - Beware the Dark Side, or an Intro to Development
Evan Mullins
 
SCWCD : Handling exceptions : CHAP : 5
SCWCD : Handling exceptions : CHAP : 5SCWCD : Handling exceptions : CHAP : 5
SCWCD : Handling exceptions : CHAP : 5
Ben Abdallah Helmi
 
WordCamp SF 2011: Debugging in WordPress
WordCamp SF 2011: Debugging in WordPressWordCamp SF 2011: Debugging in WordPress
WordCamp SF 2011: Debugging in WordPress
andrewnacin
 
Mastering WordPress Vol.1
Mastering WordPress Vol.1Mastering WordPress Vol.1
Mastering WordPress Vol.1
Wataru OKAMOTO
 
Don't sh** in the Pool
Don't sh** in the PoolDon't sh** in the Pool
Don't sh** in the PoolChris Jean
 
How to Build ToDo App with Vue 3 + TypeScript
How to Build ToDo App with Vue 3 + TypeScriptHow to Build ToDo App with Vue 3 + TypeScript
How to Build ToDo App with Vue 3 + TypeScript
Katy Slemon
 
Best practices in WordPress Development
Best practices in WordPress DevelopmentBest practices in WordPress Development
Best practices in WordPress Development
Mindfire Solutions
 
5 年後還是新手 - WordPress Plugin 開發大冒險 - GOTY
5 年後還是新手 - WordPress Plugin 開發大冒險 - GOTY5 年後還是新手 - WordPress Plugin 開發大冒險 - GOTY
5 年後還是新手 - WordPress Plugin 開發大冒險 - GOTY
William Chong
 
How to Webpack your Django!
How to Webpack your Django!How to Webpack your Django!
How to Webpack your Django!
David Gibbons
 
Writing your own WordPress themes and plugins
Writing your own WordPress themes and pluginsWriting your own WordPress themes and plugins
Writing your own WordPress themes and plugins
Stephanie Wells
 
Magento++
Magento++Magento++
Intro to drupal_7_architecture
Intro to drupal_7_architectureIntro to drupal_7_architecture
Intro to drupal_7_architecture
Hai Vo Hoang
 

Similar to Debugging - Figuring it out yourself (WordCamp Dublin 2019) (20)

WordPress basic fundamental of plugin development and creating shortcode
WordPress basic fundamental of plugin development and creating shortcodeWordPress basic fundamental of plugin development and creating shortcode
WordPress basic fundamental of plugin development and creating shortcode
 
Write your first WordPress plugin
Write your first WordPress pluginWrite your first WordPress plugin
Write your first WordPress plugin
 
Plug in development
Plug in developmentPlug in development
Plug in development
 
Wordpress as a framework
Wordpress as a frameworkWordpress as a framework
Wordpress as a framework
 
Vanjs backbone-powerpoint
Vanjs backbone-powerpointVanjs backbone-powerpoint
Vanjs backbone-powerpoint
 
Diagnosing WordPress: What to do when things go wrong
Diagnosing WordPress: What to do when things go wrongDiagnosing WordPress: What to do when things go wrong
Diagnosing WordPress: What to do when things go wrong
 
Creating Openbravo Workspace Widgets
Creating Openbravo Workspace WidgetsCreating Openbravo Workspace Widgets
Creating Openbravo Workspace Widgets
 
Getting started with WordPress development
Getting started with WordPress developmentGetting started with WordPress development
Getting started with WordPress development
 
WordCamp Greenville 2018 - Beware the Dark Side, or an Intro to Development
WordCamp Greenville 2018 - Beware the Dark Side, or an Intro to DevelopmentWordCamp Greenville 2018 - Beware the Dark Side, or an Intro to Development
WordCamp Greenville 2018 - Beware the Dark Side, or an Intro to Development
 
SCWCD : Handling exceptions : CHAP : 5
SCWCD : Handling exceptions : CHAP : 5SCWCD : Handling exceptions : CHAP : 5
SCWCD : Handling exceptions : CHAP : 5
 
WordCamp SF 2011: Debugging in WordPress
WordCamp SF 2011: Debugging in WordPressWordCamp SF 2011: Debugging in WordPress
WordCamp SF 2011: Debugging in WordPress
 
Mastering WordPress Vol.1
Mastering WordPress Vol.1Mastering WordPress Vol.1
Mastering WordPress Vol.1
 
Don't sh** in the Pool
Don't sh** in the PoolDon't sh** in the Pool
Don't sh** in the Pool
 
How to Build ToDo App with Vue 3 + TypeScript
How to Build ToDo App with Vue 3 + TypeScriptHow to Build ToDo App with Vue 3 + TypeScript
How to Build ToDo App with Vue 3 + TypeScript
 
Best practices in WordPress Development
Best practices in WordPress DevelopmentBest practices in WordPress Development
Best practices in WordPress Development
 
5 年後還是新手 - WordPress Plugin 開發大冒險 - GOTY
5 年後還是新手 - WordPress Plugin 開發大冒險 - GOTY5 年後還是新手 - WordPress Plugin 開發大冒險 - GOTY
5 年後還是新手 - WordPress Plugin 開發大冒險 - GOTY
 
How to Webpack your Django!
How to Webpack your Django!How to Webpack your Django!
How to Webpack your Django!
 
Writing your own WordPress themes and plugins
Writing your own WordPress themes and pluginsWriting your own WordPress themes and plugins
Writing your own WordPress themes and plugins
 
Magento++
Magento++Magento++
Magento++
 
Intro to drupal_7_architecture
Intro to drupal_7_architectureIntro to drupal_7_architecture
Intro to drupal_7_architecture
 

Recently uploaded

Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
wottaspaceseo
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
IES VE
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
Ortus Solutions, Corp
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
e20449
 
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdfEnhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Jay Das
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
Tier1 app
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Natan Silnitsky
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
vrstrong314
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 

Recently uploaded (20)

Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
 
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdfEnhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 

Debugging - Figuring it out yourself (WordCamp Dublin 2019)

  • 2. Learning to investigate and fix issues Every time a change is made to a website there is a risk that something will break. I will help you learn how to investigate the problem and fix it. This will be quicker than posting to a Facebook group or other forum, and it will help you understand WordPress and your site better. 2
  • 3. WordPress components WordPress uses PHP and MySQL to deliver HTML, CSS and Javascript to the browser. You use different tools to debug the back end (PHP and MySQL) and front end. 3
  • 6. What changed? If it was working, you changed something and now it's broken then the problem is likely in the changed code but... Often we change a lot of things or the error appears in something that didn't change. 6
  • 7. Source control for incremental development Using source control like Git, Subversion etc can help with debugging. If you commit your changes regularly you can review the changes in the edited files to help narrow down the source of an error. Maybe do a commit after you test a change. This applies to CSS files too - a small syntax error can dramatically change how a site looks. 7
  • 8. Frontend issues The backend is a bit of a black box where the request for a page goes in and HTML, CSS and Javascript come out. Let's look at the frontend first as it's often easier to work with. While browsers are very tolerant of bad HTML markup use a validator to find issues here and fix them where possible. https://validator.w3.org/ and https://jigsaw.w3.org/css-validator/ 8
  • 9. Debugging CSS Browsers now include built-in tools to view information about the current web page. For example, you can see all the images, CSS, and Javascript files downloaded to make up the page. You can see if any failed to download or if Javascript code has caused any errors. For frontend debugging, you can see the CSS that is applied to each page element and temporarily change styles and see the result. 9
  • 10. Debugging CSS - Inspect Element 10
  • 11. Debugging CSS - Inspect Element 11
  • 12. Debugging CSS - other tools The browsers have additional features to help, for example, ● If you edit some CSS then Firefox will show you the changed styles so you know which ones to copy back to your stylesheet to paste into a text editor. ● Chrome allows you copy the styles of an element. ● Both will list all the files downloaded and how long each took. You can even throttle the download to mimic a slow network. 12
  • 13. Moving on to debugging the backend... 13
  • 14. Tools to find the error While viewing your site you will see that there is an error but not what file or line is causing it. Let's get some tools in to help us. In simpler applications, like a command line program, we could add 'echo' or 'print' or similar calls but WordPress is quite large and you need to narrow things down first. Furthermore, you should never edit WordPress core code. Even before using Xdebug and an IDE it is useful to narrow down where to look. 14
  • 15. Debug Mode with WP_DEBUG wp-config-sample.php has the following line: define( 'WP_DEBUG', false ); A comment above that line says: It is strongly recommended that plugin and theme developers use WP_DEBUG in their development environments. 15
  • 16. Debug Mode with WP_DEBUG if ($_SERVER['REMOTE_ADDR'] == '12.34.56.78') { define('WP_DEBUG', true); // errors written to /wp-content/debug.log define('WP_DEBUG_LOG', true); // Or set a filename. define('WP_DEBUG_DISPLAY', false); // Errors to file. @ini_set('display_errors', 0); } else { define('WP_DEBUG', false); } Here I have conditionally enabled debug mode for the specified IP address. This limits error messages to those generated by my site visits. 16
  • 17. Install and activate Query Monitor plugin From the website: It enables you to debug database queries, PHP errors, hooks and actions, block editor blocks, enqueued scripts and stylesheets, rewrite rules, HTTP API calls, and much more. https://wordpress.org/plugins/query-monitor/ 17
  • 18. Reload the problem page Now, with debug mode enabled and Query Monitor activated, reload the page with the error. This will write the error message to the wp-content/debug.log file and Query Monitor will provide more information. 18
  • 19. Read (and decipher) the error message The error message will list the file and line number where the error occurred but that doesn't always tell you where the problem is! 19
  • 20. Sample error messages - major error This is a serious error and cannot be ignored. The site admin will also get an email. This is a new feature introduced in WordPress 5.2 in May 2019. It's much better than a 500 error page (a White Screen of Death - WSOD)! 20
  • 21. Sample error messages - major error PHP Parse error: syntax error, unexpected '$screen_id' (T_VARIABLE) in /wp-content/plugins/wr.php on line 15 Here's lines 12 - 16 from wr.php: 12 add_action( 'admin_head', 'require_weight_field' ); 13 function require_weight_field() { 14 $screen = get_current_screen() 15 $screen_id = $screen ? $screen->id : ''; 16 if ( $screen_id == 'product' ) { 21
  • 22. Sample PHP Notice message Query Monitor didn't run because the previous error was occurred before WordPress could fully run. When it does run, the Query Monitor admin bar item will change colour - brown for notices and red for errors. 22
  • 23. More information from Query Monitor 23
  • 24. More information from Query Monitor In wp-content/debug.log: PHP Notice: Undefined variable: screenid in /wp-content/plugins/wr.php on line 16 PHP Notice: Undefined variable: variable_never_used_before in /wp-content/plugins/wr.php on line 17 24
  • 25. Fix this problem Here's the code from that file: 14 $screen = get_current_screen(); 15 $screen_id = $screen ? $screen->id : ''; 16 $new_variable = $screenid; 17 $another_var = $variable_never_used_before; On line 16 it is a typo $screenid should be $screen_id. On line 17 it's a variable that was not set. Notice that the message (undefined variable) is the same for both situations. You might see this if accessing a global variable without using 'global $variable' first. 25
  • 26. A less serious error (QM didn't run) PHP Fatal error: Uncaught Error: Call to a member function get_image_id() on null in /plugins/wcd-product-image-url.php:13 Here's lines 12 - 14 from the file: 12 global $product; 13 $post_thumbnail_id = $product->get_image_id(); 14 $image_info = wp_get_attachment_image_src( $post_thumbnail_id, 'full' ); 26
  • 27. Running in the right place The function get_image_id() function is valid but the variable is null. The problem is that the code is being run in the wrong place or without appropriate checks. The solution could be to call is_null($product) or is_product() first or ensure that the function is only called on a page where product objects are used. 27
  • 28. error_log() and var_export() Sometimes the code looks right but is not working correctly. error_log() and var_export() are very helpful here. error_log('This text will be in /wp-content/debug.log.'); To see readable versions of complex variables or objects use: error_log('Variable: ' . var_export( $variable, true )); 28
  • 29. error_log() and var_export() Example usage: add_action( 'wp_footer', 'dcwd_product_image' ); function dcwd_product_image() { global $product; $post_thumbnail_id = $product->get_image_id(); error_log( 'Thumbnail ID: ' . $post_thumbnail_id ); $image_info = wp_get_attachment_image_src( $post_thumbnail_id, 'full' ); error_log( 'Image info: ' . var_export( $image_info, true ) ); } 29
  • 31. error_log() and var_export() debug.log contents: Thumbnail ID: 22 Image info: array ( 0 => 'http://localhost/wp-content/uploads/2018/03/tshirts.jpg', 1 => 801, 2 => 801, 3 => false, ) 31
  • 32. Theme template files If you are adding a template file to a theme you will need to know the Template Hierarchy. Or cheat by getting the info from Query Monitor. WordPress will look for different theme files depending on what is requested - a page, post, archive etc. The Template Hierarchy codex page lists the files that it will look for and their order. Query Monitor will just tell you 32
  • 33. Theme template files - what should I call it? In the Query Monitor menu you will see a 'Template' item. Click it. 33
  • 34. Theme template files - what should I call it? Here I am viewing a page. I selected a Page Template when editing it. The theme is a child theme and that's where the Page Template file is. Page ID is 19 and slug is 'file-upload'. 34
  • 35. Beyond runtime PHP errors When the PHP engine reports the error (or warning or notice) it can generally be fixed without the need for a debugger. What do we do when the issue is related to the algorithm or program logic? Or in a section of code that does not have any do_action() or apply_filters() calls? We need a source code debugger - Xdebug and a debugging environment. 35
  • 36. Xdebug & an IDE Using error_log() and var_export() can only show the data at that point in time. Xdebug is a bridge between your site and your IDE (e.g. Microsoft Visual Studio Code or PhpStorm or even Notepad++). Combined with an IDE you will be able to watch variables as you step through each line of code. You may also be able to change values. 36
  • 37. Visual Studio Code - variables 37
  • 38. Visual Studio Code - source code 38
  • 39. Sometimes you need help... If you are still stuck after trying each of the debugging methods it's time to ask for help. A fresh set of eyes can be very helpful. Now you have to ask the right question and provide enough information... 39
  • 40. How to ask for help When you post on Facebook or StackExchange or a forum you will need to: ● demonstrate that you have tried to solve the problem (list the things you've tried) ● link to the code involved ● clearly state what you are trying to achieve ● include a complete description of your setup (e.g. theme, plugins). Sometimes writing this out will inspire you! 40
  • 42. “If you could see the you that I see When I see you seeing me You'd see yourself so differently Believe me.” - Henry Rollins 42