Skip to content
This repository was archived by the owner on May 8, 2021. It is now read-only.

Commit 160cb1d

Browse files
committed
Add WordPress examples
1 parent e89baa0 commit 160cb1d

File tree

9 files changed

+180
-0
lines changed

9 files changed

+180
-0
lines changed

Diff for: examples/wordpress/.gitignore

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
.DS_Store
2+
node_modules
3+
/dist
4+
5+
# local env files
6+
.env.local
7+
.env.*.local
8+
9+
# Log files
10+
npm-debug.log*
11+
yarn-debug.log*
12+
yarn-error.log*
13+
14+
# Editor directories and files
15+
.idea
16+
.vscode
17+
*.suo
18+
*.ntvs*
19+
*.njsproj
20+
*.sln
21+
*.sw?

Diff for: examples/wordpress/README.md

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# WordPress
2+
3+
Setting up TailwindCSS in WordPress is relatively simple. Navigate to your WordPress theme folder:
4+
5+
```bash
6+
cd wp-content/themes/{your_theme}
7+
```
8+
9+
First thing we need to do is create a package.json file.
10+
11+
```bash
12+
npm init
13+
```
14+
15+
Follow the instructions and once it's done, the next step is to install Tailwind itself.
16+
17+
```bash
18+
npm install tailwindcss -D
19+
```
20+
21+
We then need to create a configuration file. To do this, run the following in your theme folder:
22+
23+
```bash
24+
./node_modules/.bin/tailwind init
25+
```
26+
27+
This will create the config file for called `tailwind.config.js` within your theme folder.
28+
29+
Because WordPress doesn't come with a compilation tool built in, we need to install one. For this tutorial, we'll be using Laravel Mix, which is a wrapper built around Webpack. It's much easier to use and works perfectly fine with WordPress.
30+
31+
## Laravel Mix
32+
33+
In your WordPress theme folder, install Laravel Mix:
34+
35+
```bash
36+
npm install laravel-mix
37+
```
38+
39+
For the sake of this example, we're going to create our CSS file in `resources/css/style.css`. This is in your theme folder, so it'll be something like `wp-content/themes/{your_theme}/resources/css/style.css`.
40+
41+
In that file, add:
42+
43+
```postCss
44+
@tailwind base;
45+
@tailwind components;
46+
@tailwind utilities;
47+
```
48+
49+
Now create a file called `webpack.mix.js` in your theme folder. In it, add the following:
50+
51+
```javascript
52+
const mix = require('laravel-mix');
53+
const tailwindcss = require('tailwindcss');
54+
55+
mix.postCss('./resources/css/style.css', './css/style.css',
56+
tailwindcss('./tailwind.config.js')
57+
);
58+
```
59+
60+
We also need to install a package called `cross-env`. This is only really required if you're working on Windows.
61+
62+
```bash
63+
npm install cross-env
64+
```
65+
66+
Once you've done that, open your `package.json` file and add the following:
67+
68+
```json
69+
"scripts": {
70+
"dev": "npm run development",
71+
"development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
72+
"watch": "npm run development -- --watch",
73+
"watch-poll": "npm run watch -- --watch-poll",
74+
"hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
75+
"prod": "npm run production",
76+
"production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
77+
},
78+
```
79+
80+
After you've done this, run `npm run dev`. This will output a your css file at `/css/style.css`.
81+
82+
To hook this into your WordPress theme, within functions.php, add the following:
83+
84+
```php
85+
function add_tailwind() {
86+
wp_enqueue_style( 'style', get_template_directory_uri() . '/css/style.css' );
87+
}
88+
add_action('wp_enqueue_scripts', 'add_tailwind');
89+
}
90+
```
91+
92+
If you load your theme, you should now see the default styling.
93+
94+
## Build once
95+
```bash
96+
npm run dev
97+
```
98+
99+
## Keep building
100+
```bash
101+
npm run watch
102+
```

Diff for: examples/wordpress/functions.php

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
function add_tailwind() {
4+
wp_enqueue_style( 'tailwind', get_template_directory_uri() . '/css/style.css' );
5+
}
6+
add_action('wp_enqueue_scripts', 'add_tailwind');

Diff for: examples/wordpress/index.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
get_header();
4+
5+
echo '<section class="container mx-auto py-10">';
6+
echo '<h1>Welcome to Tailwind for WordPress</h1>';
7+
echo '</section>';
8+
9+
get_footer();

Diff for: examples/wordpress/package.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "tailwind-for-wordpress",
3+
"version": "0.1.0",
4+
"private": true,
5+
"scripts": {
6+
"dev": "npm run development",
7+
"development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
8+
"watch": "npm run development -- --watch",
9+
"watch-poll": "npm run watch -- --watch-poll",
10+
"hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
11+
"prod": "npm run production",
12+
"production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
13+
},
14+
"dependencies": {
15+
"cross-env": "^5.2.0",
16+
"laravel-mix": "^4.0.16",
17+
"tailwindcss": "^1.0.3"
18+
}
19+
}

Diff for: examples/wordpress/resources/css/style.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@tailwind base;
2+
@tailwind components;
3+
@tailwind utilities;

Diff for: examples/wordpress/style.css

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
Theme Name: Tailwind for WordPress
3+
Description: A WordPress theme built to work with TailwindCSS
4+
Version: 1.0
5+
License: GNU General Public License v2 or later
6+
License URI: http://www.gnu.org/licenses/gpl-2.0.html
7+
*/

Diff for: examples/wordpress/tailwind.config.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
theme: {
3+
extend: {}
4+
},
5+
variants: {},
6+
plugins: []
7+
}

Diff for: examples/wordpress/webpack.mix.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const mix = require('laravel-mix');
2+
const tailwindcss = require('tailwindcss');
3+
4+
mix.postCss('./resources/css/style.css', './css/style.css',
5+
tailwindcss('./tailwind.config.js')
6+
);

0 commit comments

Comments
 (0)