# Allow Custom JS in Block

**URL:** https://forum.pinegrow.com/t/allow-custom-js-in-block/8452
**Category:** Feature Request
**Tags:** feature-request, wordpress
**Created:** [October 20, 2023, 8:25pm UTC](https://forum.pinegrow.com/t/allow-custom-js-in-block/8452 "2023-10-20T20:25:58Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![jonroc](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/jonroc/32/2805_2.png) [@jonroc](https://forum.pinegrow.com/u/jonroc)
#### Post date: [October 20, 2023, 8:25pm UTC](https://forum.pinegrow.com/t/allow-custom-js-in-block/8452/1 "2023-10-20T20:25:58Z")

</div>

Similar to how we have the Expresions option in the block attributes, I want to be able to write custom JS (primarily to write if statements) in the Expressions block using the attribute IDs. I can think of many uses for this, but my immediate need is to create a `<style></style>` code block which I want to be able to populate dynamically based on the attributes chosen. I’m trying to avoid a huge CSS library that covers every possible media query and class (like BS or TW) and only inject the styles as needed on each page. But there have been other times I wish I had more control over the output. -Thanks!

---

<div class="post-metadata">

### Author: ![matjaz](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/matjaz/32/5_2.png) [@matjaz](https://forum.pinegrow.com/u/matjaz)
#### Post date: [October 23, 2023, 9:36am UTC](https://forum.pinegrow.com/t/allow-custom-js-in-block/8452/2 "2023-10-23T09:36:04Z")

</div>

@jonroc there are a couple of things that make this a bit complicated:

- You would have to write React code for constructing virtual DOM
- Such blocks could not be Hybrid blocks because JS is not executed on the backend

If you mainly need to write IF statements the Block Condition action might be a better fit.

---

<div class="post-metadata">

### Author: ![jonroc](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/jonroc/32/2805_2.png) [@jonroc](https://forum.pinegrow.com/u/jonroc)
#### Post date: [October 23, 2023, 5:25pm UTC](https://forum.pinegrow.com/t/allow-custom-js-in-block/8452/3 "2023-10-23T17:25:20Z")

</div>

Ok, I see the issue here. The Block Conditions are not advanced enough for what I need.

One thing I’ve been doing is creating 99% of the block in PG and then moving the code that is generated for WP into a PG blocks folder. This exports the block code to the theme, but since I no longer need the initial setup for the block, I disable that block in PG, and the only thing I have to do is adjust the block’s code in a text editor to what I need and manually enqueue the block. The problem I have is that the inc/custom.php is called before the blocks, so I don’t have access to the PG helper files that are needed to run the block properly.

Is there no way to put the [inc/custom.php at the bottom of function.php](https://forum.pinegrow.com/t/move-require-once-inc-custom-php-to-bottom-of-functions-php/8250/1) so I can have these custom blocks work quickly and easily? In the meantime, I can add the enqueued files manually at the bottom of functions.php, but that’s dangerous because we have no control over functions.php, and it’s easy for a team member to accidentally overwrite this.

-Thanks!

---

<div class="post-metadata">

### Author: ![matjaz](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/matjaz/32/5_2.png) [@matjaz](https://forum.pinegrow.com/u/matjaz)
#### Post date: [October 24, 2023, 8:22am UTC](https://forum.pinegrow.com/t/allow-custom-js-in-block/8452/4 "2023-10-24T08:22:56Z")

</div>

@jonroc the best way is to use WP hooks to ensure that the code runs at the correct time. For example, blocks are registered in the `init` hook with the default priority 10.

If you wish to run some code after all the blocks are registered use this in `inc/custom.php`:

```auto
add_action( 'init',function() {
    //do something
}, 100);

```

The priority parameter `100` causes this hook to be called at the end.

(posting the same to [the linked topic](https://forum.pinegrow.com/t/move-require-once-inc-custom-php-to-bottom-of-functions-php/8250/1))
