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.