admin管理员组文章数量:1122846
I am trying to change the functionality of the preview button so the URL can be changed to the address I want. I am using the code below
function preview_link_fix( $preview_link ){
$mydomain = 'http://localhost:3001/preview';
$post = get_post( get_the_ID() );
$args = array(
'post' => $post,
'preview' => 'true'
);
return add_query_arg( $args, $mydomain );
}
This basically takes the preview to my react front end page. This is working fine for the custom post types and the post only in the case if they are published and the posts which are in the draft the button is not working but the preview option which is below the post in the post-list is working.
So the preview option which is shown in the image is working but for the preview button inside the edit, post page is not working. Please anybody can help me with which this problem.
I am trying to change the functionality of the preview button so the URL can be changed to the address I want. I am using the code below
function preview_link_fix( $preview_link ){
$mydomain = 'http://localhost:3001/preview';
$post = get_post( get_the_ID() );
$args = array(
'post' => $post,
'preview' => 'true'
);
return add_query_arg( $args, $mydomain );
}
This basically takes the preview to my react front end page. This is working fine for the custom post types and the post only in the case if they are published and the posts which are in the draft the button is not working but the preview option which is below the post in the post-list is working.
So the preview option which is shown in the image is working but for the preview button inside the edit, post page is not working. Please anybody can help me with which this problem.
Share Improve this question edited Feb 6, 2019 at 4:11 mrben522 1,6831 gold badge12 silver badges17 bronze badges asked Feb 5, 2019 at 8:38 Mohit NihalaniMohit Nihalani 415 bronze badges 2- You can’t pass WP_Post object as query arg... – Krzysiek Dróżdż Commented Feb 5, 2019 at 8:49
- @KrzysiekDróżdż but it is working if I pass the post_object, the only place it is not working in the post edit page, it is even working with the preview option showed in the image. – Mohit Nihalani Commented Feb 5, 2019 at 8:58
2 Answers
Reset to default 2I encountered the same issue and after some research, I came across the following solution.
For context, there is a bug on gutenberg and there is no platform specific way to change the preview link yet.
Add the following code to your functions.php:
// Workaround script until there's an official solution for https://github.com/WordPress/gutenberg/issues/13998
function fix_preview_link_on_draft() {
echo '<script type="text/javascript">
jQuery(document).ready(function () {
const checkPreviewInterval = setInterval(checkPreview, 1000);
function checkPreview() {
const editorPreviewButton = jQuery(".editor-post-preview");
const editorPostSaveDraft = jQuery(".editor-post-save-draft");
if (editorPostSaveDraft.length && editorPreviewButton.length && editorPreviewButton.attr("href") !== "' . get_preview_post_link() . '" ) {
editorPreviewButton.attr("href", "' . get_preview_post_link() . '");
editorPreviewButton.off();
editorPreviewButton.click(false);
editorPreviewButton.on("click", function() {
editorPostSaveDraft.click();
setTimeout(function() {
const win = window.open("' . get_preview_post_link() . '", "_blank");
if (win) {
win.focus();
}
}, 1000);
});
}
}
});
</script>';
}
add_action('admin_footer', 'fix_preview_link_on_draft');
Link to solution: https://github.com/WordPress/gutenberg/issues/13998#issuecomment-568698680
All credit to the contributor - Tvanro
I checked the code of the Admin Edit Post page because I noticed that it was being correctly generated in the part where you can change the name, and then I made this code, it seems that it does the trick:
add_filter( 'preview_post_link', 'generate_preview_link', 10, 2 );
function generate_preview_link($preview_link, $post) {
$faustConfig = get_option('faustwp_settings', []);
$frontEndUrl = $faustConfig['frontend_uri'] ?? '';
$frontEndUrl = rtrim($frontEndUrl, '/');
if ($post->post_status === 'draft') {
list($permalink, $post_name) = get_sample_permalink( $post->ID, null, null );
$permalink = str_replace( array( '%pagename%', '%postname%' ), $post_name, $permalink );
} else {
$permalink = get_permalink($post->ID);
}
$previewLinkParts = parse_url($permalink);
return $frontEndUrl . '/preview' . $previewLinkParts['path'];
}
本文标签: pluginsCustom previewpostlink not working for draft post
版权声明:本文标题:plugins - Custom preview_post_link not working for draft post 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736298384a1930220.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论