簡體   English   中英

按鈕提交后,刷新頁面

[英]After button submit, refresh page

我使用的是wordpress version 4.9.8PHP 7.1.8

下面你可以看到,我在帖子中添加了一個按鈕。 此按鈕可將內容插入the_content字段中。 提交按鈕后,我想刷新單個帖子頁面並顯示內容。

在下面找到我的按鈕代碼:

add_action('media_buttons', 'add_my_media_button', 99);
function add_my_media_button()
{

    $post = $GLOBALS['post_ID'];
    echo "<a href='#' id='insert-my-media' data-post-id='{$post}' class='button'>Own content</a>";

}

add_action('wp_ajax_updateContent', 'updateContent');
function updateContent()
{

    $post_id = intval($_POST['post_id']);

    try {
        $sp = new SinglePostContent();
        $sp->main($post_id);
    } catch (Exception $e) {
        echo $e;
    }
    wp_die(); // this is required to terminate immediately and return a proper response
}

add_action('admin_footer', 'my_media_button_script');

function my_media_button_script()
{

    ?>
    <script>
        jQuery(document).ready(function ($) {
            $('#insert-my-media').click(function () {
                var post_id = $(this).attr('data-post-id');
                var data = {
                    'action': 'updateContent',
                    'post_id': post_id
                };
                // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
                jQuery.post(ajaxurl, data, function (response) {
                    console.log("test12")
                    console.log(response);
                });
            });
        });
    </script>

    <?php
}

有關如何刷新頁面和顯示內容的任何建議。

期待您的回復!

在回調函數中,重新加載頁面:

jQuery.post(ajaxurl, data, function (response) {
    console.log("test12")
    console.log(response);
    location.reload();
});
           jQuery.post(ajaxurl, data, function (response) {
                console.log("test12")
                console.log(response);
                location.reload(true);
            });

在重新加載時使用ture從服務器重新加載硬盤。 或者您可以返回更新的帖子內容並使用html()替換它而不刷新。

像這樣更改你的代碼:

[...]
jQuery.post(ajaxurl, data, function (response) {
  console.log("test12")
  console.log(response);
  window.location.reload();
});
[...]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM