最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

autosave - Custom post edit with button to save post and start creating new

programmeradmin0浏览0评论

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
 |  Show 7 more comments

1 Answer 1

Reset to default 0

I 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  
    }  

发布评论

评论列表(0)

  1. 暂无评论