Skip to content

Random Redirect: only declare the redirect function when nothing else has - #51037

Merged
haqadn merged 1 commit into
trunkfrom
fix/random-redirect-function-conflict
Aug 4, 2026
Merged

Random Redirect: only declare the redirect function when nothing else has#51037
haqadn merged 1 commit into
trunkfrom
fix/random-redirect-function-conflict

Conversation

@haqadn

@haqadn haqadn commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Proposed changes

  • Only declare jetpack_matt_random_redirect() when nothing else already has, so the Random Redirect module no longer fatals on sites where a theme or plugin ships its own copy of the function.

The module was removed from the plugin in 13.6 (#38310) and restored in 16.1 (#50940). While it was gone, some themes started carrying their own copy. A theme's functions.php runs before after_setup_theme, where Jetpack loads module-extras.php, so the theme declares the function first and the plugin's copy hits Cannot redeclare jetpack_matt_random_redirect(). This was reported against 16.1-beta.2 with the panel theme active.

The declaration has to sit inside the conditional rather than behind an early return — PHP binds unconditional top-level function declarations when the file is compiled, so a runtime check placed before one never gets the chance to run.

Reviewing with whitespace hidden (?w=1) shows the real change: 8 added lines. Everything else is the existing function being indented one level.

Related product discussion/links

p1785868165462669-slack-C01U2KGS2PQ

Does this pull request change what data or activity we track or use?

No.

Testing instructions

The redeclare only happens when something else declares the function first, so you need to simulate that.

  1. On a test site running this branch, confirm Random Redirect still works normally: visit https://<site>/?random on the front end. You should be redirected to a random published post.

  2. Drop this into wp-content/mu-plugins/random-redirect-conflict.php to stand in for a theme that ships its own copy:

    <?php
    function jetpack_matt_random_redirect() {
        wp_die( 'conflicting copy ran' );
    }
    add_action( 'template_redirect', 'jetpack_matt_random_redirect' );
  3. Reload any front-end page. On trunk this fatals with Cannot redeclare jetpack_matt_random_redirect(). On this branch the site loads normally.

  4. Visit https://<site>/?random again — you should see conflicting copy ran, confirming Jetpack stepped aside and the other copy is the one running.

  5. Delete the mu-plugin and confirm /?random goes back to redirecting to a random post.

Note that when another copy wins, the jetpack_random_redirect_enabled filter added in #50940 no longer applies, since that filter lives in Jetpack's implementation. That is the intended trade-off: not fataling matters more than owning the behaviour on those sites.

… has

The module was removed from the plugin in 13.6 (#38310) and restored in 16.1
(#50940). While it was gone, some themes and plugins started shipping their own
copy of jetpack_matt_random_redirect(). A theme's functions.php runs before
after_setup_theme, where Jetpack loads module-extras.php, so the theme declares
the function first and the plugin's copy fatals.

Wrap the declaration and its hook in a function_exists() check. The declaration
has to live inside the conditional rather than behind an early return: PHP binds
unconditional top-level function declarations when the file is compiled, so a
runtime check before one never gets the chance to prevent it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01U3VxzdSXMePRtoxepyNGYZ
@haqadn haqadn added Bug When a feature is broken and / or not performing as intended [Status] Needs Review This PR is ready for review. labels Aug 4, 2026
@haqadn haqadn self-assigned this Aug 4, 2026
@github-actions github-actions Bot added [Feature] Theme Tools [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ labels Aug 4, 2026
@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!


Jetpack plugin:

No scheduled milestone found for this plugin.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.

@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WoA dev site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin (Jetpack), and enable the fix/random-redirect-function-conflict branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack fix/random-redirect-function-conflict

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

@jp-launch-control

Copy link
Copy Markdown

Code Coverage Summary

Coverage changed in 1 file.

File Coverage Δ% Δ Uncovered
projects/plugins/jetpack/modules/theme-tools/random-redirect.php 38/51 (74.51%) -1.49% 1 ❤️‍🩹

Full summary · PHP report · JS report

@chrisbliss18 chrisbliss18 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The changes look good. Approved.

I added unit test coverage for this issue and other potential issues in Random Redirect in #51041. The unit test finds the issue in the current code and your fix satisfies the test. Please give it a review when you have a chance.

@haqadn
haqadn merged commit aab342d into trunk Aug 4, 2026
85 of 87 checks passed
@haqadn
haqadn deleted the fix/random-redirect-function-conflict branch August 4, 2026 21:11
@github-actions github-actions Bot removed the [Status] Needs Review This PR is ready for review. label Aug 4, 2026
haqadn added a commit that referenced this pull request Aug 4, 2026
… has (#51037)

The module was removed from the plugin in 13.6 (#38310) and restored in 16.1
(#50940). While it was gone, some themes and plugins started shipping their own
copy of jetpack_matt_random_redirect(). A theme's functions.php runs before
after_setup_theme, where Jetpack loads module-extras.php, so the theme declares
the function first and the plugin's copy fatals.

Wrap the declaration and its hook in a function_exists() check. The declaration
has to live inside the conditional rather than behind an early return: PHP binds
unconditional top-level function declarations when the file is compiled, so a
runtime check before one never gets the chance to prevent it.


Claude-Session: https://claude.ai/code/session_01U3VxzdSXMePRtoxepyNGYZ

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
@haqadn

haqadn commented Aug 4, 2026

Copy link
Copy Markdown
Contributor Author

Cherry-picked to jetpack/branch-16.1 as 265c553 for 16.1-beta.3.

@haqadn haqadn added this to the 16.1 milestone Aug 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug When a feature is broken and / or not performing as intended [Feature] Theme Tools [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants