Run functions only in the admin area?

I’d like this code to only run inside the admin area as it is resorting the items on the public side admin bar too.

  /* Reorder Admin Menu to put "Pages" at the top */
  function menu_order_filter($menu) {
  $content_menu = array('edit.php?post_type=page');
  array_splice($menu, 2, 0, $content_menu);
  return array_unique($menu);
  }
  add_filter('custom_menu_order', create_function('', 'return true;'));
  add_filter('menu_order', 'menu_order_filter');

4 s
4

There is very little overhead to assigning couple of filters on hooks that simply won’t fire on front end.

In general it would be something like this:

add_action('init', 'admin_only');

function admin_only() {

    if( !is_admin() )
        return;

    // filter assignemnts and such go here
}

Also create_function() is not recommended for performance and some other reasons. It is better to use more modern Anonymous Functions, but for cases like this WordPress provides ready-made __return_true() function.

Leave a Comment