• Resolved ten80snowboarder

    (@ten80snowboarder)


    I have WP installed in the root of an existing website, where the homepage in an ASP file. We are not yet moving the old site into WP, so the homepage needs to stay as it for now.

    Unfortunately, when a post is yet to be published and the preview is viewed, the URL is correct but the ASP driven homepage is displayed.

    Example: http://www.domain.com/?p=37&preview=true

    What I have determined at this point is that before the post is published the URL uses the ?p=37 but once it is published the URL has the post slug in it. Not sure how or why this would matter though.

    Has anybody seen this, or similar? Any thoughts?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter ten80snowboarder

    (@ten80snowboarder)

    UPDATE

    I have dug into some code and written a hook into the preview_post_link

    This looks to see if the post is published and if not it builds out the URL to return to the Preview Button. There was also a click function on the button, so I’m returning JS that hides the main button and then displays a new button.

    Here is my function:

    function the_preview_url_fix() {
    	global $post;
    	$post_id		= $post->ID;
    	$post_status	= $post->post_status;
    	$post_name		= $post->post_name;
    	if( $post_status != 'publish' ){
    		$post_cats	= get_the_category( $post_id );
    		$cat	= $post_cats[0]->slug;
    		$slug	= get_bloginfo('wpurl') . '/' . $cat . '/' . $post_name . '/?preview=true&preview_id=' . $post_id;
    		echo '<script>
    			  	jQuery(function($) {
    					$("#post-preview").hide().after("<a class=\"button\" href=\"'.$slug.'\" target=\"_blank\">Preview</a>");
    				});
    			  </script>';
    	}else{
    		$slug			= esc_url( get_permalink( $post_id ) );
    	}
        return	$slug;
    }
    add_filter( 'preview_post_link', 'the_preview_url_fix' );

    Hopefully this helps if anybody else has a similar issue. Thanks!

    Thread Starter ten80snowboarder

    (@ten80snowboarder)

    This topic is resolved.

    Thread Starter ten80snowboarder

    (@ten80snowboarder)

    UPDATED VERSION — there is no “post_name” before publishing the post, so this code fixes that issue in the generated URL.

    function the_preview_url_fix() {
    	global $post;
    	$post_id		= $post->ID;
    	$post_status	= $post->post_status;
    	$post_name		= $post->post_name;
    	if( $post_status != 'publish' ){
    		$slug	= get_bloginfo('wpurl') . '/index.php?p=' . $post_id.'&preview=true';
    		echo '<script>
    			  	jQuery(function($) {
    					$("#post-preview").hide().after("<a class=\"button\" href=\"'.$slug.'\" target=\"_blank\">Preview</a>");
    				});
    			  </script>';
    	}else{
    		$slug			= esc_url( get_permalink( $post_id ) );
    	}
        return	$slug;
    }
    add_filter( 'preview_post_link', 'the_preview_url_fix' );
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Preview not working with non-WP homepage’ is closed to new replies.