0

Inside a custom plugin to translate Posts contents I use add_filter() and plugins_loaded() hook to replace all $title, $excerpt, $content, $postmeta.

As the documentation says wp Ajax use admin_ajax.php. So !is_admin()can't be used to ignore add_filter($title) inside WordPress administration when an Ajax request is called in the frontend.

I also tried current_user_can('administrator') and current_user_can('manage_options') but both of them don't work.

Currently I use :

    function atv_convert_multilingual(){
        global $pagenow;
        $ignoreadmin = array('edit.php', 'admin.php');
        if( ! in_array( $pagenow, $ignoreadmin)) {
            add_filter('the_title','atv_translate_title',10,2);
            //other add_filter(). 
        }
    }
add_action('plugins_loaded', 'atv_convert_multilingual');

Ignoring specific $pagenow works but I don't think that's the right method.Do you have a safer and global way of taking all administration into account?

For greater precision: When I use !is_admin condition the filters are applied only on page load but ignored by ajax request. The Posts list of administration display title in the default language. wp_doing_ajax() used as a condition for calling plugins_loaded()hook is not possible either, since it also uses wp-admin() to work.

1 Answer 1

0

There are a couple of things here that should always be kept in-mind when you're thinking of administrators and WordPress is_admin().

  1. is_admin() tells you only whether or not the current WordPress request has been sent to the WP admin backend. This also, actually, include admin-ajax.php requests. It doesn't tell you whether or not the current logged-in user has administrator priviledges.
  2. A consistent approach to determining whether the current logged-in user has administrator priviledges, is current_user_can( 'manage_options' ). There are scenarios, which are rare, where the result of this could be misleading, but for your purposes, you can ignore that.
  3. You can only test capabilities on a logged-in user if WordPress "knows" that the user is logged-in. For most requests, this is after (and only after) the init action has fired. plugins_loaded is too early.

With all that said, taking your code exactly as you've provided and adjusting the hook and the method to determine whether or not you ignore an admin, this is the code you would use:

function atv_convert_multilingual() {
    $ignoreadmin = current_user_can( 'manage_options' );
    if ( !$ignoreadmin ) {
        add_filter( 'the_title', 'atv_translate_title', 10, 2 );
        //other add_filter().
    }
}

add_action( 'init', 'atv_convert_multilingual' );
1
  • After testing hierarchical hook, your are right add_action('init',..)is suffisant to apply my plugin of whole website but the condition ! current_user_can( 'manage_options' ); or using instead'level-10'or 'administrator'do nothing and my plugin is blocked .
    – imagIne
    Sep 14 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.