简体   繁体   English

如何在我的 functions.php 文件中加入多个 Wordpress AJAX 脚本?

[英]How do I enqueue multiple Wordpress AJAX scripts in my functions.php file?

I am currently trying to enqueue 2 Wordpress AJAX scripts in my functions.php file, but I seem to have hit an error.我目前正在尝试在我的 functions.php 文件中加入 2 Wordpress AJAX 脚本,但我似乎遇到了错误。 The first AJAX script works fine, but for the second I keep getting a 500 Internal Server Error.第一个 AJAX 脚本工作正常,但第二个脚本我一直收到 500 内部服务器错误。

What I have so far in my AJAX scripts is this:到目前为止,我的 AJAX 脚本中的内容是:

This first block is for my first AJAX script located in my-ajax-script.js .第一个块用于我位于my-ajax-script.js中的第一个 AJAX 脚本。

function lesson_watched_call(lesson_id_value) {  
    console.log('Lesson watched fn called.');
    console.log(lesson_id_value);
    $.post(my_ajax_obj.ajax_url, {
        _ajax_nonce: my_ajax_obj.nonce,  // nonce
        action: "lesson_watched", // action call
        lesson_value: lesson_id_value // data
    });
}

This second block is for my second AJAX script located in fav-ajax-script.js .第二个块用于我位于fav-ajax-script.js中的第二个 AJAX 脚本。

function toggleFavTut(tutorial_id_value) {  
    console.log('Tutorial toggle fn called.');
    console.log(tutorial_id_value);
    $.post(my_ajax_obj.ajax_url, {
        _ajax_nonce: my_ajax_obj.nonce,  // nonce
        action: "tutorial_fav_toggle", // action call
        tutorial_value: tutorial_id_value // data
    });
}

In my functions.php file, this is what I have:在我的functions.php文件中,这是我所拥有的:

/*---------------------------------------------------------------------------------*/
/*  Enqeue AJAX Script and Define Lesson Watched Function  */
/*---------------------------------------------------------------------------------*/
add_action( 'wp_enqueue_scripts', 'enqueue_ajax_scripts' );
function enqueue_ajax_scripts() {

  wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/js/my-ajax-script.js', array('jquery') );
  wp_localize_script( 'ajax-script', 'my_ajax_obj',
          array( 'ajax_url' => admin_url( 'admin-ajax.php' ),
                 'nonce'    => wp_create_nonce( 'lesson_nonce_value' ),
          ) 
        );

  wp_enqueue_script( 'ajax-script2', get_template_directory_uri() . '/js/fav-ajax-script.js', array('jquery') );
  wp_localize_script( 'ajax-script2', 'my_ajax_obj',
          array( 'ajax_url' => admin_url( 'admin-ajax.php' ),
                 'nonce'    => wp_create_nonce( 'tutorial_nonce_value' ),
          ) 
        );
}

function lesson_watched() {
    $lastValue_php = wp_unslash( $_POST['lesson_value'] );
    $current_user_id = get_current_user_id();

    $found = get_user_meta( $current_user_id, 'lessons_watched_ids', true );

    if ( !in_array( $lastValue_php, $found ) ) :
      if( empty( $found ) ) :  
        update_user_meta( $current_user_id, 'lessons_watched_ids', array( $lastValue_php ) );
      else :
        $found[] = $lastValue_php;
        update_user_meta( $current_user_id, 'lessons_watched_ids', $found );
      endif;
    endif;

    wp_die(); // Ends all AJAX handlers
}

add_action('wp_ajax_nopriv_lesson_watched', 'lesson_watched');
add_action('wp_ajax_lesson_watched', 'lesson_watched');

/*---------------------------------------------------------------------------------*/
/*  Enqeue AJAX Script and Define Tutorial Toggle Function  */
/*---------------------------------------------------------------------------------*/
function tutorial_fav_toggle() {
  $tutorial_value = wp_unslash( $_POST['tutorial_value'] );
  $current_user_id = get_current_user_id();

  $found = get_user_meta( $current_user_id, 'tutorial_favorited_ids', true );

  if ( !in_array( $tutorial_value, $found ) ) :
    $found[] = $tutorial_value;
    update_user_meta( $current_user_id, 'tutorial_favorited_ids', $found );
  else:
    $key = array_search( $tutorial_value, $found );
    unset( $found[$key] );
    update_user_meta( $current_user_id, 'tutorial_favorited_ids', $found );
  endif;

  wp_die(); // Ends all AJAX handlers
}

