Title: add_cssclass
Published: April 25, 2014
Last modified: May 20, 2026

---

# add_cssclass( string $class_to_add, string $classes ): string

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/add_cssclass/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/add_cssclass/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/add_cssclass/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/add_cssclass/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/add_cssclass/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/add_cssclass/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/add_cssclass/?output_format=md#wp--skip-link--target)

Adds a CSS class to a string.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/add_cssclass/?output_format=md#parameters)󠁿

 `$class_to_add`stringrequired

The CSS class to add.

`$classes`stringrequired

The string to add the CSS class to.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/add_cssclass/?output_format=md#return)󠁿

 string The string with the CSS class added.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/add_cssclass/?output_format=md#source)󠁿

    ```php
    function add_cssclass( $class_to_add, $classes ) {
    	if ( empty( $classes ) ) {
    		return $class_to_add;
    	}

    	return $classes . ' ' . $class_to_add;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-admin/includes/menu.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-admin/includes/menu.php#L212)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-admin/includes/menu.php#L212-L218)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/add_cssclass/?output_format=md#related)󠁿

| Used by | Description | 
| [add_menu_classes()](https://developer.wordpress.org/reference/functions/add_menu_classes/)`wp-admin/includes/menu.php` |

Adds CSS classes for top-level administration menu items.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/add_cssclass/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.7.0](https://developer.wordpress.org/reference/since/2.7.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/add_cssclass/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 3 content](https://developer.wordpress.org/reference/functions/add_cssclass/?output_format=md#comment-content-3904)
 2.    [MakeWebBetter](https://profiles.wordpress.org/makewebbetter/)  [  6 years ago  ](https://developer.wordpress.org/reference/functions/add_cssclass/#comment-3904)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_cssclass%2F%23comment-3904)
     Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_cssclass%2F%23comment-3904)
 4.  **Usage when working over menus order**
 5.      ```php
         $firstm = $lastm = false;
         $i = 0;
     
         foreach ( $menu as $order => $top ) {
                 $i++;
     
                 if ( 0 == $order ) {
                     $menu[0][4] = add_cssclass( 'menu-top-first', $top[4] );
                     $lastm = 0;
                     continue;
                 }
     
                 if ( 0 === strpos( $top[2], 'separator' ) && false !== $lastm ) {
                     // if separator
                     $firstm = true;
                     $c = $menu[ $lastm ][4];
                     $menu[ $lastm ][4] = add_cssclass( 'menu-top-last', $c );
                     continue;
                 }
     
                 if ( $first ) {
                     $c = $menu[ $order ][4];
                     $menu[ $order ][4] = add_cssclass( 'menu-top-first', $c );
                     $firstm = false;
                 }
     
                 $lastm = $order;
             }
         ```
     
 6.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_cssclass%2F%3Freplytocom%3D3904%23feedback-editor-3904)
 7.   [Skip to note 4 content](https://developer.wordpress.org/reference/functions/add_cssclass/?output_format=md#comment-content-3906)
 8.    [crstauf](https://profiles.wordpress.org/crstauf/)  [  6 years ago  ](https://developer.wordpress.org/reference/functions/add_cssclass/#comment-3906)
 9.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_cssclass%2F%23comment-3906)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_cssclass%2F%23comment-3906)
 10. While the function name does suggest no limitation on its availability, it is 
     [defined](https://developer.wordpress.org/reference/functions/add_cssclass/#source)
     in `wp-admin/includes/menu.php`, so is not immediately available on the frontend(
     and use on the frontend is probably discouraged).
 11.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_cssclass%2F%3Freplytocom%3D3906%23feedback-editor-3906)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_cssclass%2F)
before being able to contribute a note or feedback.