1

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';
    }

2 Answers 2

2

With 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>';
}
0

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';     
    
}
6
  • Tried this. I added my code above. Perhaps I'm doing something incorrectly, but it's just returning a single "Line 0" at the very beginning.
    – Michael
    Commented Dec 22, 2020 at 20:36
  • you need to validate that $string is a real string and that $lines is an array with data - do this by dumping to the screen - i.e. var_dump( $array ); or to an error log - error_log( var_export( $array, true ) );
    – Q Studio
    Commented Dec 22, 2020 at 20:46
  • Check updated example.
    – Q Studio
    Commented Dec 22, 2020 at 20:47
  • Tried what you updated it with. Still just the single line number. This time with no output. I did verify that $string is indeed a string and $line is indeed an array via var_dump.
    – Michael
    Commented Dec 22, 2020 at 20:52
  • 1
    Tried that, too. I just posted my answer below. Got it to work with PHP_EOL instead of \n. Thanks for your help, Q Studio!
    – Michael
    Commented Dec 22, 2020 at 20:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.