Redirect users from admin pages the optimal solution

On two different sites I use two somewhat different solutions. Are they different in terms of security and performance? Which is better? Are there even better ones?

Solution 1.

/** Redirect users from admin pages if not administrators **/ add_action( 'admin_init', function() {     if( ! current_user_can( 'manage_options' ) && ( ! wp_doing_ajax() ) ) {         wp_safe_redirect( site_url() );         exit;     } } ); 

Solution 2.

/** Redirect users from admin pages if not administrators **/ add_action( 'admin_init', function() {     if( ! in_array( 'administrator', wp_get_current_user()->roles ) ) {         wp_redirect( get_bloginfo( 'wpurl' ) );         exit;     } } ); 

How to break line / add to ADMIN menu

Disclaimer: This question is not about "frontend" nav menu

I am looking for a way how to break line in a menu item label in WP admin sidebar menu, sidereason is that the custom post type label I am registering is too long for 1 line and I don’t really want to mess with the width of the admin menu.

I tried adding both "\n" and <br> into the custom post type label, but <br> gets escaped and line breaks are not converted.

I tried to look on the Internet, but I mainly found articles how to line break in "frontend" menu items which is not what I am looking for.

Is there some sort of filter or other way to do this?

Note: I would also like to avoid tinkering with anything global or special characters like nonbreaking hyphen, if possible
Note2: I know there is auto linebreak in place, however I need a manually added line break because the term is in some-thing format so it breaks on dash which is unwanted behavior

Which admin color represents what?

In wordpress, the admin color scheme has 7(basic, more or less) colors, the first 4 represent "colors", and the last 3 "base", "focus", "current". In the first 4 colors, which color represents what?

In the default-fresh scheme in array 1 color # 1d2327, represents the background, and in sunrise 2 colors # cf4944 in the array represents the backround. Why is this so and how to get and insert color for text and hover etc …

Also the colors for hover and text are mixed, how to understand the admin color scheme?

Fresh 1. color = #1d2327 = background

    wp_admin_css_color(         'fresh',         _x( 'Default', 'admin color scheme' ),         false,         array( '#1d2327', '#2c3338', '#3582c4', '#72aee6' ),         array(             'base'    => '#a7aaad',             'focus'   => '#72aee6',             'current' => '#fff',         )     ); 

Sunrise 2. color = #cf4944 = background

    wp_admin_css_color(         'sunrise',         _x( 'Sunrise', 'admin color scheme' ),         admin_url( "css/colors/sunrise/colors$  suffix.css" ),         array( '#b43c38', '#cf4944', '#dd823b', '#ccaf0b' ),         array(             'base'    => '#f3f1f1',             'focus'   => '#fff',             'current' => '#fff',         )     ); 

Clicking custom plugin admin menu redirects to “No Update Required” on live site

I built a custom WordPress plugin that sends woocommerce purchase info to a CRM. The plugin works perfectly on localhost. But on live site, whenever I click on the admin menu, I always get redirected to "No Update Required" page.

enter image description here

Here is my main plugin file:

/**  * Plugin Name: Woo Crm  * Description: Sends customer data to the CRM on purchase.  */  if (!defined('ABSPATH')) {     echo 'I am a plugin I cannot do anything when called directly.';     exit; }  // Config define('PLUGIN_NAME', 'Woo Crm'); define('PLUGIN_SLUG', 'woo_crm'); define('PLUGIN_URL', plugin_dir_url(__FILE__)); define('PLUGIN_PATH', plugin_dir_path(__FILE__));  // Imports require_once(PLUGIN_PATH . 'utils/utils.php'); require_once(PLUGIN_PATH . 'views/views.php'); require_once(PLUGIN_PATH . 'services/crm/crm.php'); require_once(PLUGIN_PATH . 'views/components/settings/settings.php');  class WooCrm {     function __construct()     {         // Register Styles         add_action('admin_enqueue_scripts', array($  this, 'register_styles'));          // Create Menu Page         add_action('admin_menu', array($  this, 'plugin_setup_menu'));          // Add Settings Fields to Menu Page         add_action('admin_init', array('settings', 'init'));          // Woocommerce hook         /**          * First check if WooCommerce is activated          * Then check if hook isn't empty          */         if (class_exists('WooCommerce')) {             $  hook = get_when_to_send_data_hook();             if (!empty($  hook)) {                 add_action($  hook, array($  this, 'woo_order_create_client'));             }         }     }      function woo_order_create_client($  order_id)     {         // Create client logic     }      function plugin_setup_menu()     {         $  icon_url = PLUGIN_URL . "public/images/dash-icon.png";         add_menu_page(__(PLUGIN_NAME, PLUGIN_SLUG), __(PLUGIN_NAME, PLUGIN_SLUG), 'manage_options', PLUGIN_SLUG, array('views', 'admin'), $  icon_url);     }      function register_styles()     {         wp_register_style('woo_crm_admin', PLUGIN_URL . 'public/css/admin.css');         wp_enqueue_style('woo_crm_admin');     }  }  new WooCrm(); 

