-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: replace example code with generators
- install `prompts` - add generator for page templates - add generator for shortcodes - add generator for custom post types - add generator for custom taxonomies - provide npm scripts for generators - remove example scripts/templates - update documentation for generators - remove redundant width limiter class
- Loading branch information
Showing
23 changed files
with
588 additions
and
184 deletions.
There are no files selected for viewing
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Generators | ||
|
||
We have a handful of generators to make it easier to add new custom post types, taxonomies, shortcodes, and page templates. If none of the generators give you what you need, you can try using https://generatewp.com/ to get more relevant code snippets. | ||
|
||
## Custom Post Type | ||
|
||
The generator for custom post types will prompt you for some details that it will use to create the necessary files for registering the post type. You can specify whether to create scripts and templates for single and archive pages for the post type. If you opt not to, the default scripts/templates will be used based on WordPress' template hierarchy. | ||
|
||
```sh | ||
npm run generate:post-type | ||
``` | ||
|
||
The following files will be created based on your inputs: | ||
|
||
- `src/php/inc/custom-post-types/<post-type-name>.php` | ||
- `src/php/single-<post-type-name>.php` (optional) | ||
- `src/php/archive-<post-type-name>.php` (optional) | ||
- `src/php/views/single-<post-type-name>.twig` (optional) | ||
- `src/php/views/archive-<post-type-name>.twig` (optional) | ||
|
||
[Custom Post Types documentation](https://wordpress.org/support/article/post-types/#custom-post-types) | ||
|
||
## Custom Taxonomy | ||
|
||
The generator for custom taxonomies will prompt you for some details that it will use to create the necessary files for registering the taxonomy. You can specify whether to create a script/template for the taxonomy listing page, and if you choose not to, the default archive script/template will be used. You will also be prompted to specify which post types the taxonomy should be associated with. | ||
|
||
```sh | ||
npm run generate:taxonomy | ||
``` | ||
|
||
The following files will be created based on your inputs: | ||
|
||
- `src/php/inc/taxonomies/<taxonomy-name>.php` | ||
- `src/php/taxonomy-<taxonomy-name>.php` (optional) | ||
- `src/php/views/taxonomy-<taxonomy-name>.twig` (optional) | ||
|
||
[Custom Taxonomies documentation](https://developer.wordpress.org/plugins/taxonomies/working-with-custom-taxonomies/) | ||
|
||
## Shortcode | ||
|
||
The generator for shortcodes will prompt you for a name, then create minimal files to register a shortcode. The bulk of implementation will still be up to you, but the boilerplate should speed things up a bit. | ||
|
||
```sh | ||
npm run generate:shortcode | ||
``` | ||
|
||
The following files will be created based on your input: | ||
|
||
- `src/php/inc/shortcodes/<shortcode-name>.php` | ||
- `src/php/views/shortcodes/<shortcode-name>.twig` | ||
|
||
[Shortcode documentation](https://codex.wordpress.org/Shortcode_API) | ||
|
||
## Custom Page Template | ||
|
||
The generator for custom page templates will prompt you for a name, then create minimal files to register a custom page type. | ||
|
||
```sh | ||
npm run generate:page-template | ||
``` | ||
|
||
The following files will be created based on your input: | ||
|
||
- `src/php/<page-template-name>.php` | ||
- `src/php/views/<page-template-name>.twig` | ||
|
||
[Page Template documentation](https://developer.wordpress.org/themes/template-files-section/page-template-files/) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const { writeFileSync } = require('fs'); | ||
const { join } = require('path'); | ||
const prompts = require('prompts'); | ||
|
||
const getScriptTemplate = (templateName, baseFileName) => `<?php | ||
/** | ||
* Template Name: ${templateName} | ||
*/ | ||
$context = Timber\\Timber::context(); | ||
$timber_post = new Timber\\Post(); | ||
$context['post'] = $timber_post; | ||
// Render HTML templates. | ||
render_with_password_protection( $timber_post, '${baseFileName}.twig', $context ); | ||
`; | ||
|
||
const twigTemplate = `{% extends "layouts/base.twig" %} | ||
{% block content %} | ||
<div class="obj-width-limiter"> | ||
{{ post.content }} | ||
</div> | ||
{% endblock %} | ||
`; | ||
|
||
const getName = async () => { | ||
const response = await prompts({ | ||
type: 'text', | ||
name: 'value', | ||
message: 'What should the page template be called?', | ||
}); | ||
|
||
return response.value; | ||
}; | ||
|
||
const generatePageTemplate = async () => { | ||
const templateName = await getName(); | ||
const baseFileName = templateName.toLowerCase().replace(/\W/g, '-'); | ||
|
||
const scriptTemplate = getScriptTemplate(templateName, baseFileName); | ||
|
||
const scriptPath = join(__dirname, '../src/php', `${baseFileName}.php`); | ||
const twigPath = join(__dirname, '../src/php/views', `${baseFileName}.twig`); | ||
|
||
writeFileSync(scriptPath, scriptTemplate, 'utf-8'); | ||
console.log(`Created ${scriptPath}`); | ||
|
||
writeFileSync(twigPath, twigTemplate, 'utf-8'); | ||
console.log(`Created ${twigPath}`); | ||
}; | ||
|
||
generatePageTemplate(); |
Oops, something went wrong.