Answer the question
In order to leave comments, you need to log in
What is wrong with wp_insert_post?
Good afternoon, please help me solve the problem, an error appears
Fatal error: Uncaught Error: Call to undefined function wp_insert_post() in A:\XAMP\htdocs\test\wp-content\themes\test\shop_scripts\add_cart.php:29
Stack trace:
#0 {main}
thrown in A:\XAMP\htdocs\test\wp-content\themes\test\shop_scripts\add_cart.php on line 29
<?php
define('SHORTINIT', true);
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );
$add_cart_number = $_POST["add_cart_number"];
$add_cart_name = $_POST["add_cart_name"];
$add_cart_description = $_POST["add_cart_description"];
$add_cart_price = $_POST["add_cart_price"];
$add_cart_src = $_POST["add_cart_src"];
if($add_cart_number=="" or $add_cart_name=="" or $add_cart_description=="" or $add_cart_price=="" or $add_cart_src==""){
echo "Заполните все поля";
}
else{
echo $add_cart_number . '<br>';
echo $add_cart_name . '<br>';
echo $add_cart_description . '<br>';
echo $add_cart_price . '<br>';
echo $add_cart_src . '<br><br>';
$_post_data = array(
'post_title' => $add_cart_name,
'post_status' => 'publish',
'post_author' => $user_ID,
'post_type' => 'cart',
);
$post_id = wp_insert_post( $_post_data );
// image
update_field( "field_5ec3c890522fd", $add_cart_src, $post_id );
// price
update_field( "field_5ec3c87e522fb", $add_cart_price, $post_id );
//description
update_field( "field_5ec3c88a522fc", $add_cart_description, $post_id );
// количество
update_field( "field_5ec3c8af522fe", $add_cart_number, $post_id );
}
?>
Answer the question
In order to leave comments, you need to log in
Questions like this are always solved the same way - read the error.
Fatal error: Uncaught Error: Call to undefined function wp_insert_post()
translates asНеустранимая ошибка: Необработанная ошибка: Вызов неопределенной функции wp_insert_post()
So what is not clear,
Call to undefined function wp_insert_post()
He does not know what this function is.
--
What needs to be done:
Localize parameters, connect scripts
wp_localize_script('тут handle js', 'ajax', array(
'url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('ajax-custom-request'),
));
//ajax_init
add_action('init', 'auth_ajax_init');
function auth_ajax_init(){
//для не авторизированных
add_action('wp_ajax_nopriv_custom_request, 'custom_request_callback');
//для авторизированных
add_action('wp_ajax_custom_request', 'custom_request_callback');
}
function custom_request_callback(){
//Проверяем Ajax запрос на соответствие nonce коду, если не соответствует выходим
check_ajax_referer('ajax-custom-request', 'nonce_code');
//Если проверку прошли
//Здесь обрабатываем запрос, здесь доступна $_POST
wp_die();
}
let data = {
action: 'ajax-custom-request',
nonce_code: ajax.nonce,
data: здесь сериализуем данные для передачи,
};
$.post(ajax.url, data, function(data, textStatus) {
// здесь обработать ответ data
}, 'json');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question