admin管理员组文章数量:1304504
It seems that this is a common issue. I have googled and searched Stack Exchange about this but didn't find any solution that helps in my situation.
Problem: When trying to use Ajax with admin-ajax.php, I always get the error 400.
POST .php 400
This is what I'm trying to achieve:
I'm building a web site for my client with my custom made theme. I created custom post type with custom post status 'Archived'. I'm trying to add a new column with a button in the custom post listing. With this button the admin can set the custom post as archived with single click.
Here is the simplified version of the code. I removed unrelated codes here.
<?php
add_action( 'wp_ajax_set_post_archived', 'set_post_archived' );
add_action( 'admin_footer', 'set_post_archived_js' );
function set_post_archived_js() {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('.set-archived-button').click(function(event){
event.preventDefault();
var data = {
action: 'set_post_archived',
post_id: $(this).data('post_id'),
};
jQuery.post(
ajaxurl, data,
function(response) {
alert('Got this from the server: ' + response);
});
});
});
</script>
<?php
}
function set_post_archived()
{
// The function code here...
wp_die();
}
The button's code is
<a href="#" data-post_id="70" class="button set-archived-button">Archive</a>
Post id in data-post_id
is created dynamically.
As mentioned, this code is on the backend, so wp_ajax_nopriv_{action_hook}
shouldn't be required.
Background-info
I have tried both jQuery.post()
and jQuery.ajax()
. Seems that, it doesn't make difference.
Here is how the code is being loaded if it matters.
In functions.php
:
if ( is_admin() ) {
// Only if on backend
require get_template_directory() . '/admin/admin-functions.php';
}
In /admin/admin-functions.php
:
global $pagenow;
if ( $pagenow == 'edit.php' || $pagenow == 'post.php' ) {
require get_template_directory() . '/admin/machines-functions.php';
}
The actual code with Ajax is in the file /admin/machines-functions.php
.
It seems that this is a common issue. I have googled and searched Stack Exchange about this but didn't find any solution that helps in my situation.
Problem: When trying to use Ajax with admin-ajax.php, I always get the error 400.
POST https://www.example/wp-admin/admin-ajax.php 400
This is what I'm trying to achieve:
I'm building a web site for my client with my custom made theme. I created custom post type with custom post status 'Archived'. I'm trying to add a new column with a button in the custom post listing. With this button the admin can set the custom post as archived with single click.
Here is the simplified version of the code. I removed unrelated codes here.
<?php
add_action( 'wp_ajax_set_post_archived', 'set_post_archived' );
add_action( 'admin_footer', 'set_post_archived_js' );
function set_post_archived_js() {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('.set-archived-button').click(function(event){
event.preventDefault();
var data = {
action: 'set_post_archived',
post_id: $(this).data('post_id'),
};
jQuery.post(
ajaxurl, data,
function(response) {
alert('Got this from the server: ' + response);
});
});
});
</script>
<?php
}
function set_post_archived()
{
// The function code here...
wp_die();
}
The button's code is
<a href="#" data-post_id="70" class="button set-archived-button">Archive</a>
Post id in data-post_id
is created dynamically.
As mentioned, this code is on the backend, so wp_ajax_nopriv_{action_hook}
shouldn't be required.
Background-info
I have tried both jQuery.post()
and jQuery.ajax()
. Seems that, it doesn't make difference.
Here is how the code is being loaded if it matters.
In functions.php
:
if ( is_admin() ) {
// Only if on backend
require get_template_directory() . '/admin/admin-functions.php';
}
In /admin/admin-functions.php
:
global $pagenow;
if ( $pagenow == 'edit.php' || $pagenow == 'post.php' ) {
require get_template_directory() . '/admin/machines-functions.php';
}
The actual code with Ajax is in the file /admin/machines-functions.php
.
1 Answer
Reset to default 1It seems that you're adding the AJAX action only if $pagenow
(the current admin page/file) is edit.php
or post.php
, and that's not going to work on admin-ajax.php
, so you should instead add the action via the admin_init
hook if you wish to add the action only on the admin side. E.g.
add_action( 'admin_init', function () {
add_action( 'wp_ajax_set_post_archived', 'set_post_archived' );
} );
本文标签: Adminajaxphp 400 error in custom theme
版权声明:本文标题:Admin-ajax.php 400 error in custom theme 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741784187a2397486.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
admin-ajax.php
api, and if something had happened it would have given you a plaintext error message in human readable language. Is there a specific reason you decided to use the older API? In fact on closer inspection of your Q I suspect what you want can be done with a request to the WP Core official endpoints that already exist! – Tom J Nowell ♦ Commented Feb 1, 2021 at 16:00