several admin_enqueue_scripts using the same script

hello,
can calling the same script with multiple admin_enqueue_scripts be a problem?
I use an autocomplete on a field and a click on a button which saves the result in the database.
I have a 404 error.


add_action( 'admin_enqueue_scripts', array( $this, 'add_books_author_js' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'add_tag_author_js' ) );
add_action('wp_ajax_nopriv_booticUIAutocomplete', array( $this, 'awp_autocomplete_author' ) );
add_action('wp_ajax_booticUIAutocomplete', array( $this, 'awp_autocomplete_author' ) );
add_action('wp_ajax_nopriv_booticUIAddTagAuthor', array( $this, 'awp_add_tag_author' ) );
add_action('wp_ajax_booticUIAddTagAuthor', 		array( $this, 'awp_add_tag_author' ) );

public function add_books_author_js() {
	wp_enqueue_script('books-author-js',get_stylesheet_directory_uri() . '/assets/js/books-author.js',
array( 'jquery', 'jquery-ui-autocomplete' ),'1.0',false);
			wp_localize_script(
				'books-author-js',
				'BooticUIAutocomplete',
				[
					'bootic_author_url' 	=> admin_url('admin-ajax.php'),
					'bootic_author_nonce' 	=> wp_create_nonce('BooticAuthorNonce'),
					'pid'					=> $this->pid
				]
			);
			$wp_scripts = wp_scripts();
			wp_enqueue_style('jquery-ui-css',
				'//ajax.googleapis.com/ajax/libs/jqueryui/' . $wp_scripts->registered['jquery-ui-autocomplete']->ver . '/themes/smoothness/jquery-ui.css',
				false, null, false
			);
		}

		public function add_tag_author_js() {
			wp_enqueue_script(
				'tags-author-js',
				get_stylesheet_directory_uri() . '/assets/js/books-author.js',
				array( 'jquery' ),
				'1.0',
				false
			);
			wp_localize_script(
				'tags-author-js',
				'BooticUIAddTagAuthor',
				[
					'bootic_add_author_url' 	=> admin_url('admin-ajax.php'),
					'bootic_add_author_nonce' 	=> wp_create_nonce('BooticAddTagAuthorNonce'),
					'pid'					=> $this->pid
				]
			);
		}

js

$('.books_author').autocomplete({
		source: function(request, response) {
			$.ajax({
				dataType: 'json',
				url: BooticUIAutocomplete.bootic_author_url,

				data: {
					term: request.term,
					action: 'booticUIAutocomplete',
					security: BooticUIAutocomplete.bootic_author_nonce,
				},

				success: function(data) {
					response($.map(data, function (item) {
						if(item.list != '')item.list+=', ';
						return {
							label: 	item.title,
							value: 	item.list + item.title + ', ',
							id: 	item.id,
							pid:	BooticUIAutocomplete.pid
						};
					}));
				}
			});
		},
		minLength: 3,
		select: function(event, ui) {
			console.log(ui.item.label+ ' ' + ui.item.id + ' ' + ui.item.pid);

			var author_id = $('#meta-box-author .inside #list-author-id');

			author_id.val( author_id.val() + ui.item.id + ', ' );
		},
	});

	/**
	 *
	 * @since 1.0
	 *
	 * @return {void}
	 */
	$('#books_author_add').click(function(){
		if( add_author_item() ) $('.books_author').val('');
		var authors = $('.books_author').val();
		/*
		$.ajax({
			url: BooticUIAddTagAuthor.bootic_add_author_url,
			type: 'POST',
			data: {
				authors: authors,
				action: 'BooticUIAddTagAuthor',
				security: BooticUIAddTagAuthor.bootic_add_author_nonce,
			},
			success: function(data) {
				console.log( 'data: ' + data );
			}
		});*/
	});
  • This topic was modified 2 days, 10 hours ago by vincentavct.
  • This topic was modified 2 days, 10 hours ago by vincentavct.



Source link