I have custom post edit screen with custom metaboxes and wanted the last metabox to "Save this post and start creating a new one".
It's easy to redirect user to new post creation page with url like post-new.php?post_type=customposttype but what url should I call with javascript before this to make the post saved?
Thanks
add_meta_box("addanotheritem", 'Add another', 'addanother_metabox', 'custompost', 'side', 'high');
function addanother_metabox() {
?>
<script type="text/javascript">
function saveAndGo() {
jQuery('button.editor-post-publish-button__button').click();
window.location='./post-new.php?post_type=custompost';
return false;
}
</script>
<button onclick='javascript:saveAndGo();'>save & add another</button>
<?php
}
I have custom post edit screen with custom metaboxes and wanted the last metabox to "Save this post and start creating a new one".
It's easy to redirect user to new post creation page with url like post-new.php?post_type=customposttype but what url should I call with javascript before this to make the post saved?
Thanks
add_meta_box("addanotheritem", 'Add another', 'addanother_metabox', 'custompost', 'side', 'high');
function addanother_metabox() {
?>
<script type="text/javascript">
function saveAndGo() {
jQuery('button.editor-post-publish-button__button').click();
window.location='./post-new.php?post_type=custompost';
return false;
}
</script>
<button onclick='javascript:saveAndGo();'>save & add another</button>
<?php
}
Share
Improve this question
edited Jan 28, 2021 at 9:24
mik256
asked Jan 26, 2021 at 22:13
mik256mik256
11 bronze badge
12
- perhaps bind a click event to the editor save button? – Q Studio Commented Jan 26, 2021 at 22:50
- that would be great, how this can be done? – mik256 Commented Jan 27, 2021 at 7:51
- 1 Amazing. Thank you! – mik256 Commented Jan 27, 2021 at 22:19
- 1 sorry, I have already updated the question – mik256 Commented Jan 28, 2021 at 9:03
- 1 need to study about promises, but I got the solution, I used "stupid" timeout and it works – mik256 Commented Jan 28, 2021 at 9:27
1 Answer
Reset to default 0I know this is kinda stupid solution, but 2s timeout is acceptable solution
add_meta_box("addanotheritem", 'Add another', 'addanother_metabox', 'custompost', 'side', 'high');
function addanother_metabox() {
?>
<script type="text/javascript">
function saveAndGo() {
jQuery('button.editor-post-publish-button__button').click();
setTimeout(function() {
window.location='./post-new.php?post_type=custompost';
}, 2000);
return false;
}
</script>
<button onclick='javascript:saveAndGo();'>save & add another</button>
<?php
}