AI Verified

Name

Display snippet line numbers

About

Based on work by platypus424 and germankiwi, this snippet will allow you to see code on a particular line number from all of your snippets, allowing you to track down exactly where an error might be occuring.

Language

PHP

Rating

Voted: 0 by 0 user(s)

How to Setup Snippet

Activate the snippet on your site and then visit the Snippets > Display Line Numbers menu.

Codevault

shea

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: 6.1

Our AI bot has checked this snippet is compatable up to wordpress version: 6.1

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

Snippet Source:

https://wordpress.org/support/topic/guessing-which-snippet-caused-an-error/

History

Last modified:

07/04/2023

Important Note

This snippet has the following status:

AI Verified

This snippet has been tested by our AI bot, see any comments below.

AI Bot Comments:

Potential vulnerability found : Cross Site Scripting
Found on line : 22
Code : echo(esc_attr( $line_number ? $line_number : $default_line_number )
Vulnerable line : 5
Code : $line_number = isset( $_REQUEST['line_number'] ) ? intval( $_REQUEST['line_number'] ) : 0);

Potential vulnerability found : Cross Site Scripting
Found on line : 22
Code : echo(esc_attr( $line_number ? $line_number : $default_line_number )
Vulnerable line : 5
Code : $line_number = isset( $_REQUEST['line_number'] ) ? intval( $_REQUEST['line_number'] ) : 0);

Potential vulnerability found : Cross Site Scripting
Found on line : 48
Code : echo('<pre><code>', esc_html( trim( $lines[ $line_number - 1 ] )
Vulnerable line : 36
Code : $lines = explode( PHP_EOL, $snippet->code ));

Potential vulnerability found : Cross Site Scripting
Found on line : 48
Code : echo('<pre><code>', esc_html( trim( $lines[ $line_number - 1 ] )
Vulnerable line : 5
Code : $line_number = isset( $_REQUEST['line_number'] ) ? intval( $_REQUEST['line_number'] ) : 0);

Found 4 vulnerabilities

Display snippet line numbers

 
                    
1namespace Code_Snippets;
2 
3$render_callback = function () {
4 $default_line_number = 2;
5 $line_number = isset( $_REQUEST['line_number'] ) ? intval( $_REQUEST['line_number'] ) : 0;
6 echo '<div class="wrap">';
7 
8 ?>
9 <h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
10 <p>This will display the contents of a given line number for all of your active snippets. You can use this to help
11 identify which snippet is causing a particular PHP error on your website.</p>
12 <p>For example, if you receive a PHP error similar to this, which mentions the <strong>snippet-ops.php</strong>
13 file, it means one of your snippets is causing the error:</p>
14 <p><code>Undefined variable: xyz in /wp-content/plugins/code-snippets/php/snippet-ops.php(446) :
15 eval()'d code on line N</code></p>
16 <p>Then you can enter the line number from the error message into the field below, and look through the results to
17 find which snippet contains the text that matches the cause of the error (e.g. "xyz" in the above example).</p>
18 <hr>
19 <form method="post" action="">
20 <p>
21 <label for="line_number">Line number to show:</label>
22 <input type="number" name="line_number" id="line_number"
23 value="<?php echo esc_attr( $line_number ? $line_number : $default_line_number ); ?>">
24 &nbsp;
25 <input type="submit" value="Show" class="button button-primary">
26 </p>
27 </form>
28 <?php
29 
30 if ( $line_number ) {
31 echo '<h2>Results</h2>';
32 
33 $all_snippets = get_snippets();
34 
35 foreach ( $all_snippets as $snippet ) {
36 $lines = explode( PHP_EOL, $snippet->code );
37 
38 if ( ! $snippet->active || $line_number > count( $lines ) ) {
39 continue;
40 }
41 
42 printf(
43 '<hr><p><a href="%s" target="_blank">%s</a></p>%s',
44 esc_url( code_snippets()->get_snippet_edit_url( $snippet->id ) ),
45 esc_html( $snippet->display_name ),
46 PHP_EOL
47 );
48 
49 echo '<pre><code>', esc_html( trim( $lines[ $line_number - 1 ] ) ), '</code></pre>', PHP_EOL;
50 }
51 }
52 
53 echo '</div>';
54};
55 
56add_action( 'admin_menu', function () use ( $render_callback ) {
57 add_submenu_page(
58 code_snippets()->get_menu_slug(),
59 'Display Snippet Line Numbers',
60 'Display Line Numbers',
61 code_snippets()->get_cap(),
62 'snippet-line-numbers',
63 $render_callback
64 );
65}, 99 );

0

Related Snippets

Please see some snippets below related to this snippet..

WordPress Admin

Unverified

0

Clamp

Added: 7 months ago

Last Updated: 7 months ago

Fluid Typography Code Snippet - Add the Tool to your Dashboard or Page

WordPress Admin

AI Verified

0

Password-Protect-Page-Style

Added: 6 days ago

Last Updated: 6 days ago

WordPress Admin

AI Verified

0

Remove The Events Calendar menu item from the WordPress Admin Bar

Added: 8 months ago

Last Updated: 7 months ago

This snippets removes the "Events" link from the WordPress admin bar. The link comes from The Events Calendar plugin.

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

WordPress Admin

AI Verified

21

Change admin footer text

Added: 1 year ago

Last Updated: 2 weeks ago

Customises the footer text in the WordPress dashboard.

General

AI Verified

2

Make upload filenames lowercase

Added: 1 year ago

Last Updated: 3 days ago

Ensures that the filenames of all uploaded files are converted to lowercase, for consistency.

WordPress Admin

AI Verified

1

Disable automatic scroll-to-notices when adding snippets

Added: 8 months ago

Last Updated: 7 months ago

By default, Code Snippets will scroll the page up so that new notices are visible when editing or adding snippets. This snippet disables that functionality for those who prefer a more consistent editi...