diff --git a/404.php b/404.php index a15615c..8a24ce4 100644 --- a/404.php +++ b/404.php @@ -4,17 +4,17 @@ ?> -
-
+
+
-
-

- -

-
+
+

+ +

+
-
-
+
+
Olivier Guilleux'; + echo 'Thème crée par Olivier Guilleux'; } add_filter('admin_footer_text', 'remove_footer_admin'); // Move Yoast to bottom function yoasttobottom() { - return 'low'; + return 'low'; } add_filter( 'wpseo_metabox_prio', 'yoasttobottom'); @@ -75,27 +75,28 @@ function yoasttobottom() { remove_action('wp_print_styles', 'print_emoji_styles'); remove_action( 'admin_print_scripts', 'print_emoji_detection_script' ); remove_action( 'admin_print_styles', 'print_emoji_styles' ); +remove_action('wp_head', 'wp_generator'); // delete wp-embed.js from footer function my_deregister_scripts() { - wp_deregister_script( 'wp-embed' ); + wp_deregister_script( 'wp-embed' ); } add_action( 'wp_footer', 'my_deregister_scripts' ); // delete jquery migrate function dequeue_jquery_migrate( &$scripts){ - if(!is_admin()){ - $scripts->remove( 'jquery'); - $scripts->add('jquery', 'https://code.jquery.com/jquery-3.6.1.min.js', null, null, true ); - } + if(!is_admin()){ + $scripts->remove( 'jquery'); + $scripts->add('jquery', 'https://code.jquery.com/jquery-3.6.1.min.js', null, null, true ); + } } add_filter( 'wp_default_scripts', 'dequeue_jquery_migrate' ); // add SVG to allowed file uploads function add_file_types_to_uploads($mime_types) { - $mime_types['svg'] = 'image/svg+xml'; + $mime_types['svg'] = 'image/svg+xml'; - return $mime_types; + return $mime_types; } add_action('upload_mimes', 'add_file_types_to_uploads', 1, 1); diff --git a/configure/js-css.php b/configure/js-css.php index 22940b9..2ee9f06 100644 --- a/configure/js-css.php +++ b/configure/js-css.php @@ -1,19 +1,87 @@ 'main.js' + ]; + + // add your custom scss files here + $scss_files = [ + 'main' => 'main.scss' + ]; + + if ( VITE_BUILD ) { + $manifest = json_decode( file_get_contents( DIST_PATH . '/.vite/manifest.json' ), true ); + } + + foreach ( $js_files as $handle => $file ) { + $js_uri = VITE_SERVER . '/assets/src/js/' . $file; + if ( VITE_BUILD ) { + $js_uri = DIST_URI . '/' . $manifest[ 'assets/src/js/' . $file ]['file']; + } + + wp_register_script( $handle, $js_uri, null, null, true ); + $vars = array( +// 'ajaxUrl' => admin_url( 'admin-ajax.php' ), // uncomment to use - in your js : siteVars.ajaxUrl + ); + wp_localize_script( $handle, 'siteVars', $vars ); + wp_enqueue_script( $handle ); + } + + foreach ( $scss_files as $handle => $file ) { + $css_uri = VITE_SERVER . '/assets/src/scss/' . $file; + if ( VITE_BUILD ) { + $css_uri = DIST_URI . '/' . $manifest[ 'assets/src/scss/' . $file ]['file']; + } + + wp_enqueue_style( $handle, $css_uri, null, null ); + } +} + +add_action( 'wp_enqueue_scripts', 'add_vite_assets', 100 ); + +function vite_client_head_hook() { + if ( ! VITE_BUILD ) { + echo ''; + } +} + +add_action( 'wp_head', 'vite_client_head_hook' ); + +function add_module_type_attribute( $tag, $handle, $src ) { + // The handles of the enqueued scripts we want to modify + if ( 'main' === $handle && ! VITE_BUILD ) { + return ''; + } + + return $tag; +} + +add_filter( 'script_loader_tag', 'add_module_type_attribute', 10, 3 ); +add_filter( 'style_loader_tag', 'add_module_type_attribute', 10, 3 ); + +function cleaning_wordpress() { // force all scripts to load in footer remove_action('wp_head', 'wp_print_scripts'); remove_action('wp_head', 'wp_print_head_scripts', 9); remove_action('wp_head', 'wp_enqueue_scripts', 1); + + // removing all WP css files enqueued by default + wp_dequeue_style('wp-block-library'); + wp_dequeue_style('wp-block-library-theme'); + wp_dequeue_style('wc-block-style'); + wp_dequeue_style('global-styles'); + wp_dequeue_style('classic-theme-styles'); } -add_action('wp_enqueue_scripts', '_add_javascript', 100); - -function _add_stylesheets() { - // removing all WP css files enqueued by default - wp_dequeue_style('wp-block-library'); - wp_dequeue_style('wp-block-library-theme'); - wp_dequeue_style('wc-block-style'); - wp_dequeue_style('global-styles'); - wp_dequeue_style('classic-theme-styles'); -} -add_action('wp_enqueue_scripts', '_add_stylesheets'); +add_action('wp_enqueue_scripts', 'cleaning_wordpress', 100); diff --git a/configure/utilities.php b/configure/utilities.php index 06e7681..f2272a7 100644 --- a/configure/utilities.php +++ b/configure/utilities.php @@ -1,3 +1,5 @@ admin_url('admin-ajax.php'), - ); - wp_localize_script('main', 'siteVars', $vars); - wp_enqueue_script('main'); -} -add_action('wp_enqueue_scripts', 'add_vite_assets', 100); - -function vite_head_module_hook() { - if(!VITE__BUILD) { - echo ''; - echo ''; - } -} -add_action( 'wp_head', 'vite_head_module_hook' ); - -function add_module_type_attribute($tag, $handle, $src) { - // The handles of the enqueued scripts we want to modify - if ('main' === $handle && !VITE__BUILD) { - return ''; - } - return $tag; -} -add_filter('script_loader_tag', 'add_module_type_attribute', 10, 3); diff --git a/footer.php b/footer.php index 48515b8..90a7494 100644 --- a/footer.php +++ b/footer.php @@ -1,9 +1,9 @@ - + - + diff --git a/functions.php b/functions.php index d66d044..e19a547 100644 --- a/functions.php +++ b/functions.php @@ -1,9 +1,5 @@ > - - - - + + + + >
- + + -
+
diff --git a/index.php b/index.php index 36d2864..798b832 100644 --- a/index.php +++ b/index.php @@ -2,41 +2,27 @@ get_header(); ?> -
-
+
+
- + -
- ', ''); ?> +
+ ', ''); ?> - -
+ +
-
-

