0

I am working on my wordpress website and I want to keep a very low number of plugins. So I only write codes that I need. I was wondering what is the best practice of working with php, css and javascript files? I'll explain...

As far as my knowledge is concerned, I think there are only two options for organizing files:

  1. Write files with separate extensions (css, js and php) and include css and js inside the php file.

  2. Write only php file and put inside it js and css pure code.

Here is some code related to the options to further clarify what I mean. Which of the two options is more practical and safe? Also can there be any impact on performance or page loads?

Options 1: separate css and js which are included inside php files, in this case css and js are located in a specific directory as separate files.

<?php

/* My custom Template */

?><link href="https://mywebsite.it/wp-content/themes/theme-child/assets/my_custom.css" rel="stylesheet" type="text/css" ><?php
?><script type="text/javascript" src="https://mywebsite.it/wp-content/themes/theme-child/assets/my_custom.js" defer></script><?php

defined( 'ABSPATH' ) || exit;

<form class="my-form" action="" method="post">

My form content php code...

<fomr/>

Options 2: write css and js directly into php file, in this case as you can see I write css and js code directly into php file.

<?php

/* My custom Template */

<style type="text/css">
My css Content here
</style>

defined( 'ABSPATH' ) || exit;

<form class="my-form" action="" method="post">

My form content php code...

<fomr/>

<script>
My js Content here
</script>
Snorlax
  • 183
  • 4
  • 27
  • Either way, you can work on multiple files and use a plugin to merge them compress them or even inline them. – IT goldman Jul 02 '22 at 14:52
  • 2
    This is very subjective, but I think generally most WordPress developers would say a form of option 1 and to use the enqueuing system for JS and CSS. If you have a lot of per-page JS and more so CSS, you might be designing things the hard way. As for perf, that’s another topic. Byte count matters, so less is better, but request counts matter, too, and we’ve got good cacheing techniques. Inlining also has a place at times. – Chris Haas Jul 02 '22 at 14:54

1 Answers1

1

Option 1 would be the accepted method if you are coding everything yourself. You will have a performance hit because of it slowing down the page render though. If you are planning to only load scripts and styles on the pages needed would offset performance hit.

Personally I load enqueue scripts on pages I need them on only and don't inline anything, just be sure to use a good performance plugin like WP Rocket or Asset Cleanup, they are very good at minimizing performance hit and also have nice features for pre loading fonts, integrating CDN etc.

Lastly good call on minimizing plugins, less is better.

Edit: Would use this plugin though for loading custom scripts, has conditional control for only loading scripts on certain pages, bit more control and its pretty light - https://en-gb.wordpress.org/plugins/custom-css-js/

Connor Jovan Nel
  • 84
  • 1
  • 2
  • 4
  • Thanks for the suggestion, I really appreciate it. I have a problem, following option 1 then the css and js files are cached. This way every time I have to clear the browser cache to see the changes made. With option 2 all this does not happen, why do I have this problem? I don't think it's the browser and I don't have any caching plugin active, just CloudFlare from the hosting panel. Should we open another question ? – Snorlax Jul 02 '22 at 19:45
  • 1
    Use the version argument which will be passed in the query string. A common thing, especially during development, is to use [`filemtime`](https://stackoverflow.com/a/41258221/231316) – Chris Haas Jul 02 '22 at 19:58
  • @Chris Haas Thanks for your answer, I've tried but it's not working. Is there a reference that explains this problem well? – Snorlax Jul 02 '22 at 21:50
  • I found this reference and it is working for me. https://gist.github.com/MwieMarin/2d7a38d3825ed1bf44e82ef48b3badf9 – Snorlax Jul 02 '22 at 21:57
  • @Snorlax When using caching plugin only enable it after you're done building site or use incognito mode in chrome during development to offset caching , I just CTRL + F5 every time I make a change, habit at this point – Connor Jovan Nel Jul 03 '22 at 05:48