Deal with PHP or Javascript code in Wordpress

Hi,

Usually when I create a subpage with a form or something that needs to run php or javascript code, I like to write this code directly in the html structure () or above the html (<?php). In Wordpress, it turns out that it’s not so easy there.

Javascript code in the tag is missing on the WP-Website. I figured out that it appears and works by adding the “Do not enqueue” smart action in Pinegrow WP.

Because of the main content structure (Site Content) in Wordpress (everything above is header.php, everything below is footer.php) php code written above the html wouldn’t be exported as well. I could try to write it inside the main (Site Content) area of the page but it feels a little weird?

I think I would try to embed the php and javascript code somehow and export the page with the form as a block or page template, that I can use in Wordpress after adding the subpage in the pages tab in WP.

Does this make sense? Or do you know a better way dealing with complex forms or other things that need php or javascript to work in Wordpress?

@matjaz do you have an idea?

@Riccarcharias one way would be, as you suggest, to simply add PHP code and JS code (with Do not enqueue) to the Block. This could work.

Although, the official WP way would probably be to hook relevant WP actions and filters.

I would try with the first approach and see if it works. It makes sense to have the form functionality nicely encapsulated into a block or template.

Anybody else has a better way?

1 Like

I experimented with a page template which worked quite well. It seems like I do not have to enqueue the code in a page template…And I have to write the PHP script in the area (I’m not sure if this could be a problem / is unusual, normally I write my PHP script above the HTML).

A Block would make more sense for a form I want to use on several sites. I’m sure I’ll try this in the near future, too.