T
T
trenton2021-08-18 21:36:37
AJAX
trenton, 2021-08-18 21:36:37

What variable corresponds to this if without woocommerce connection?

I want to use a script that was written for woocommerce taxonomies (not by me, I found it and used it many times), but this time when woocommerce is not connected. It turned out to replace taxonomies with others, but the console does not determine

var ajax_url = woocommerce_params.ajax_url;
for obvious reasons. I ran a search through the code, this variable is not manually defined anywhere, so it is out of the box.
And how to replace it for a regular wordpress without woocommerce? What does it correspond to? What record?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Nesterov, 2021-08-19
@trenton

In simple terms, this is a javascript variable that is needed to work with ajax.
It's in the admin by default.
Woocommerce adds it when installing the plugin.
That is, without Woocommerce, you need to add it yourself, since it is not included in your theme by default.
Anatoly gave you a link to the documentation above - everything is described in detail on this topic.
To simplify it further - you need to figure it out and add code like this -

// Подключаем локализацию в самом конце подключаемых к выводу скриптов, чтобы скрипт
// 'twentyfifteen-script', к которому мы подключаемся, точно был добавлен в очередь на вывод.
// Заметка: код можно вставить в любое место functions.php темы
add_action( 'wp_enqueue_scripts', 'myajax_data', 99 );
 function myajax_data(){

// Первый параметр 'twentyfifteen-script' означает, что код будет прикреплен к скрипту с ID 'twentyfifteen-script'
// 'twentyfifteen-script' должен быть добавлен в очередь на вывод, иначе WP не поймет куда вставлять код локализации
// Заметка: обычно этот код нужно добавлять в functions.php в том месте где подключаются скрипты, после указанного скрипта
  wp_localize_script( 'twentyfifteen-script', 'myajax',
    array(
      'url' => admin_url('admin-ajax.php')
    )
  );

}

where you need to replace the name of the script "twentyfifteen-script" with your own.
As a result, you will have a variable (in this example it will be called url - you can call it whatever you like) that will store the url for ajax requests and which you can access from javascript.

P
Pychev Anatoly, 2021-08-18
@pton

This article will help you understand the principles of wordpress ajax, and also describes the nature of the ajaxurl variable and how to use it or replace it with your own.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question