add_action('wp_ajax_nopriv_tutorial_fav_toggle', 'tutorial_fav_toggle');
add_action('wp_ajax_tutorial_fav_toggle', 'tutorial_fav_toggle');

I first start out by enqueueing both scripts and then localizing each script.我首先将两个脚本加入队列,然后对每个脚本进行本地化。 I'm wondering if maybe I have to use a different variable for some of the localize variables, but I am not certain about that.我想知道我是否必须为某些本地化变量使用不同的变量,但我不确定。 Would you have an tips on what to try to debug?你有关于尝试调试什么的提示吗?

Per the docs , the second parameter to wp_localize_script should be unique.根据文档wp_localize_script的第二个参数应该是唯一的。

$object_name is the name of the variable which will contain the data. $object_name 是将包含数据的变量的名称。 Note that this should be unique to both the script and to the plugin or theme.请注意,这对于脚本和插件或主题都应该是唯一的。 Thus, the value here should be properly prefixed with the slug or another unique value, to prevent conflicts.因此,此处的值应该以 slug 或其他唯一值作为前缀,以防止冲突。

So, following your pattern the second function could use my_ajax_obj2 for the variable.因此,按照您的模式,第二个 function 可以使用my_ajax_obj2作为变量。

  wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/js/my-ajax-script.js', array('jquery') );
  wp_localize_script( 'ajax-script', 'my_ajax_obj',
          array( 'ajax_url' => admin_url( 'admin-ajax.php' ),
                 'nonce'    => wp_create_nonce( 'lesson_nonce_value' ),
          ) 
        );

  wp_enqueue_script( 'ajax-script2', get_template_directory_uri() . '/js/fav-ajax-script.js', array('jquery') );
  wp_localize_script( 'ajax-script2', 'my_ajax_obj2',
          array( 'ajax_url' => admin_url( 'admin-ajax.php' ),
                 'nonce'    => wp_create_nonce( 'tutorial_nonce_value' ),
          ) 
        );

Do take care to make your variable specific to your application, too.也请注意使您的变量特定于您的应用程序。 These variables all effectively are just properties on the global window object and everything can read/write them, so make sure they are unique.这些变量实际上都是全局window object 上的属性,任何东西都可以读/写它们,所以要确保它们是唯一的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Wordpress子主题functions.php中正确排队retina.js? - How do I enqueue retina.js in a Wordpress child theme functions.php properly? 损坏的functions.php; 如何将脚本从引导移动到WordPress? - Broken functions.php; how to Move scripts From bootstrap into WordPress? 将wordpress中的样式表和脚本与functions.php链接 - linking stylesheets and scripts in wordpress with functions.php 使用wp_enqueue_script()在Wordpress-functions.php中添加Javascript - Add Javascript in Wordpress - functions.php with wp_enqueue_script() WordPress functions.php wp_enqueue_script无效 - WordPress functions.php wp_enqueue_script not working 在function.php中排队脚本后,Javascript无法在wordpress上运行 - Javascript not working on wordpress after enqueue script in functions.php 尝试在我的 Wordpress PHP 文件中将几个 Javascript 脚本排入队列后,如何防止“无法在模块外使用导入语句?” - After trying to enqueue several Javascript scripts in my Wordpress PHP file, how can I prevent “cannot use import statement outside a module?” 将Javascript放入functions.php中 - Enqueue Javascript in functions.php 如何在WordPress`functions.php`中将一个脚本排入另一个脚本,并使它们一起工作? - How to enqueue one script after another in WordPress `functions.php`, and have them both work together? 为什么在WordPress中我无法使用functions.php文件中的钩子来加载JavaScript? - Why in WordPress I can't load my JavaScripts using an hook inside my functions.php file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM