0

i try to add a class to the body for a specific page/url, which has a parameter at the end. the url looks like this: https://sample-site.com/user/37/?um_action=edit

with my code, the class is also added to the url without parameter. i know why this is happening, but i dont have any other idea to get this to work.

function leweb_add_body_class_um_edit_profile( $classes ) {

    global $wp;
    $current_url = home_url( add_query_arg( array(), $wp->request ) ) . '/?um_action=edit';
    $profile_url = um_user_profile_url() . '?um_action=edit';

    if ( $profile_url == $current_url ) {

        $classes[] = 'leweb-um-profile-edit';

    }

    return $classes;

}
add_filter( 'body_class','leweb_add_body_class_um_edit_profile' );
1

1 Answer 1

1

You're overcomplicating it a bit. ?um_action=edit is a query string, and its values are available in the $_GET superglobal. To check if it exists, and has a specific value, you just need to do this:

function leweb_add_body_class_um_edit_profile( $classes ) {
    if ( isset( $_GET['um_action'] ) && 'edit' === $_GET['um_action'] ) {
        $classes[] = 'leweb-um-profile-edit';
    }

    return $classes;
}
add_filter( 'body_class','leweb_add_body_class_um_edit_profile' );
1
  • perfect, thanks Commented Oct 13, 2019 at 21:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.