• Resolved Insomnia88

    (@insomnia88)


    Hi,

    I am developing my first “bigger” plugin with the goal to compress and merge css/js files.
    I know that there are other plugins that already do that but I want to have specific behavior, look and feel and the most important learn how to do it myself to get an understanding of how the things work.

    This is the setup:

    Plugin file in plugin folder -> here the admin can setup the path to the js/css files that need to be compressed and change the order of what files should come first.
    compress.php inside a lib folder -> when the admin hits the save button all the needed data is send to the compress.php that “do the magic”. (compressing and merging)

    So far so good, everything is working. But now I want to enqueue the final compressed file “automatically” in the current used theme and I have no clue how to manage that. Is it even possible? I was in the wordpress help chat before and got the hint that my approach is “wrong”.

    The thing is, I know other plugins are able to do that (but they are kinda too complex for me).

    Oh, “just” registering/enqueuing the files in the compress.php isn’t working. Because the file is not included in the normal “frontend”/theme and only for admins in backend.

Viewing 2 replies - 1 through 2 (of 2 total)
  • catacaustic

    (@catacaustic)

    Oh, “just” registering/enqueuing the files in the compress.php isn’t working. Because the file is not included in the normal “frontend”/theme and only for admins in backend.

    That’s easy then. Make sure that the file is included in the front-end. That should be easy enough to do by incuding the file in the main plugins file.

    As far as outputting the minified script and CSS, the way that I’d do it (which might not be optimal so if anyone else has better ideas, pelase let us all know!) is like this:

    1. Hook into the wp_enqueue_script action at the last possible time. I’d use a priority of PHP_IT_MAX to do that.
    2. Gather all of the currently queued JS and CSS files and set these in your minifying system
    3. De-queue the existing JS and CSS files
    4. Enqueue your plugins JS and CSS output

    There is most likely other ways to do it, but that would make the most sense to me personally.

    Thread Starter Insomnia88

    (@insomnia88)

    Well, it was indeed actually pretty easy!

    I just add a function to my index plugin file that hooks into the wp_enqueue_script action like you mentioned.
    The files are now included in the frontend. I don’t even need PHP_IT_MAX. ??

    Thanks for the advice.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘own minifier plugin wrong approach?’ is closed to new replies.