Add a class with body_class to a specific url with parameter

i try to add a class to the body for a specific pageurl, which has a parameter at the end.the url looks like this: ?u

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: /?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' );

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/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' );
Share Improve this question asked Oct 11, 2019 at 21:07 LovinQuaQuaLovinQuaQua 733 silver badges18 bronze badges 1
  • i found the answer, its well described here: wordpress.stackexchange/questions/83999/… – LovinQuaQua Commented Oct 11, 2019 at 21:30
Add a comment  | 

1 Answer 1

Reset to default 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' );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745088226a4610533.html

相关推荐

  • Add a class with body_class to a specific url with parameter

    i try to add a class to the body for a specific pageurl, which has a parameter at the end.the url looks like this: ?u

    18小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信