• Resolved Patte

    (@infosion)


    Hi,
    the ajax search works great – thanks for the cool extension.
    I can remove the CSS in principle and integrate it into my own scss files.
    I would also like to do away with script.min.js and include the content of the file itself. Is that possible?
    Patrick

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi @infosion,

    there is not an option to prevent the loading of the Javascript file but you can dequeue the file and then enqueue your own file:

    add_action( ‘wp_enqueue_scripts’, function () {
    wp_dequeue_script( ‘swp-live-search-client’ );
    }, 20 );

    Please note that the script depends on jQuery so that should be specified on your wp_register_script.

    I hope this helps!

    Thread Starter Patte

    (@infosion)

    Hey Elio,

    thanks much for your help! Works like a charm.

    Is there also a way to disable those inline scripts? I would use them only on specific pages.

    Thanks again!

    Patrick

    Hi @infosion,

    if you are using the block editor you can remove the inline scripts with this code:

    add_action( 'init', function () {
        $form = searchwp_live_search()->get( 'Form' );
        remove_action( 'wp_footer', [ $form, 'gutenberg_integration' ] );
    }, 20 );

    That said, after looking more into this, I believe the Live Ajax Search functionality may break when dequeueing the scripts as suggested before.
    This is because the scripts also needs to be localized with extra data. If you enqueue the scripts on your custom file the localization would not work.

    My suggestion would be to let the Live Ajax Search load its own files and scripts but use the code below to limit them only on specific pages:

    add_action( 'init', function () {
    
    	// List of post IDs where the Live Ajax Search should be loaded
    	$swp_live_ajax_pages = [123,456];
    
    	// If the current page is not in the list prevent the scripts from loading
    	if ( ! in_array( get_queried_object_id(), $swp_live_ajax_pages ) ) {
    		return;
    	}
    
    	$form = searchwp_live_search()->get( 'Form' );
    	remove_action( 'wp_footer', [ $form, 'gutenberg_integration' ] );
    
    	add_action( 'wp_enqueue_scripts', function () {
    		wp_dequeue_script( 'swp-live-search-client' );
    	}, 20 );
    }, 10 );

    I hope this helps!

    Thread Starter Patte

    (@infosion)

    Hey Elio,

    works perfect – thanks for the great support!

    Patrick

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘script.min.js only on relevant pages’ is closed to new replies.