# How to initialize JS inside of the Block Editor (PHP Block)

**URL:** <https://forum.pinegrow.com/t/how-to-initialize-js-inside-of-the-block-editor-php-block/8915>\
**Category:** Blocks\
**Created:** [May 16, 2024, 11:53am UTC](https://forum.pinegrow.com/t/how-to-initialize-js-inside-of-the-block-editor-php-block/8915 "2024-05-16T11:53:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![MichelyWeb](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/michelyweb/32/6652_2.png) [@MichelyWeb](https://forum.pinegrow.com/u/MichelyWeb)\
**Post date:** [May 16, 2024, 11:53am UTC](https://forum.pinegrow.com/t/how-to-initialize-js-inside-of-the-block-editor-php-block/8915/1 "2024-05-16T11:53:07Z")

</div>

I have a slider block (created with Splide) which is a PHP Block that fetches a CPT.  
On the frontend the Slider JS gets inizialized. But on the Backend it’s not working. The slides are hidden (aka not initialized JS).

I was able to get it working with a method [mentioned in this GitHub comment](https://github.com/WordPress/gutenberg/issues/8379#issuecomment-2051812010). But as soon as the slider is used it gets hidden again.

How to solve this the right way?

---

<div class="post-metadata">

**Author:** ![Guido](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/guido/32/4958_2.png) [@Guido](https://forum.pinegrow.com/u/Guido)\
**Post date:** [May 17, 2024, 1:38pm UTC](https://forum.pinegrow.com/t/how-to-initialize-js-inside-of-the-block-editor-php-block/8915/2 "2024-05-17T13:38:41Z")

</div>

Hi @MichelyWeb

You have to load the splide.js in the block so that it uses it in the back-end and front-end. The initialization code you can put also in the splide.js at the bottom. Since you can only load one js file in the block.

That should work. Been a while that i did it that way. I now am using Alpine.js to initialize the slider and that works great.

Guido

---

<div class="post-metadata">

**Author:** ![MichelyWeb](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/michelyweb/32/6652_2.png) [@MichelyWeb](https://forum.pinegrow.com/u/MichelyWeb)\
**Post date:** [May 18, 2024, 2:21pm UTC](https://forum.pinegrow.com/t/how-to-initialize-js-inside-of-the-block-editor-php-block/8915/3 "2024-05-18T14:21:14Z")

</div>

I am using the action `enqueue_block_editor_assets` for enqueueing assets manually. That way i am able to add multiple assets per block. Using the method you mentioned does not help (because the splide Script is loaded as a dependency).

Is there an alpine.js based slider you would recommend?

---

<div class="post-metadata">

**Author:** ![Guido](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/guido/32/4958_2.png) [@Guido](https://forum.pinegrow.com/u/Guido)\
**Post date:** [May 19, 2024, 12:54pm UTC](https://forum.pinegrow.com/t/how-to-initialize-js-inside-of-the-block-editor-php-block/8915/4 "2024-05-19T12:54:27Z")

</div>

Ah oke yes manually that way is a good option. I use splide.js and alpine.js to initialize the slider. Alpine also has its own slider/carousel but i like splide and never really tried the alpine slider.

You can initialize it like this:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/9/99617ecc535bd541650b7fc2bde24dfe2697600a.png)

And i load the slider.js in the block. That is working great for me on front-end and back-end.

---

<div class="post-metadata">

**Author:** ![MichelyWeb](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/michelyweb/32/6652_2.png) [@MichelyWeb](https://forum.pinegrow.com/u/MichelyWeb)\
**Post date:** [May 19, 2024, 2:57pm UTC](https://forum.pinegrow.com/t/how-to-initialize-js-inside-of-the-block-editor-php-block/8915/5 "2024-05-19T14:57:04Z")

</div>

Thank you for the example code. I’ll try it that way next week.

---

<div class="post-metadata">

**Author:** ![MichelyWeb](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/michelyweb/32/6652_2.png) [@MichelyWeb](https://forum.pinegrow.com/u/MichelyWeb)\
**Post date:** [May 28, 2024, 5:04pm UTC](https://forum.pinegrow.com/t/how-to-initialize-js-inside-of-the-block-editor-php-block/8915/6 "2024-05-28T17:04:29Z")

</div>

Get an Error with this setup:

 ![CleanShot 2024-05-28 at 19.04.59@2x](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/a/ace8564c9c97595d93f05e49e5a2aec5123648d6.png)

The error/warning:

 ![CleanShot 2024-05-28 at 19.03.30@2x](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/9/94530d5147750b698079444694e06430fa6e6a0a.png)

---

<div class="post-metadata">

**Author:** ![MichelyWeb](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/michelyweb/32/6652_2.png) [@MichelyWeb](https://forum.pinegrow.com/u/MichelyWeb)\
**Post date:** [May 29, 2024, 3:20pm UTC](https://forum.pinegrow.com/t/how-to-initialize-js-inside-of-the-block-editor-php-block/8915/8 "2024-05-29T15:20:40Z")

</div>

Okay, after changing the Block Type from ‘dynamic PHP’ to ‘ACF’ the slider is working in Backend also. The Block itself has no fields because it’s just a PHP Query Loop. But whatever, now it works.

---

<div class="post-metadata">

**Author:** ![Guido](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/guido/32/4958_2.png) [@Guido](https://forum.pinegrow.com/u/Guido)\
**Post date:** [May 30, 2024, 12:10pm UTC](https://forum.pinegrow.com/t/how-to-initialize-js-inside-of-the-block-editor-php-block/8915/9 "2024-05-30T12:10:49Z")

</div>

Hi @MichelyWeb

ha strange! I always have it on Dynamic PHP. But i use it in a theme not a plugin. So that might be the issue. Good to hear it is working now! 😉
