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 badges2 Answers
Reset to default 1Remove 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
评论列表(0条)