Skip to content

Commit

Permalink
feat: replace example code with generators
Browse files Browse the repository at this point in the history
- 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
dustin-jw committed Oct 18, 2023
1 parent 9562853 commit 287859b
Show file tree
Hide file tree
Showing 23 changed files with 588 additions and 184 deletions.
27 changes: 13 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@

This is a starter to fast-track WordPress websites. It provides a way to skip many of the steps required when setting up a WordPress installation.

| Developer Documentation |
|-------------------------------------------------|
| [Local Development Setup](docs/development.md) |
| [Error Handling](docs/error-handling.md) |
| [Project Structure](docs/structure.md) |
| [Wordpress](docs/wordpress.md) |
| [PHP Helpers](docs/helpers.md) |
| [Shortcodes](docs/shortcodes.md) |
| [Plugins Docs](docs/plugins.md) |
| [CSS Class Reference](docs/css.md) |
| [Page Templates](docs/page-templates.md) |
| [Deployment](docs/deployment.md) |
| [Custom Blocks](docs/custom-blocks.md) |
| [How to Contribute](docs/contributing.md) |
| Developer Documentation |
| ---------------------------------------------- |
| [Local Development Setup](docs/development.md) |
| [Wordpress](docs/wordpress.md) |
| [Project Structure](docs/structure.md) |
| [Generators](docs/generators.md) |
| [Plugins](docs/plugins.md) |
| [Custom Blocks](docs/custom-blocks.md) |
| [CSS Class Reference](docs/css.md) |
| [Deployment](docs/deployment.md) |
| [How to Contribute](docs/contributing.md) |
| [Code of Conduct](docs/code-of-conduct.md) |

## SparkPress Team

- **[Kasey Bonifacio](https://github.com/kaseybon)**
- **[Ricardo Fearing](https://github.com/rfearing)**
- **[Rob Tarr](https://github.com/robtarr)**
Expand Down
4 changes: 0 additions & 4 deletions docs/error-handling.md

This file was deleted.

39 changes: 0 additions & 39 deletions docs/example-content.md

This file was deleted.

67 changes: 67 additions & 0 deletions docs/generators.md
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/)
7 changes: 6 additions & 1 deletion docs/structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ The `php/` directory holds the PHP template files for the WordPress theme. Other
- `inc/` - Includes directory, to keep the `functions.php` file clean. Some important files to note are:
- `shortcodes/*.php` - Add shortcodes for the content editor to use
- `helpers/*.php` - Add helper functions here, separated by purpose
- `theme-scripts.php` - Add CSS and JavaScript files here.
- `setup-queries.php` - Specify query variables here so WordPress can read them from HTTP requests.
- `theme-scripts.php` - Add JavaScript files here.
- `theme-setup.php` - Configure theme settings and supported features here.
- `theme-styles.php` - Add CSS files here.
- `theme-widgets.php` - Register [widgets][widgets] for admins to utilize and add them to Timber context here.
- `views/` - [Twig][twig] is used to separate presentation from logic, and all `.twig` components can be found here. Some Twig extensions, notably [HTML Extension][html-extension] and [String Extension][string-extension] have been added to enhance templates with data URIs, class management, text manipulation, and ASCII-safe string transformations.
- `layouts` - Any twig templates that include the full document structure should go here. That includes the default `base.twig` template and any alternatives, such as for art-directed posts.
- `partials` - Twig templates for components or pieces of the page to be reused should go here.
Expand All @@ -35,3 +39,4 @@ The `php/` directory holds the PHP template files for the WordPress theme. Other
[html-extension]: https://github.com/twigphp/html-extra
[string-extension]: https://github.com/twigphp/string-extra
[underscores]: https://underscores.me/
[widgets]: https://developer.wordpress.org/themes/functionality/sidebars/
53 changes: 53 additions & 0 deletions generators/page-template.js
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();
Loading

0 comments on commit 287859b

Please sign in to comment.