Created
January 19, 2017 09:21
-
-
Save frankiejarrett/c381a6a1df715ed52746ff9be929ba88 to your computer and use it in GitHub Desktop.
Replace mustache placeholders in Child Theme starter content
This file contains hidden or 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
| <?php | |
| /** | |
| * Filter starter content placeholders for new sites using this Child Theme. | |
| * | |
| * @var array | |
| */ | |
| add_filter( 'mysite_starter_content_placeholders', function () { | |
| return [ | |
| 'city' => 'Chicago', | |
| 'phone' => '(999) 999-9999', | |
| ]; | |
| } ); |
This file contains hidden or 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
| <?php | |
| $content = [ | |
| 'posts' => [ | |
| 'about' => [ | |
| 'post_type' => 'page', | |
| 'post_title' => 'About {{city}}', | |
| 'post_content' => '<p>To learn more, call {{phone}}.</p>', | |
| ], | |
| ], | |
| ]; | |
| $defaults = [ | |
| 'city' => 'New York', | |
| 'phone' => '(555) 555-5555', | |
| ]; | |
| /** | |
| * Starter content for new sites using this theme. | |
| * | |
| * @link https://make.wordpress.org/core/2016/11/30/starter-content-for-themes-in-4-7/ | |
| */ | |
| add_theme_support( 'starter-content', function () use ( $content, $defaults ) { | |
| $content = json_encode( $content ); // Array to JSON string | |
| $placeholders = (array) apply_filters( 'mysite_starter_content_placeholders', [] ); | |
| $placeholders = wp_parse_args( $placeholders, (array) $defaults ); | |
| foreach ( $placeholders as $placeholder => $value ) { | |
| $content = str_replace( sprintf( '{{%s}}', $placeholder ), $value, $content ); | |
| } | |
| return json_decode( $content, true ); // JSON string to assoc array | |
| } ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment