faustjs icon indicating copy to clipboard operation
faustjs copied to clipboard

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

Open cfg-fadica opened this issue 3 years ago • 3 comments

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):

  • #739

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.

cfg-fadica avatar Jan 21 '22 03:01 cfg-fadica

cc @piotrmocko for visibility

piotrmocko avatar Feb 22 '22 16:02 piotrmocko

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.

theodesp avatar Feb 24 '22 10:02 theodesp

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.

ghost avatar Apr 06 '22 20:04 ghost

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 https://github.com/wpengine/faustjs/pull/1320.

josephfusco avatar Mar 11 '23 05:03 josephfusco