• 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.'">&nbsp;
        <input type="submit" name="show" value="Show">
      </form>
      '.$lines.'
    </div>';
    }

    Hope this will be of some help for someone.

    • This topic was modified 3 years, 7 months ago by platybill.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    This is a good solution, very innovative!

    Something else you can try, which is not very well documented, is including @line:n in the search box. For example, if you know there’s a snippet that has an error involving the text ‘id’ on line 9, you could search for id @line:9

    Thread Starter platybill

    (@platypus424)

    Wow, It’s an Easter egg for me. Thank you for sharing.

    Is there any other hidden search parameters by chance?

    Plugin Author Shea Bunge

    (@bungeshea)

    That’s the only one so far, but I’d be happy to add more if you have any suggestions.

    Hi @platypus424, I just came across this thread and the function you’ve provided in the top post is incredible – an absolute life saver for me!

    I’ve just added your function to a new snippet on my site, along with a second function to add your code as a new sub-menu page.

    I also made a few minor tweaks to your function to add some help text and horizontal lines, as well as displaying the snippet names when they don’t have an existing title (eg. “Untitled #29”).

    So for anyone who is interested in a complete snippet, which will add a new sub-page called “Display Line Numbers” to your admin menu (inside the Snippets menu), then please paste the code below verbatim into a new snippet. Set the snippet to “Only run in administration area” and save it, and then look for the new sub-page inside the Snippets menu on the left.

    @bungeshea maybe this could be added to a future version of the plugin itself as a new feature? ??

    function snippet_line_search_submenu_page() {
    	add_submenu_page(
    		'snippets',
    		'Display Snippet Line Numbers',
    		'Display Line Numbers',
    		'manage_options',
    		'snippet-line-number',
    		'lf_admin_show_snippets_Nth_line',
    	);
    }
    add_action( 'admin_menu', 'snippet_line_search_submenu_page', 99 );
    
    /*
     * Show the Nth line of all active snippets
     * (I put this function in a 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();
    			$title = empty( $s['name'] ) ? sprintf( __( 'Untitled #%d' ), $s['id'] ) : $s['name'];
    			$lines .= '<hr><p><a href="'.admin_url( '/admin.php?page=edit-snippet&id='.$s['id'] ).'" target="blank">'.$title.'</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">
    	<h1>'.get_admin_page_title().'</h1>
    	<p>This will display the contents of a given line number for all of your active snippets. You can use this to help identify which snippet is causing a particular PHP error on your website.</p>
    	<p>For example, if you receive a PHP error similar to this, which mentions the <strong>snippet-ops.php</strong> file, it means one of your snippets is causing the error:</p>
    	<p><code>Undefined variable: xyz in /wp-content/plugins/code-snippets/php/snippet-ops.php(446) : eval()\'d code on line N</code></p>
    	<p>Then you can enter the line number from the error message into the field below, and look through the results to find which snippet contains the text that matches the cause of the error (eg. "xyz" in the above example).</p>
    	<hr>
    	<form method="post" action="">
    		<p>Line number to show: <input type="text" name="line_number" value="'.$line_number.'">&nbsp;
    		<input type="submit" name="show" value="Show"></p>
    	</form>
    	<h2>Results:</h2>
    	'.$lines.'
    </div>';
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Guessing which snippet caused an error’ is closed to new replies.