Support » Fixing WordPress » Jump Link into Password protected page

  • jasonoberholzer

    (@jasonoberholzer)


    Hello,

    I’m maintaining a wordpress site for a friend and found an issue with Jump Links to a password protected page.

    I created my anchor tag with proper id on the password protected page. Then on another page, I created an anchor link that points to the password protected page with the ID of the anchor tag. Whenever I click on the link from the public page, WordPress redirects me to the password page (with the jump link visible in the URL). I enter the password and click Submit. I get redirected to the password page as expected, but the jump link is removed from the URL and the password protected page doesn’t scroll to the location where the anchor tag is.

    Has anyone else had this issue? Is there any work around for this?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Joy

    (@joyously)

    It seems like it should use the entire URL, and not strip the target.
    Can you check Trac to see if there’s a ticket for that, and if not, write one?
    https://core.trac.wordpress.org/

    I have the same issue. Don’t have time to report bug but I’m going to write a fix and submit here. If it works for both of us, I’ll create a ticket.

    Damn. Can’t hook or filter my way out of this one.

    Here’s what I found out:

    • wp-login.php handles password protected pages around line 500
    • little known fact: after putting in a password, the browser cookie lives for 10 freaking days! You can make it a session cookie with the following code add_filter( 'post_password_expires', function () { return 0; } ); … not part of the solution here but I thought I’d let folks know.
    • the issue here is that they use wp_get_referer() to get the URL to send you back to … that in turn uses wp_get_raw_referer() to grab preferably $_REQUEST['_wp_http_referer'] or the fallback of $_SERVER['HTTP_REFERER'] … which ignore anchors unless handled by PHP in some different way
    • interesting but unrelated: the code also calls wp_get_referer() 2 times and the second one in unnecessary because it’s already been stored in the $referer variable … come on WP! … just kidding … ya’ll rock

    Sooooo, basically we’re hosed for now. I’ll need to write some JS to make this work I think.

    OK, this’ll do it. It’s a bit dirty:

    ** NOTE: I use the fabulous Custom Content Shortcode plugin so I shortcutted some script tags here …

    wp_enqueue_script( 'jquery' );
    
    // this function filters the password input form, adding the _wp_http_referer URL ... no big deal
    add_filter( 'the_password_form', function ( $output ) {
    	global $wp;
    	return str_ireplace( '</form>', '<input type="hidden" name="_wp_http_referer" class="_wp_http_referer" value="' . trailingslashit( home_url( $wp->request ) ) . '"></form>', $output );
    } );
    
    //this function outputs some nasty JS to add the URL hashish fragments to the referer form input (which we created above ... how nice)
    add_action( 'wp_footer', function () {
    	ob_start();?>
    [js]
    		jQuery(function( $ ){
    			var $hidden = $( '.post-password-form input[name=_wp_http_referer]' ),
    			_wp_http_referer = $hidden.val();
    			$hidden.val( _wp_http_referer + window.location.hash ); //the magic is real
    		});
    [/js]<?php
    	echo do_shortcode( ob_get_clean() );
    });

    If you want it without the shortcodes hit me up. I’m watching the thread.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Jump Link into Password protected page’ is closed to new replies.