|
| 1 | +--- |
| 2 | +outline: deep |
| 3 | +--- |
| 4 | + |
| 5 | +# Migrating from Theme Core |
| 6 | + |
| 7 | +## Introduction |
| 8 | +This guide will walk you through the process of migrating from the [theme core repository](https://github.com/creode/wordpress-theme-core) to the new framework. |
| 9 | + |
| 10 | +## Upgrade Steps |
| 11 | + |
| 12 | +### 1: Ensure you have all composer packages updated |
| 13 | +Run the following command to ensure you have all the latest packages installed. |
| 14 | + |
| 15 | +```bash |
| 16 | +composer update |
| 17 | +``` |
| 18 | + |
| 19 | +### 2: Install the Creode WordPress Theme Framework |
| 20 | + |
| 21 | +Follow the [installation guide](/installation) to install the Creode WordPress Theme Framework. |
| 22 | + |
| 23 | +### 3: Remove the theme core from your project |
| 24 | +Using composer, remove the theme core from your project. |
| 25 | + |
| 26 | +```bash |
| 27 | +composer remove creode/theme-core |
| 28 | +``` |
| 29 | + |
| 30 | +### 4: Remove the requirement of the theme-core boot file |
| 31 | +Remove the following line from your `functions.php` file. |
| 32 | + |
| 33 | +```php |
| 34 | +require_once get_template_directory() . '/core/boot.php'; // [!code --] |
| 35 | +``` |
| 36 | + |
| 37 | +### 5: Replace class of register_vite_script function |
| 38 | +The assets class from theme core has been removed and instead is now replaced with a function from the new theme framework. |
| 39 | + |
| 40 | +You will need to replace all occurrences and function calls: |
| 41 | +```php |
| 42 | +use Creode_Theme\Asset_Enqueue; // [!code ++] |
| 43 | + |
| 44 | +function [[THEME_NAME]]_enqueue_script() { |
| 45 | + Assets::register_vite_script( 'header', 'src/components/header.js', array( 'jquery' ), true ); // [!code --] |
| 46 | + |
| 47 | + $asset_enqueue = Asset_Enqueue::get_instance(); // [!code ++] |
| 48 | + $asset_enqueue->register_vite_script( 'header', 'src/components/header.js', array( 'jquery' ), true ); // [!code ++] |
| 49 | + |
| 50 | + // ... other required code ... |
| 51 | +}; |
| 52 | +add_action( 'wp_enqueue_scripts', '[[THEME_NAME]]_enqueue_script' ); |
| 53 | +``` |
| 54 | + |
| 55 | +This will now ensure that all JS is loaded from Vite correctly. |
| 56 | + |
| 57 | +### 6: Remove hot reloading from your vite config file. |
| 58 | +Hot reloading was a feature that was a little buggy on the theme core. It is therefore no longer supported in the new theme framework. There may be plans in future to reintroduce this feature. |
| 59 | + |
| 60 | +In order to remove hot reloading, you will need to remove the following line from your vite config file. |
| 61 | + |
| 62 | +```js |
| 63 | +import { manageHotReloadFile } from './core/js/hot-reload.js'; // [!code --] |
| 64 | + |
| 65 | +export default defineConfig( |
| 66 | + (command) => { |
| 67 | + manageHotReloadFile(command.mode, DDEV_HOSTNAME, HOT_RELOAD_PORT); // [!code --] |
| 68 | + |
| 69 | + // ... other vite config code ... |
| 70 | + } |
| 71 | +); |
| 72 | +``` |
| 73 | + |
| 74 | +### 7: Move your SCSS folder up a level |
| 75 | +In the previous version of the theme core framework, the SCSS folder was located within the `src` folder. As part of this migration, you will need to move the SCSS folder up a level to the root of the project. The `SCSS` folder should now be located in the root of the theme. |
| 76 | + |
| 77 | +For example if you had a `src/scss` folder in the theme, it should now be moved to the root of the theme to `scss/`. |
| 78 | + |
| 79 | +### 8: Migrate the component specific CSS files up to the component directory |
| 80 | +As part of the changes to how blocks are handled, the SCSS files for each block should now be moved to their relevant block folder. |
| 81 | + |
| 82 | +For example a header.scss component file will now be located in the following folder based on the theme root: `/blocks/header-block/assets/header.scss`. |
| 83 | + |
| 84 | +Each of these components should be moved to their relevant block folder. |
| 85 | + |
| 86 | +This is a change that will need to be done manually and can be quite tedious however it will ensure that all themes and blocks are kept consistent in the future. |
| 87 | + |
| 88 | +### 9: Remove individual components imports from the `admin.scss` and `main.scss` files |
| 89 | +As part of the changes to how blocks are handled, the individual components `@use` and `@include` from the `admin.scss` and `main.scss` files are no longer required. These are now handled by the theme framework and will be automatically added to the theme in a new `blocks/_all.scss` file. |
| 90 | + |
| 91 | +This is a change that will need to be done manually and can be quite tedious however it will ensure that all themes and blocks are kept consistent in the future. |
| 92 | + |
| 93 | +You can recompile the assets to check over your changes periodically during this process by running the following WP CLI command: |
| 94 | +```bash |
| 95 | +wp creode-theme:build |
| 96 | +``` |
| 97 | + |
| 98 | +### 10: Clean up scss import paths |
| 99 | +After moving the SCSS files to their relevant block folder, you will need to clean up the `@use` paths for each of the SCSS files. Paths to scss files in vite can be absolute based on where the `vite.config.js` file is located. In this case it will be in the theme. |
| 100 | + |
| 101 | +An example of this is demonstrated below with pulling the global file into the `header.scss` file: |
| 102 | + |
| 103 | +```scss |
| 104 | +@use "../globals"; // [!code --] |
| 105 | +@use "/scss/globals"; // [!code ++] |
| 106 | +``` |
| 107 | + |
| 108 | +This change can be quite tedious to do manually however we want to ensure that all themes keep the same structure and paths so a change like this will help our projects stay consistent in the future. |
| 109 | + |
| 110 | +### 11: Run the script to install any framework files and compile assets |
| 111 | +As part of the theme framework, there is a WordPress CLI command that will install any missing files and compile the assets. This ensures that the structure of the theme is kept consistent and that new files as part of the boilerplate can be added automatically to themes without having to keep track of them manually. |
| 112 | + |
| 113 | +### 12: Remove the admin and main js files from src |
| 114 | +As part of the framework, the main.js and admin.js within the `src` folder are no longer required. These are now handled by the theme framework and will be automatically added to the theme in a new `vite-entry-points` folder. These files are no longer required and can be removed from the `src` folder, if there is more content to them, ensure this is now merged with the newly created equivalent files in the `vite-entry-points` folder. |
| 115 | + |
| 116 | +### 13: Update the vite config file to switch these entrypoints over |
| 117 | +The `vite.config.js` file will need to be updated to switch these entrypoints over to the new `vite-entry-points` folder. |
| 118 | + |
| 119 | +```js |
| 120 | +export default defineConfig( |
| 121 | + (command) => { |
| 122 | + return { |
| 123 | + // ... other vite config code ... |
| 124 | + build: { |
| 125 | + rollupOptions: { |
| 126 | + // overwrite default .html entry |
| 127 | + input: { |
| 128 | + main: resolve(__dirname, 'src/main.js'), // [!code --] |
| 129 | + admin: resolve(__dirname, 'src/admin.js'), // [!code --] |
| 130 | + main: resolve(__dirname, 'vite-entry-points/main.js'), // [!code ++] |
| 131 | + admin: resolve(__dirname, 'vite-entry-points/admin.js'), // [!code ++] |
| 132 | + // ... other vite config code ... |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | +); |
| 139 | +``` |
| 140 | + |
| 141 | + |
| 142 | +### 14: Import the new `blocks/_all.scss` file into the main.scss file |
| 143 | +Add the following to the top of the `main.scss` file: |
| 144 | + |
| 145 | +```scss |
| 146 | +// Use base elements |
| 147 | +@use 'base'; |
| 148 | + |
| 149 | +// Use blocks // [!code ++] |
| 150 | +@use '/blocks/all' as blocks; // [!code ++] |
| 151 | + |
| 152 | +// ...other scss code... |
| 153 | + |
| 154 | +// Render base elements |
| 155 | +@include base.render; |
| 156 | + |
| 157 | +// Render blocks // [!code ++] |
| 158 | +@include blocks.render; // [!code ++] |
| 159 | +``` |
| 160 | + |
| 161 | +This will ensure that all blocks are loaded into the theme. |
| 162 | + |
| 163 | +### 15: Import the new `blocks/_all.scss` file into the admin.scss file |
| 164 | +This process is very similar to the `main.scss` file however you will need to ensure that any admin specific mixins are handled manually using the new folder location. The new blocks all file only handle the render mixin and admin ones will need to be handled manually. See the below example for how this has changed: |
| 165 | + |
| 166 | +```scss |
| 167 | +@use 'components/header'; // [!code --] |
| 168 | +@use '/blocks/header-block/assets/_header' as header; // [!code ++] |
| 169 | + |
| 170 | +// ...other scss code... |
| 171 | + |
| 172 | +@include header.render; // [!code --] |
| 173 | +@include header.admin; // [!code ++] |
| 174 | +``` |
| 175 | + |
| 176 | +Add the following to the `admin.scss` file: |
| 177 | + |
| 178 | +```scss |
| 179 | +// Use base elements |
| 180 | +@use 'base'; |
| 181 | + |
| 182 | +// Use blocks |
| 183 | +@use '/blocks/all' as blocks; // [!code ++] |
| 184 | + |
| 185 | +// ...other scss code... |
| 186 | + |
| 187 | +// Render base elements |
| 188 | +@include base.render; |
| 189 | + |
| 190 | +// Render blocks // [!code ++] |
| 191 | +@include blocks.render; // [!code ++] |
| 192 | +``` |
| 193 | + |
| 194 | +### 16: Recompile the assets |
| 195 | +Once these steps have been completed, you will need to recompile the assets. This can be done by running the following WP CLI command: |
| 196 | + |
| 197 | +```bash |
| 198 | +wp creode-theme:build |
| 199 | +``` |
0 commit comments