ajax - Trigger action via button

I am using wordpress 4.9.8 and PHP 7.1.8 I have added a button to the new post screen:I am trying to trigger the button

I am using wordpress 4.9.8 and PHP 7.1.8 I have added a button to the new post screen:

I am trying to trigger the button the following way:

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_my_action', 'updateContent' );
function updateContent() {

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

    wp_die(); // this is required to terminate immediately and return a proper response
    $post = array(
        'ID'           => $post_id,
        'post_content' => 'Insert this content',
    );

    // Update the post into the database
    wp_update_post( $post );
}

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
                };
                console.log("test: " + ajaxurl)
                console.log(data)
                // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
                jQuery.post(ajaxurl, data, function (response) {
                    alert('Got this from the server: ' + response);
                });
            });
        });
    </script>

    <?php
}

However, when pressing submit I get:

Any suggestions why I get this error.

The above code was partly taken from the official reference.

I am using wordpress 4.9.8 and PHP 7.1.8 I have added a button to the new post screen:

I am trying to trigger the button the following way:

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_my_action', 'updateContent' );
function updateContent() {

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

    wp_die(); // this is required to terminate immediately and return a proper response
    $post = array(
        'ID'           => $post_id,
        'post_content' => 'Insert this content',
    );

    // Update the post into the database
    wp_update_post( $post );
}

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
                };
                console.log("test: " + ajaxurl)
                console.log(data)
                // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
                jQuery.post(ajaxurl, data, function (response) {
                    alert('Got this from the server: ' + response);
                });
            });
        });
    </script>

    <?php
}

However, when pressing submit I get:

Any suggestions why I get this error.

The above code was partly taken from the official reference.

Share Improve this question edited Jul 18, 2019 at 21:14 Castiblanco 2,1947 gold badges20 silver badges26 bronze badges asked Sep 16, 2018 at 9:45 Carol.KarCarol.Kar 3771 gold badge8 silver badges18 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

Remove this line

add_action( 'wp_ajax_my_action', 'updateContent' );

Add this two Line

add_action( 'wp_ajax_nopriv_updateContent', 'updateContent' );
add_action( 'wp_ajax_updateContent', 'updateContent' );

hopefully it will solve your issue

https://codex.wordpress/Plugin_API/Action_Reference/wp_ajax_nopriv_(action)

Replace this add_action( 'wp_ajax_my_action', 'updateContent' );

with

add_action( 'wp_ajax_updateContent', 'updateContent' ); add_action( 'wp_ajax_nopriv_updateContent', 'updateContent' );

Happy codeing

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745310543a4621976.html

相关推荐

  • ajax - Trigger action via button

    I am using wordpress 4.9.8 and PHP 7.1.8 I have added a button to the new post screen:I am trying to trigger the button

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信