Last active
June 7, 2022 13:52
-
-
Save iamroi/6a6f8286246c9852b45381d07b002f1d to your computer and use it in GitHub Desktop.
Wordpress <3 Tailwind Functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Do this once to initiate the setup | |
* Works only on Local development. Configure isLocal() to your taste | |
* Run yourwebsite.dev?tw-init. This will create ANY existing posts- | |
* -as HTML files in your theme-root/posts-html directory | |
* Make sure to .gitignore the posts-html directory | |
* | |
* Update the Tailwind purge config like below and | |
* also add it to the Browsersync watcher list if you are using | |
* | |
* purge: [ | |
* './posts-html/*.html', | |
* ... | |
* ] | |
*/ | |
function init_build_html_files_for_tailwind() { | |
if(!isset($_GET['tw-init']) || !isLocal()) return; | |
if (!file_exists(get_stylesheet_directory().'/posts-html')) { | |
mkdir(get_stylesheet_directory().'/posts-html', 0777, true); | |
} | |
$args = array( | |
'post_status' => 'publish', | |
'post_type' => 'any', | |
'posts_per_page' => -1, | |
); | |
$postsResult = get_posts($args); | |
foreach($postsResult as $k => $postItem) { | |
put_post_content_to_file($postItem); | |
} | |
die('HTML files created for Tailwind ;)'); | |
} | |
add_action( 'init', 'init_build_html_files_for_tailwind', 10, 3); | |
/** | |
* Update the HTML file on post save | |
* | |
* @param $post_id | |
* @param $post | |
* @param $update | |
*/ | |
function update_post_html_for_tailwind( $post_id, $post, $update ) | |
{ | |
put_post_content_to_file($post, $overwrite = true); | |
} | |
add_action( 'save_post', 'update_post_html_for_tailwind', 10,3 ); | |
/** | |
* Save the full HTML content of a post to file | |
* | |
* @param $post | |
* @param bool $overwrite | |
*/ | |
function put_post_content_to_file($post, $overwrite = false) | |
{ | |
$excluded_post_types = ['revision']; | |
if(in_array($post->post_type, $excluded_post_types)) return; | |
$file_path = get_stylesheet_directory().'/posts-html/'.$post->ID.'.html'; | |
if(file_exists($file_path) && $overwrite === false) return; | |
file_put_contents($file_path, apply_filters('the_content', $post->post_content)); | |
} | |
// HELPER FUNCTIONS | |
/** | |
* @return bool | |
*/ | |
function isLocal() | |
{ | |
if (!isset($_SERVER['HTTP_HOST'])) return false; | |
if (strstr($_SERVER['HTTP_HOST'], '.test') || | |
strstr($_SERVER['HTTP_HOST'], '.development') || | |
strstr($_SERVER['HTTP_HOST'], '.dev') || | |
strstr($_SERVER['HTTP_HOST'], '.staging') || | |
strstr($_SERVER['HTTP_HOST'], 'loc.') || | |
strstr($_SERVER['HTTP_HOST'], 'localhost')) { | |
return true; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is the WP solution I'm currently using to make use of
purge
and my production build is always in KBs.This method will also work with shiny new JIT. But not the 'real time' update and preview for Gutenberg blocks in the backend unfortunately.
Please comment if you know a better method to do this and to support JIT for Gutenberg