Skip to content

Instantly share code, notes, and snippets.

@mindctrl
Last active May 23, 2022 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mindctrl/2642cfb3301b84c9d69ffbeae5b65a6a to your computer and use it in GitHub Desktop.
Save mindctrl/2642cfb3301b84c9d69ffbeae5b65a6a to your computer and use it in GitHub Desktop.
Using React Dev Tools (react-devtools) with WordPress and Safari

Introduction

The react-devtools package can be used to debug React applications in Safari (and other environments). Here's a quick example of using react-devtools with WordPress and Safari. This is useful for use cases where you're building React apps in wp-admin. It's also useful for exploring the structure and design of the Gutenberg block editor.

Install react-devtools package

Use these instructions to install react-devtools.

Install the PHP plugin code on this Gist

If you don't know how to add the code in enqueue-react-devtools-script.php as a plugin to your site, try using Pluginception to create a new custom plugin, paste in the code, and activate your new custom plugin.

Note: if your react-devtools screen shows you a different script source URL, update the PHP code to match. In the example, the URL is http://localhost:8097. Yours might be different.

image

Using it

Navigate to your React app in wp-admin, flip over to the react-devtools app, and explore your React app in the interface.

Screenshot of react-devtools displaying the structure of the Gutenberg block editor in WordPress. image

Resources

https://www.npmjs.com/package/react-devtools

<?php
// This loads the react-devtools script in wp-admin when using Safari.
add_action( 'admin_enqueue_scripts', function() {
if ( ! empty( $_SERVER['HTTP_USER_AGENT'] ) && strpos( $_SERVER['HTTP_USER_AGENT'], 'Safari' ) !== false ) {
?>
<script src="http://localhost:8097"></script>
<?php
}
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment