0

How do I find the page name or page slug inside a filter in a plugin?

I am customizing a plug-in and I need to run some code based on which page is currently being displayed.

The code is inside a function (let's call it FunctionA() ), which is called by a filter:

add_filter( 'woocommerce_registration_errors', 'FunctionA', 10 );

I've tried all the options described here and a few more I found googling, but everything seems to return NULL or blank (instead of the page slug, page name, $post or whatever other variable is suggested).

Here is a bit more of the code:

function FunctionA( $errors ) {

    $fields = FunctionB_get_account_fields();

    foreach ( $fields as $key => $field_args ) {

        // if none of the fields are required, 
        // don't produce any errors and check next field
        if ( empty( $field_args['required'] ) ) {

            continue; 

        }


        if ( isset( $_POST['register'] ) ) {

            //Here I want to do something like:

            // if (current_page_slug = 'vendor_reg_page') {

            //      if( ! empty( $field_args['hide_in_vendor_registration'] ) ) {
            //          continue;
            //       }

            // }else if(current_page_slug == 'other_reg_page'){

           //         if( ! empty( $field_args['hide_in_other_registration'] ) ) {
            //          continue;
            //       }
            //}



        }

        if ( empty( $_POST[ $key ] ) ) {

            $message = sprintf( __( '%s is a required field.', '<strong>' . $field_args['label'] . '</strong>' );
            $errors->add( $key, $message );
        }
    }

    return $errors;
}

add_filter( 'woocommerce_registration_errors', 'FunctionA', 10 );

What do I need to do to get the page name, slug or some other thing that identifies from where the form is being posted?

I am able to get the page slug without any problems inside an action in this same plugin, but doing this in a filter does not work.

I'm fairly new to wordpress development so I don't know: is there any way to get the page info in a filter? or is this impossible?


UPDATE: Answer

By dumping $_POST (using var_dump($_POST)) I found out that the array item _wp_http_referer contains the page's relative URL (which includes the slug). For example, in my case it contained "/register/vendor_reg_page/", where vendor_reg_page was the slug of the page I was running my tests on. I used that to check for the current page.

This is the final code that worked for me:

$post_relative_url = $_POST['_wp_http_referer'];

$post_slug = substr(strrchr(trim($post_relative_url, "/"), "/"), 1);
//removes leading and trailing "/" 
//gets the last substring after "/"
//and removes the "/" char from that substr

Plugging that back on the full code, it looked something like this:

function FunctionA( $errors ) {

    $fields = FunctionB_get_account_fields();

    foreach ( $fields as $key => $field_args ) {

        // if none of the fields are required, 
        // don't produce any errors and check next field
        if ( empty( $field_args['required'] ) ) {

            continue; 

        }


        if ( isset( $_POST['register'] ) ) {

            // ============ Here is where I added the solution: ======

            $post_relative_url = $_POST['_wp_http_referer'];

            if ( isset($post_relative_url) ){

              $post_slug = substr(strrchr(trim($post_relative_url, "/"), "/"), 1);

              if ($post_slug == 'vendor_reg_page') {

                 if( ! empty( $field_args['hide_in_vendor_registration'] ) ) {
                        continue;
                   }

              }else if(...)
                ...
              }

            }   

        }

        if ( empty( $_POST[ $key ] ) ) {

            $message = sprintf( __( '%s is a required field.', '<strong>' . $field_args['label'] . '</strong>' );
            $errors->add( $key, $message );
        }
    }

    return $errors;
}

add_filter( 'woocommerce_registration_errors', 'FunctionA', 10 );

It seems like the reason why the other ways (described in my question) didn't work isn't so much because of the function being a filter, but more because we are reading data that was sent via a POST request, instead of one that is being displayed on a page (anyone: feel free to correct me if I'm wrong).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.