Created
October 9, 2019 08:09
-
-
Save goranseric/04a1a5f6db0976bcf31acbc45b1b95c5 to your computer and use it in GitHub Desktop.
WP Post Preview link
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // credit: https://wordpress.stackexchange.com/questions/191918/custom-post-preview-without-saving | |
| // related: https://support.advancedcustomfields.com/forums/topic/preview-solution/ | |
| add_filter( 'preview_post_link', function ( $link ) { | |
| return 'http://domain.com/mobile-preview/?src=' . urlencode($link) . '%26admin_bar=false'; | |
| } ); | |
| -- | |
| (function ($) { | |
| $(document).ready(function () { | |
| // Create URL for post preview | |
| var previewUrl = $('#preview-action').find('.preview').attr('href'); | |
| var parser = document.createElement('a'); | |
| parser.href = previewUrl; | |
| var postId = $('#post_ID').val(); | |
| mobilePreviewUrl = parser.protocol + '//' + parser.host + '?p=' + postId; | |
| var href = mobilePreviewUrl ? 'http://domain.com/mobile-preview/?src=' + encodeURIComponent(mobilePreviewUrl) + '%26preview=true' : ''; | |
| // Preview buttons | |
| var mobilePreviewBtn = $('<a/>').addClass('preview button').attr({ | |
| 'id' : 'mobile-preview', | |
| 'href' : href, | |
| 'target': '_new' | |
| }).text('Preview Mobile'); | |
| $('#preview-action').prepend(mobilePreviewBtn); | |
| $('#post-preview').hide(); | |
| mobilePreviewBtn.on('click', function(e){ | |
| e.preventDefault(); | |
| $(window).off( 'beforeunload.edit-post' ); | |
| wp.autosave.server.tempBlockSave(); | |
| $('form#post').submit(); | |
| window.open(href, 'mobilePreview'); | |
| }); | |
| }); // end document ready | |
| })(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment