Skip to content

Sanitize theme slugs for usage in PHP function names#381

Merged
swissspidy merged 2 commits intomainfrom
copilot/fix-invalid-character-scaffold
Mar 31, 2026
Merged

Sanitize theme slugs for usage in PHP function names#381
swissspidy merged 2 commits intomainfrom
copilot/fix-invalid-character-scaffold

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 31, 2026

Child theme slugs containing dots (e.g. twentytwentyfive-child-2.0.2) produced invalid PHP function names in the generated functions.php, since only spaces and hyphens were being replaced with underscores.

Changes

  • src/Scaffold_Command.php: Replace str_replace( [ ' ', '-' ], '_', ... ) with preg_replace( '/[^a-zA-Z0-9_]/', '_', ... ) so that all non-identifier characters are sanitized, not just spaces and hyphens.
  • features/scaffold.feature: Add a scenario asserting that a slug like my-theme-2.0.1 produces my_theme_2_0_1_parent_theme_enqueue_styles in both the function declaration and the add_action call.

Before:

// wp scaffold child-theme twentytwentyfive-child-2.0.2 --parent_theme=twentytwentyfive
add_action( 'wp_enqueue_scripts', 'twentytwentyfive_child_2.0.2_parent_theme_enqueue_styles' );
function twentytwentyfive_child_2.0.2_parent_theme_enqueue_styles() { ... }

After:

add_action( 'wp_enqueue_scripts', 'twentytwentyfive_child_2_0_2_parent_theme_enqueue_styles' );
function twentytwentyfive_child_2_0_2_parent_theme_enqueue_styles() { ... }

Copilot AI linked an issue Mar 31, 2026 that may be closed by this pull request
2 tasks
@github-actions github-actions bot added bug command:scaffold-child-theme Related to 'scaffold child-theme' command scope:testing Related to testing state:unconfirmed labels Mar 31, 2026
… special characters

Agent-Logs-Url: https://github.com/wp-cli/scaffold-command/sessions/d8e5a698-2f32-4933-9dd4-dc42975c1d9f

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix bad scaffold for child theme with invalid characters Fix invalid PHP function names in child theme scaffold when slug contains dots or special characters Mar 31, 2026
Copilot AI requested a review from swissspidy March 31, 2026 09:13
@swissspidy swissspidy added this to the 2.5.5 milestone Mar 31, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 31, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@swissspidy swissspidy marked this pull request as ready for review March 31, 2026 10:27
@swissspidy swissspidy requested a review from a team as a code owner March 31, 2026 10:27
Copilot AI review requested due to automatic review settings March 31, 2026 10:27
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes child-theme scaffolding when the provided theme slug contains characters that are not valid in PHP function identifiers (e.g., dots in version-like slugs), ensuring the generated functions.php contains valid add_action() callbacks and function declarations.

Changes:

  • Sanitize the child theme slug more broadly when generating the prefix_safe used in functions.php function names.
  • Add an acceptance test scenario covering a dotted slug (e.g. my-theme-2.0.1) and asserting the generated callback/function name uses underscores.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/Scaffold_Command.php Updates how prefix_safe is derived from the slug so generated PHP identifiers don’t contain invalid characters like .
features/scaffold.feature Adds coverage to prevent regressions for dotted slugs in child-theme scaffolding

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

$data = wp_parse_args( $assoc_args, $defaults );
$data['slug'] = $theme_slug;
$data['prefix_safe'] = str_replace( [ ' ', '-' ], '_', $theme_slug );
$data['prefix_safe'] = preg_replace( '/[^a-zA-Z0-9_]/', '_', $theme_slug );
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

preg_replace( '/[^a-zA-Z0-9_]/', '_', $theme_slug ) still allows prefix_safe to start with a digit (e.g. slug 2.0-theme -> 2_0_theme), which would generate an invalid PHP function name and a parse error in the scaffolded functions.php. Consider additionally normalizing prefix_safe to ensure the first character is [A-Za-z_] (e.g. prefixing with _ or theme_ when it isn’t) and handling the edge case where the sanitized result becomes empty.

Suggested change
$data['prefix_safe'] = preg_replace( '/[^a-zA-Z0-9_]/', '_', $theme_slug );
$prefix_safe = preg_replace( '/[^a-zA-Z0-9_]/', '_', $theme_slug );
if ( '' === $prefix_safe ) {
$prefix_safe = 'theme';
}
if ( ! preg_match( '/^[A-Za-z_]/', $prefix_safe ) ) {
$prefix_safe = '_' . $prefix_safe;
}
$data['prefix_safe'] = $prefix_safe;

Copilot uses AI. Check for mistakes.
@swissspidy swissspidy changed the title Fix invalid PHP function names in child theme scaffold when slug contains dots or special characters Sanitize theme slugs for usage in PHP function names Mar 31, 2026
@swissspidy swissspidy merged commit a0d8996 into main Mar 31, 2026
64 of 66 checks passed
@swissspidy swissspidy deleted the copilot/fix-invalid-character-scaffold branch March 31, 2026 10:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug command:scaffold-child-theme Related to 'scaffold child-theme' command scope:testing Related to testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bad scaffold created when child theme has invalid characters

3 participants