Hello!

-

Finally, it works!

-

Try to type something below and save it in editor without refresh the browser!

+ - -
-

You can set background image too!

-
-
-
-
-
- - - -
-
+
+
=3.0.0 <4.0.0", @@ -1305,9 +1305,9 @@ } }, "node_modules/vite": { - "version": "5.0.10", - "resolved": "https://registry.npmjs.org/vite/-/vite-5.0.10.tgz", - "integrity": "sha512-2P8J7WWgmc355HUMlFrwofacvr98DAjoE52BfdbwQtyLH06XKwaL/FMnmKM2crF0iX4MpmMKoDlNCB1ok7zHCw==", + "version": "5.0.11", + "resolved": "https://registry.npmjs.org/vite/-/vite-5.0.11.tgz", + "integrity": "sha512-XBMnDjZcNAw/G1gEiskiM1v6yzM4GE5aMGvhWTlHAYYhxb7S3/V1s3m2LDHa8Vh6yIWYYB0iJwsEaS523c4oYA==", "dev": true, "dependencies": { "esbuild": "^0.19.3", diff --git a/package.json b/package.json index 3813a3d..ae814c7 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { - "name": "wordpress-vite-theme", + "name": "vite-wordpress-theme", "private": true, - "version": "0.0.0", + "version": "1.0.0", "type": "module", "scripts": { "dev": "rimraf ./assets/dist && vite", @@ -10,8 +10,8 @@ }, "devDependencies": { "rimraf": "5.0.5", - "sass": "1.69.5", - "vite": "5.0.10" + "sass": "1.69.7", + "vite": "5.0.11" }, "dependencies": { "bootstrap": "5.3.2" diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..8a4f405 --- /dev/null +++ b/readme.md @@ -0,0 +1,39 @@ +# WordPress Vite.js starter theme + +![](screenshot.png) + +## Under the hood + +- [ES6](https://github.com/lukehoban/es6features#readme) for JavaScript +- [SASS](http://sass-lang.com/) preprocessor for CSS following [SASS Guidelines](https://sass-guidelin.es/#the-7-1-pattern) +- [Bootstrap 5](https://getbootstrap.com/docs/5.2/getting-started/introduction/) as CSS framework ([customizable with SASS](https://getbootstrap.com/docs/5.2/customize/sass/)) +- [Vite.js](https://vitejs.dev/) to compile theme assets and provide live reload + +## Requirements + +* [Node](https://nodejs.org/) + +## Usage + +First, clone this repository in your WordPress themes directory. + +Then, run the following commands in the theme's directory : + + npm install + +Launch your watch for assets with : + + npm run dev + +For production sites, create your build with : + + npm run build + +## Vite & WordPress +- All the static assets used in scss or js files (images, fonts, etc.) are copied as-is to the `dist` folder. The other images used directly in php files are not copied. +- To detect dev mode in php there is no `dist` folder + +## Troubleshooting [dev mode] + +- Vite needs to know the root path of your project so you cannot use a subdirectory as the root of your WordPress installation. +- If you haven't started the dev server, your assets will not be compiled just `npm run dev` and refresh page. diff --git a/screenshot.png b/screenshot.png index 5f112d0..dc4222c 100644 Binary files a/screenshot.png and b/screenshot.png differ diff --git a/static/img/mono-log-unsplash.jpg b/static/img/mono-log-unsplash.jpg deleted file mode 100644 index e9ef86c..0000000 Binary files a/static/img/mono-log-unsplash.jpg and /dev/null differ diff --git a/static/img/tom-delanoue-unsplash.jpg b/static/img/tom-delanoue-unsplash.jpg deleted file mode 100644 index 06d30ea..0000000 Binary files a/static/img/tom-delanoue-unsplash.jpg and /dev/null differ diff --git a/static/svg/.gitkeep b/static/svg/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/style.css b/style.css index 203fe53..58fa32c 100644 --- a/style.css +++ b/style.css @@ -1,9 +1,9 @@ /* -Theme Name: Vite WordPress Starter theme -Theme URI: http://www.wp-default.com/ +Theme Name: Vite WordPress starter theme +Theme URI: https://github.com/oguilleux/vite-wordpress-starter-theme Author: Olivier Guilleux Author URI: https://www.olivier-guilleux.com -Description: Theme for www.wp-default.com +Description: Theme for an awesome team ! Version: 1.0.0 Text Domain: textdomaintomodify */ diff --git a/vite.config.js b/vite.config.js index 0c969e1..4754011 100644 --- a/vite.config.js +++ b/vite.config.js @@ -10,7 +10,14 @@ import { defineConfig } from "vite"; import { resolve } from 'path'; import { glob } from 'glob'; +// Get the relative path of the vite.config.js file for the alias +const fullPath = import.meta.url.slice(0, import.meta.url.lastIndexOf('/')); +const getWpContentIndex = fullPath.indexOf('wp-content'); +const wpContentPath = fullPath.slice(getWpContentIndex); + export default defineConfig({ + base: './', + plugins: [ { handleHotUpdate({ file, server }) { @@ -23,11 +30,6 @@ export default defineConfig({ css: { devSourcemap: true, - // preprocessorOptions: { - // scss: { - // additionalData: '@import "./src/scss/sass_imports.scss";', - // } - // } }, build: { @@ -36,6 +38,9 @@ export default defineConfig({ outDir: resolve(__dirname, 'assets/dist/'), + // don't base64 images + assetsInlineLimit: 0, + rollupOptions: { input: { 'js/main': resolve(__dirname + '/assets/src/js/main.js'), @@ -53,14 +58,19 @@ export default defineConfig({ chunkFileNames: '[name]-[hash].js', assetFileNames: (assetInfo) => { let extType = assetInfo.name.split('.'); - let extSuffix = extType[extType.length - 1]; - let name = assetInfo.name.split('/').pop(); - // stip extension - name = name.replace(/\.[^/.]+$/, ""); - console.log(extSuffix); - return `${extSuffix}/${name}-[hash][extname]`; - }, + // group fonts in a folder + if (extType[1] === 'woff' || extType[1] === 'woff2' || extType[1] === 'ttf') { + return 'fonts/[name]-[hash].[ext]'; + } + + // group images in a folder + if (extType[1] === 'gif' || extType[1] === 'jpg' || extType[1] === 'jpeg' || extType[1] === 'png') { + return 'img/[name]-[hash].[ext]'; + } + + return'[ext]/[name]-[hash].[ext]'; + } }, }, }, @@ -72,14 +82,13 @@ export default defineConfig({ }, // We need a strict port to match on PHP side. - // You can change it. But, please update it on your .env file to match the same port strictPort: true, - port: 5173 + port: 5173, }, resolve: { alias: { - "@": resolve(__dirname, '../static/'), + '@': process.env.NODE_ENV === 'development' ? resolve(wpContentPath + '/static') : '/static' } }, });