Skip to content

Define DB_NAME via auto-prepend instead of rewriting wp-config.php#3458

Merged
JanJakes merged 2 commits intotrunkfrom
db-name
Apr 8, 2026
Merged

Define DB_NAME via auto-prepend instead of rewriting wp-config.php#3458
JanJakes merged 2 commits intotrunkfrom
db-name

Conversation

@JanJakes
Copy link
Copy Markdown
Member

@JanJakes JanJakes commented Apr 2, 2026

Summary

  • Stop modifying wp-config.php to insert define('DB_NAME', 'wordpress'). Instead, use static token-based parsing to check if the constant is already defined, and define it via the PHP auto-prepend script if missing.
  • The user's wp-config.php value always takes priority — if they define DB_NAME, Playground respects it.
  • Extract the logic into a reusable defineWpConfigConstantFallbacks() function.

Test plan

  • Existing tests updated and passing (79 tests in wp-config.spec.ts)
  • Verify fresh Playground boot works (DB_NAME defaults to database-name-here from wp-config-sample.php)
  • Verify mounting a project with custom DB_NAME in wp-config.php uses the custom value
  • Verify importing a WordPress backup without DB_NAME in wp-config.php still works (DB_NAME is wordpress)

Closes #2530.

🤖 Generated with Claude Code

Instead of modifying wp-config.php to insert a define() call for
DB_NAME, use static token-based parsing to check whether the constant
is already defined, and if not, define it via the PHP auto-prepend
script. This ensures the user's wp-config.php value always takes
priority and the file is never modified for this purpose.

Extracts the logic into a reusable defineWpConfigConstantFallbacks()
function that can handle any number of constants.
@JanJakes JanJakes marked this pull request as ready for review April 3, 2026 08:18
@JanJakes JanJakes requested review from a team, adamziel and Copilot April 3, 2026 08:18
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 changes how Playground ensures required WordPress constants (notably DB_NAME) are available by avoiding direct rewrites to wp-config.php and instead defining missing constants via PHP auto-prepend.

Changes:

  • Stop rewriting wp-config.php to inject DB_NAME; detect missing constants using WP_Config_Transformer and define fallbacks via php.defineConstant.
  • Add defineWpConfigConstantFallbacks() to encapsulate the “check + define via auto-prepend” flow.
  • Update tests to assert auto-prepend constants behavior rather than asserting rewritten wp-config.php content.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
packages/playground/wordpress/src/wp-config.ts Switches from rewriting wp-config.php to defining missing constants via auto-prepend after token-based detection.
packages/playground/wordpress/src/test/wp-config.spec.ts Updates tests to validate DB_NAME fallback behavior without relying on rewritten config output.
packages/playground/wordpress/src/boot.ts Adjusts comment to reflect the new behavior (no wp-config.php rewrite).
packages/playground/blueprints/src/lib/steps/import-wordpress-files.ts Adjusts comment to reflect the new behavior (no wp-config.php rewrite).

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

Comment on lines +25 to +34
it('should define DB_NAME via defineConstant when wp-config.php does not define it', async () => {
php.writeFile(wpConfigPath, `<?php`);
await ensureWpConfig(php, documentRoot);

// wp-config.php should not be modified.
expect(php.readFileAsText(wpConfigPath)).toBe(`<?php`);

// DB_NAME should be defined via the auto-prepend mechanism.
expect(getDefinedConstants(php)).toHaveProperty('DB_NAME', 'wordpress');
});
Copy link

Copilot AI Apr 3, 2026

Choose a reason for hiding this comment

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

This assertion verifies that defineConstant was invoked (via consts.json), but it doesn’t prove DB_NAME is actually available to WordPress execution (i.e., visible at runtime when WordPress loads). Add an assertion that runs PHP (via php.run) after ensureWpConfig and checks defined('DB_NAME') and DB_NAME value, to cover the end-to-end behavior this PR relies on.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator

@adamziel adamziel left a comment

Choose a reason for hiding this comment

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

LGTM! I'd just address the last unresolved node from copilot

Verify that DB_NAME is not only written to consts.json but is
actually available at PHP runtime via the auto-prepend script.
@JanJakes JanJakes merged commit 32d4af0 into trunk Apr 8, 2026
47 checks passed
@JanJakes JanJakes deleted the db-name branch April 8, 2026 11:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid wp-config.php conflicts with pre-defined DB_NAME constant

3 participants