8

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 Answers 4

7

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.

9
  • Sorry i'm more of a copy/paste hacker at the moment. How would i modify my above code to use __return_true()? Thanks. Commented Dec 21, 2010 at 13:32
  • @firefusion simply replace create_function('', 'return true;') with '__return_true'
    – Rarst
    Commented Dec 21, 2010 at 13:38
  • 4
    You could just hook onto admin_init instead of init and you'll not need that is_admin() check.
    – t31os
    Commented Dec 21, 2010 at 13:47
  • @t31os yep - that will work too. I just usually have giant wrapper running on init and it is more convenient to keep both admin and non-admin stuff in there.
    – Rarst
    Commented Dec 21, 2010 at 13:53
  • anonymous functions only work in PHP 5.3+. I'd be curious to know more about the disadvantages of create_function Commented Dec 21, 2010 at 14:09
3

https://developer.wordpress.org/reference/functions/is_admin/

if ( ! is_admin() ) {
     echo "You are viewing the theme";
} else {
     echo "You are viewing the WordPress Administration Panels";
}
1

use the Hook admin_init and later hooks, the init-Hook comes realy ealier as only the admin. But it is important, when you use li18n-functions or AJAX, than it is better you use init.

0

Update in 2020, Working answer, I went to different answers none of them works now in new WordPress.

function hide_categories_for_specific_user( $exclusions, $args ){

if ( ((defined( 'REST_REQUEST' ) && REST_REQUEST) or $GLOBALS['pagenow'] === 'edit.php' ) && !current_user_can( 'manage_options' ) ) {

   // IDs of terms to be excluded
   $exclude_array = array("12","16","17"); // CHANGE THIS TO IDs OF YOUR TERMS


   // Generation of exclusion SQL code
   $exterms = wp_parse_id_list( $exclude_array );
   foreach ( $exterms as $exterm ) {
           if ( empty($exclusions) )
               $exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' ';
       else
               $exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' ';
   }

   // Closing bracket
   if ( !empty($exclusions) )
   $exclusions .= ')';

   // Return our SQL statement

  }
   return $exclusions;
}

 // Finally hook up our filter
 add_filter( 'list_terms_exclusions', 'hide_categories_for_specific_user', 10, 2 );

answer final help from Hide Some Categories in Post Editor

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.