V
V
Vladimir Kulikov2020-05-19 16:27:43
PHP
Vladimir Kulikov, 2020-05-19 16:27:43

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


in code:

<?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 );



    }

?>


The script is called via ajax.

How to fix the error?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Igor, 2020-05-19
@it_proger29

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()

You include wp-load.php, but this file (if you haven't changed it) does not have this function.

A
Artem, 2020-05-19
@TMProject

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'),
    ));

register a handler for ajax in the function.php file
//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');
}

then the handler itself (you can also find it in the function.php file):
function custom_request_callback(){
          //Проверяем Ajax запрос на соответствие nonce коду, если не соответствует выходим
    check_ajax_referer('ajax-custom-request', 'nonce_code');
    
          //Если проверку прошли
  
          //Здесь обрабатываем запрос, здесь доступна $_POST

    wp_die();
   }

in js script:
let data = {
 action: 'ajax-custom-request',
 nonce_code: ajax.nonce,
 data: здесь сериализуем данные для передачи,
};
$.post(ajax.url, data, function(data, textStatus) {
 // здесь обработать ответ data
}, 'json');

https://wp-kama.ru/id_2018/ajax-v-wordpress.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question