Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat[core]: (#739) allow plugins to filter out pages from public url redirect #740

Closed
wants to merge 1 commit into from
Closed

Conversation

ghost
Copy link

@ghost ghost commented Jan 21, 2022

Offer the option to exclude specific pages from redirect should a developer have a need to do this.

Description

The WordPress plugin offers the option to redirect all or none of the pages to the headless site URI. There may be cases where a developer would want some exceptions to this all or nothing approach. For instance if someone is using WooCommerce and wants to use the WooCommerce checkout page without having to rewrite all the logic in the headless site.

Related Issue(s):

Testing

I added the following filter to a custom plugin I built after modifying callbacks.php to include this filter.

add_filter( 'faustwp_exclude_from_public_redirect', function( $excluded ) {
  $excluded = array_merge( $excluded, [
    'checkout',
    'cart',
    'shop',
  ]);
  return $excluded;
}, 10, 1 );

This successfully prevented redirecting from https://mywordpresssite.com/shop/ to http://localhost:3000/shop. I tested other URLs such as https://mywordpresssite.com/alligator/ and that DID redirect correctly.

Offer the option to exclude specific pages from redirect should a developer have a need to do this.
@ghost ghost self-requested a review January 21, 2022 03:50
@piotrmocko
Copy link
Member

cc @piotrmocko for visibility

@theodesp
Copy link
Member

Thank you @cfg-fadica. I think it's a really useful feature. We will review the proposal with the dev team and get back to you.

@ghost
Copy link
Author

ghost commented Apr 6, 2022

While I had the code submitted previously in use on my site, I moved away from it, and now use a built in filter to accomplish this.

<?php
/**
 * Exclude some pages from being redirected by Faust
 * @since 3.0.0
 */

 namespace CFG\Faust;

 use CFG\Interfaces\Hookable;

 class ExcludeRedirectPages implements Hookable {

    public function register_hooks() {
        add_filter( 'faustwp_get_setting', [ $this, 'exclude_pages' ], 10, 3 );
    }


    /**
     * Exclude a page from being redirected
     * @param mixed  $value   The setting value.
     * @param string $name    The setting name.
     * @param mixed  $default Optional setting value.
     */
    public function exclude_pages( $value, $name, $default ) {
        if( $name == 'enable_redirects' ) {
            
            /**
             * Disable redirects for certain WooCommerce pages.
             * 
             * @see /plugins/woocommerce/includes/wc-conditional-functions.php
             * @since 3.0.0
             */
            if( is_wc_endpoint_url() || is_checkout() || is_account_page() || is_privacy_policy() ) {
                return false;
            }

        }

        return $value;
    }

 }

The downside to this is that the $_SERVER variable is not available using this method.

@josephfusco
Copy link
Member

josephfusco commented Mar 11, 2023

Thank you for the feedback and proposed changes!

Feel free to post your WordPress.org username (if you have one) and we'll be happy to get you added as a contributor on the plugin page.

Since this account is now closed, this proposal can be tracked in #1320.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants