我读过10多个答案,建议在数据中使用操作,并在函数的末尾添加回显和死。但我还是收到了错误的要求。如果我在浏览器中直接打开URL "http://localhost/wpdemo/wp-admin/admin-ajax.php“,它将返回0。
以下是我的ajax代码:
jQuery(document).ready(function($) {
$('#submit').on('click', function(e){
e.preventDefault();
let title = $('#title').val();
let content = $('#content').val();
var formData = new FormData();
var formData = {
image: document.getElementById('image-input').files[0],
var1: title,
var2: content
};
$.ajax({
url: "/wpdemo/wp-admin/admin-ajax.php",
type: 'POST',
data: { action : 'my_ajax_handler', chosen : formData },
processData: false,
contentType: false,
success: function(response) {
console.log(response);
}
});
})
});
我添加了一个php文件来处理ajax请求。这是我的PHP代码。
$post_title,
'post_content' => $post_content,
'post_status' => 'publish',
'post_name' => 'pending',
);
$pid = wp_insert_post($new_post);
add_post_meta($pid, 'meta_key', true);
if (!function_exists('wp_generate_attachment_metadata'))
{
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
}
if ($_FILES)
{
foreach ($_FILES as $file => $array)
{
if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK)
{
return "upload error : " . $_FILES[$file]['error'];
}
$attach_id = media_handle_upload( $file, $pid );
}
}
if ($attach_id > 0)
{
//and if you want to set that image as Post then use:
update_post_meta($pid, '_thumbnail_id', $attach_id);
}
}
echo json_encode('success');
die();
}
add_action("wp_ajax_my_ajax_handler", "my_ajax_handler");
add_action("wp_ajax_nopriv_my_ajax_handler", "my_ajax_handler");
?>
我的表单来自我在我的主插件文件中创建的一个简短的代码。我的插件文件代码在这里。
.form-container {
width: 70%;
margin: 0 auto;
border: 1px solid #ccc;
padding: 20px;
}
.form-container input[type="text"], .form-container textarea {
width: 100%;
border: 1px solid #ccc;
padding: 10px;
margin-bottom: 10px;
}
.form-container input[type="submit"] {
background-color: #333;
color: #fff;
border: 0;
padding: 10px 20px;
cursor: pointer;
}
Sumit Your Article!
submit
';
return $output;
}
add_shortcode('form', 'form_shortcode_func');
发布于 2022-12-27 11:10:12
在使用ajax调用时,不要使用这样的ajax URL。使用wp_localize_script是实现这一目标的好方法。
让我们假设js代码在script.js文件中。你需要本地化那个文件。
function my_theme_enqueue_scripts() {
wp_enqueue_script( 'custom-jquery', 'https://code.jquery.com/jquery-3.5.1.min.js', array(), '1.0', true );
wp_enqueue_script( 'ajax-script', plugin_dir_url( __FILE__ ) . 'js/script.js', array( 'jquery' ), '1.0', true );
wp_localize_script( 'ajax-script', 'Obj', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_scripts' );
JS文件:
jQuery(document).ready(function ($) {
$("#submit").on("click", function (e) {
e.preventDefault();
let ajax_url = Obj.ajax_url; //Ajax url
let title = $("#title").val();
let content = $("#content").val();
var data = new FormData($('#postform')[0]);
data.append( "image", $('#input-image')[0].files[0]);
data.append( "action", 'my_ajax_handler');
data.append( "title", $('#title').val());
data.append( "content", $('#content').val());;
$.ajax({
url: ajax_url,
type: "POST",
data: data,
success: function (response) {
console.log(response);
},
});
});
});
Ajax回调函数:
function my_ajax_handler() {
if ( isset( $_FILES['image'] ) ) {
$post_title = $_POST['title'];
$image = $_FILES['image'];
$post_content = $_POST['content'];
$new_post = array(
'post_title' => $post_title,
'post_content' => $post_content,
'post_status' => 'publish',
'post_name' => 'pending',
);
$pid = wp_insert_post( $new_post );
add_post_meta( $pid, 'meta_key', true );
if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) {
require_once ABSPATH . 'wp-admin' . '/includes/image.php';
require_once ABSPATH . 'wp-admin' . '/includes/file.php';
require_once ABSPATH . 'wp-admin' . '/includes/media.php';
}
if ( $_FILES ) {
foreach ( $_FILES as $file => $array ) {
if ( $_FILES[ $file ]['error'] !== UPLOAD_ERR_OK ) {
return 'upload error : ' . $_FILES[ $file ]['error'];
}
$attach_id = media_handle_upload( $file, $pid );
}
}
if ( $attach_id > 0 ) {
// and if you want to set that image as Post then use:
update_post_meta( $pid, '_thumbnail_id', $attach_id );
}
}
echo json_encode( 'success' );
die();
}
add_action( 'wp_ajax_my_ajax_handler', 'my_ajax_handler' );
add_action( 'wp_ajax_nopriv_my_ajax_handler', 'my_ajax_handler' );
对我来说很管用。我想知道它是否适用于您,并确保在您已经排队的JS文件的主插件文件中包含了ajax请求的PHP文件。
https://wordpress.stackexchange.com/questions/412365
复制相似问题