Pinegrow WP best practice to add multi-line custom PHP in templates

Hello,

Pinegrow WP works well even on complex custom theme development projects, and lets you add custom PHP (PHP that’s not available as built-in actions) to templates multiple ways. For example, this guide suggests to use the ‘Function’ global smart action (either by adding simple custom code, or by calling a named function that lives in ‘/inc/custom.php’): Adding custom PHP code | Pinegrow Web Editor

My problem is, that sometimes I need to add multi-line custom PHP to templates (e.g. for declaring variables), and I can’t / don’t want to use named functions. In this cases, the ‘Function’ global smart action’s PHP editor window is very limited, and the code neither is optimal to write in VSCode (because of PG WP project files being html by default). I send you a screenshot to illustrate my current workflow as an example (adding a <div>, and putting a ‘Function’ on it with custom PHP code to replace the element):

I can work around the issue by e.g. using PHP include / require statements for larger PHP code blocks like the one above (and only write the include statement within PG), but that requires a lot of extra steps. Regarding this, I’ve a couple of questions:

  • Can’t I just enable Server-side scripts within Pinegrow on WP projects, and set PG WP project files’ extensions to PHP instead of HTML, so that I can use simple PHP blocks with all the advantages?
  • The latter I tried, but didn’t seemed to work well (as Page settings options seemed to break / change).
  • I’m very interested in what you suggest me to do in orther to be able to use PG WP up to its full potential, while still being able to write PHP code with syntax highlighting and indenting (in VSCode) when needed? Maybe there’s already a better way, which I don’t know.

(I remember there was an old tutorial on your website on using ACF Flexible content which used native PHP blocks within a template as I recall, but I’m not sure that’s possible with current versions of Pinegrow.)

Thank you for your suggestions and tips in advance!

Best,
Márton Lente

@martonlente you can use normal <?php ?> code blocks in source project HTML files. PG knows how to handle them:

In addition, you can activate Server side scripts for the HTML page (in File - Manage libraries and plugins):

This adds the Visual helper option to hide yellow PHP code blocks on the page view:

Hope this helps!

1 Like

Hello @matjaz,

Thank you for your quick answer! It’s already much better as you suggest, and I also like how server-side scripts are displayed within Pinegrow. I definitely prefer this over what I captured on the screenshot. :+1:

However, there’s still one thing missing: as the files has an HTML extension, VSCode doesn’t do syntax highlighting for PHP code blocks. Do you think there’s a workaround for this? (I assume PG WP files’ extensions shouldn’t be changed from HTML to PHP in order to achieve this, but please let me know if that would work.)

Thanks,
Márton

@martonlente this would be the question for the VS Code, more than for PG. Perhaps VSC has an extension that lets you force the code language (PHP in your case) for any document?

Changing the source file extensions to PHP will not work in PG because these files will be just copied to the theme, without any processing.

1 Like

Hello @matjaz,

Thank you for your feedback. You must be right: I might just need to enable PHP syntax highlighting (and indenting) on HTML files within VSC: which I assume can be done one way or another.

Ok: now I see how extensions work within PG WP, thanks for the clarification.

As a feature request, please consider adding PHP syntax highlighting within Pinegrow WP’s custom code fields, so that writing PHP code within a template file would be more efficient in PG.

Best,
Márton

1 Like