Guessing which snippet caused an error
-
Hello,
Thank you for maintaining a great plugin. I am a big fun of your plugin and have 30+ snippets.
I would like to share how I guess which snippet caused an error like this:
<Error message> in … \snippet-ops.php(xxx) : eval()’d code on line N
The idea is simple: I write a function to list Nth line of all active snippets. It’s simple and not-so-smart way, but works for me.
/* * Show the Nth line of all active snippets * (I put this function in custom admin menu using add_submenu_page() ) */ function lf_admin_show_snippets_Nth_line() { // Default to 2 (where I put the code description) $line_number = $_POST['line_number'] ?? 2; $lines = ''; /* * Show the results (when 'Show' button is pressed) */ if ( isset( $_POST['show'] ) ) { // $ids, $multisite, $args $active_snippets = get_snippets( array(), null, array( 'active_only' => true ) ); foreach ( $active_snippets as $snip ) { $s = $snip->get_fields(); $lines .= '<p><a href="'.admin_url( '/admin.php?page=edit-snippet&id='.$s['id'] ).'" target="blank">'.$s['name'].'</a></p>'.PHP_EOL; $code = explode( PHP_EOL, $s['code'] ); if ( (int)$line_number <= count( $code ) ) { $lines .= '<pre><code>'.trim( $code[$line_number-1] ).'</code></pre>'.PHP_EOL; } } } /* * Set the line number to show */ echo ' <div class="wrap"> <h2>'.get_admin_page_title().'</h2> <form method="post" action=""> Line number to show: <input type="text" name="line_number" value="'.$line_number.'"> <input type="submit" name="show" value="Show"> </form> '.$lines.' </div>'; }
Hope this will be of some help for someone.
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Guessing which snippet caused an error’ is closed to new replies.