Any help would be highly appreciated.

Add a functionality to quickly filter custom posts in Admin Area

I am creating a website for a doggie rescue, basically configuring the backend so they can manage dogs, volunteers, etc.

The legacy system had a feature where the user could start typing into an input field and all matching results would be displayed as you typed, most likely making ajax calls.

They would like to have this feature in WordPress. I tried using Admin Columns but you have to create the filter, modify the value and then click the Filter button and wait for it to load the results. I want to avoid the "click filter button and wait for page to come back with results" part.

One of the CPT that I am creating, volunteers, apparently has 40K records, and they would like to filter through them by using this "filter as you type" idea.

Is this possible from WPAdmin? I mean, adding a text input field that would filter all the posts using AJAX?

Thanks!

Add term of current custom post type to admin body class using admin_body_class

I am trying to add the current custom post type term to the body class of my WordPress admin page. So when I am viewing an existing custom post type that has been assigned a term it will add that term to the body class.

I have found the following code but cannot get it to work for me:

add_filter( 'admin_body_class', 'rw_admin_body_class' ); function rw_admin_body_class( $  classes ) {     if ( 'post' != $  screen->base )         return $  classes;      global $  post;     $  terms = wp_get_post_terms( $  post->ID, 'product_cat', array( 'fields' => 'all' ) );     $  terms = wp_list_pluck( $  terms, 'slug' );     foreach ( $  terms as $  term )     {         $  classes .= ' my_taxonomy-' . $  term;     }      return $  classes; } 

Any pointers on how to get this working?

Admin only taxonomies?

I’m building a marketplace site using woocommerce, wcvendors pro, buddyboss marketplace and buddypress.

I’d like for the admin to have an admin-only taxonomy that she can use to tag/categorise certain products for use in featured blocks on the home page, for example.

What would be the best approach for this? Thanks!

Is “document loaded” different on admin side than public side?

I’m writing a plugin to support Google Graphs. It’s working fine on the public side, but is intermittently rendering weirdly on the admin side, and also intermittently throwing errors that make me think the JS is running too soon. (for example, TypeError: null is not an object (evaluating 'document.getElementById(colControlId).appendChild'), but when I go inspect the page, that element is absolutely there.

The function that drives everything is registered to the Google library as a callback:

// Load the Visualization API and the corechart package. google.charts.load('current', {'packages':['corechart', 'controls']});  // Set a callback to run when the Google Visualization API is loaded. google.charts.setOnLoadCallback(initializeData);  // Query a CSV for the data function initializeData() {         let graphs = document.getElementsByClassName("cwraggbp_chart"); 

The Google docs say, "When the packages [loaded above by google.charts.load] have finished loading, this callback function will be called with no arguments. The loader will also wait for the document to finish loading before calling the callback."

But, I can’t see how I can be getting this error intermittently if the DOM is fully loaded, and I don’t know what "document loaded" means. So, since this works flawlessly on the public side, but not in admin, I’m wondering if … something is different.

I’m loading the scripts thusly:

    public function enqueue_scripts() {              wp_enqueue_script( $  this->plugin_name . '-public',                 plugin_dir_url( __FILE__ )                     . 'js/cwra-google-graph-block-public.js', // this is where the JS above is                 array( $  this->plugin_name . 'googlecharts', 'jquery' ),                 $  this->date_version(                     'js/cwra-google-graph-block-public.js'), false );             wp_localize_script( $  this->plugin_name . '-public',                 'cwraggbp',                 array(                     'contentdir' => wp_upload_dir()['baseurl']                         . '/cwraggb'                 ));              wp_enqueue_script( $  this->plugin_name . 'googlecharts',                 'https://www.gstatic.com/charts/loader.js',                 array(), $  this->version, false );      } 

how to set post order by custom field in admin area wordpress

Hello friends

I have some posts that I have made in custom post type. working with Ajax Pagination. I want to post these orders by show, so I have a plugin "ACF" for custom fields . When I give the number to any post, that 1, 2, 3, then that post should be shown in the same order.

Can anyone tell me how it is possible.

Image 1

enter image description here

Image 2

enter image description here

Image 3

enter image description here