Support » Plugin: Virtual Robots.txt » textarea and actual file is all on one line

  • I believe that the stripslashes function applied to the options, inside the textarea field (in admin.php) and then possibly in the do_robots function (in pc-robotstxt.php) may be causing the error where the backslash for line endings,\n, are being stripped.

    The end result is a problematic one-liner that must be separated, in the textarea box by hand. Many WordPress users not adept at this, because they don’t understand the purpose of the code in the first place. It’s easy to unwind this improperly, even with instructions.

    Then they get errors from Google and are upset at my recommendation of this otherwise terrific plugin. I love it, other than for this problem.

    Can you please fix it? Thank you!

    –Fran

    https://wordpress.org/plugins/pc-robotstxt/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Marios Alexandrou

    (@marios-alexandrou)

    Thanks for reporting this issue. I’ve just uploaded an updated version that shouldn’t strip newlines.

    Thank you Marios! I’ll check it out, test and report back. Sure appreciate it.

    Marios, the stripslashes did not solve it.

    All of us continued to have the textarea in the displayed “Settings” shown as one line. This was difficult to update.

    After lots of experimentation, I came up with something that works.

    The only change is to the “settings_page” function, in the admin.php file.

    Here is the patch:

    $optstr = "";
                        //newly defined
    
    		if ( isset($_POST['update']) ) {
    			// check user is authorised
    			if ( function_exists( 'current_user_can' ) && !current_user_can( 'manage_options' ) )
    				die( 'Sorry, not allowed...' );
    			check_admin_referer( 'pc_robotstxt_settings' );
    
    			$options['user_agents'] = trim( $_POST['user_agents'] );
    
                          //Changes below: treat lines of post data as an array
    			$options['user_agents'] = explode(PHP_EOL, $options['user_agents']);
    			$options['user_agents'] = array_filter($options['user_agents'], 'trim');
                                   // remove any extra \r characters left behind
    
    			foreach ($options['user_agents'] as $line) {
        				$optstr .= $line . "";
                                         //blank text at end of line prevents run-on text
    			}
    			$options['user_agents'] = $optstr;
                                    //reassign
    
    			isset( $_POST['remove_settings'] ) ? $options['remove_settings'] = true : $options['remove_settings'] = false;
    
    			update_option( 'pc_robotstxt', $options );
    
    			echo '<div id="message" class="updated fade"><p><strong>Settings saved.</strong></p></div>';
    
    		}
    		else {
    			$options['user_agents'] = explode(PHP_EOL, $options['user_agents']);
    			$options['user_agents'] = array_filter($options['user_agents'], 'trim');
                                // remove any extra \r characters left behind
    
    			foreach ($options['user_agents'] as $line) {
        				$optstr .= $line . "";
                                       //blank text at end of line prevents run-on text
    			}
    			$options['user_agents'] = $optstr;
                                 //reassign
    		}

    Essentially and oddly, the important change here is to separate the $options[‘user_agent’] lines into an array, and add nothing more than a double quoted empty text to the end of each line:

    $optstr .= $line . "";

    A double quoted newline does not work; nor does an escaped newline work; nor does nl2br function work. However anything a blank text added works. Don’t ask me why.

    I had to add the else-block, because whether the result of a Post or a pull of the current Robots file, the effect was the same—a single line of text displayed on the textarea. Thus Post results and pull results are treated equally.

    Perhaps it is only Macs that ever had this problem? I don’t know. But if so, it’s fixed with this patch, even with stripslashes left in.

    Thanks, Marios. I like your plugin very much. Now it’s perfect and convenient.
    –Fran Corpier

    Plugin Author Marios Alexandrou

    (@marios-alexandrou)

    Thank you for the code. I tried it out and it looks good. I just submitted the new version of the plugin to the repository.

    Plugin Author Marios Alexandrou

    (@marios-alexandrou)

    I had to undo this update. It turns out something in the code removes blank lines which are perfectly valid in a robots.txt. I will look into it as soon as I can…

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘textarea and actual file is all on one line’ is closed to new replies.