I have a private custom plugin that I just use on my multiple sites, and since I do a lot of debugging I find it easier to include the wp-config.php file contents in the admin area to ensure I have enabled/disabled debugging properly. Now I am training other designers to help with debugging, so I want to include line numbers similar to the theme/plugin editor. I retrieve the contents of the wp-config file using the file_get_contents()
function. Is there a way I can add line numbers to this output?
Here is my function that gets the wp-config file contents:
function eriWpConfig(){
$wp_config = FALSE;
if ( is_readable( ABSPATH . 'wp-config.php' ) )
$wp_config = ABSPATH . 'wp-config.php';
elseif ( is_readable( dirname( ABSPATH ) . '/wp-config.php' ) )
$wp_config = dirname( ABSPATH ) . '/wp-config.php';
if ( $wp_config )
$code = esc_html( file_get_contents( $wp_config ) );
else
$code = 'wp-config.php not found';
echo '<pre class="code"
>Installation path: ' . ABSPATH
. "\n\n"
. $code
. '</pre>';
}
EDIT: Per Q Studio's suggestion, I tried the following and I'm just returning Line 0 at the beginning of the output.
if ( $wp_config ) {
$string = file_get_contents( $wp_config );
$lines = explode('\n', $string);
$modified_lines = [];
$line_count = 0;
foreach($lines as $line){
$modified_lines[] = 'Line '.$line_count.' '.$line;
}
$code = esc_html( implode('<br>', $modified_lines) );
} else {
$code = 'wp-config.php not found';
}
I have a private custom plugin that I just use on my multiple sites, and since I do a lot of debugging I find it easier to include the wp-config.php file contents in the admin area to ensure I have enabled/disabled debugging properly. Now I am training other designers to help with debugging, so I want to include line numbers similar to the theme/plugin editor. I retrieve the contents of the wp-config file using the file_get_contents()
function. Is there a way I can add line numbers to this output?
Here is my function that gets the wp-config file contents:
function eriWpConfig(){
$wp_config = FALSE;
if ( is_readable( ABSPATH . 'wp-config.php' ) )
$wp_config = ABSPATH . 'wp-config.php';
elseif ( is_readable( dirname( ABSPATH ) . '/wp-config.php' ) )
$wp_config = dirname( ABSPATH ) . '/wp-config.php';
if ( $wp_config )
$code = esc_html( file_get_contents( $wp_config ) );
else
$code = 'wp-config.php not found';
echo '<pre class="code"
>Installation path: ' . ABSPATH
. "\n\n"
. $code
. '</pre>';
}
EDIT: Per Q Studio's suggestion, I tried the following and I'm just returning Line 0 at the beginning of the output.
if ( $wp_config ) {
$string = file_get_contents( $wp_config );
$lines = explode('\n', $string);
$modified_lines = [];
$line_count = 0;
foreach($lines as $line){
$modified_lines[] = 'Line '.$line_count.' '.$line;
}
$code = esc_html( implode('<br>', $modified_lines) );
} else {
$code = 'wp-config.php not found';
}
Share
Improve this question
edited Dec 22, 2020 at 20:38
Michael
asked Dec 22, 2020 at 19:50
MichaelMichael
2811 silver badge14 bronze badges
2 Answers
Reset to default 2With the help of Q Studio and my own research, I found that exploding with PHP_EOL worked. Here is my modified code:
function eriWpConfig(){
$wp_config = FALSE;
if ( is_readable( ABSPATH . 'wp-config.php' ) )
$wp_config = ABSPATH . 'wp-config.php';
elseif ( is_readable( dirname( ABSPATH ) . '/wp-config.php' ) )
$wp_config = dirname( ABSPATH ) . '/wp-config.php';
if ( $wp_config ) {
$string = file_get_contents( $wp_config );
$lines = explode(PHP_EOL, $string);
$modified_lines = [];
$line_count = 0;
foreach($lines as $line){
$modified_lines[] = '<span style="color: #ccc;">Line: '.sprintf("%03d", $line_count).' | </span>'.esc_html($line);
$line_count ++;
}
$code = implode('<br>', $modified_lines);
} else {
$code = 'wp-config.php not found';
}
echo '<pre class="code"
>Installation path: ' . ABSPATH
. "\n\n"
. $code
. '</pre>';
}
Perhaps something like:
if ( $wp_config ) {
$string = file_get_contents( $wp_config );
$lines = explode('\n', $string);
$line_count = 0;
foreach( $lines as $line ){
$code .= 'Line: '.$line_count.' | '.$line.'<br />';
// iterate count ##
$line_count ++;
}
} else {
$code = 'wp-config.php not found';
}