i've got a strange problem. I have two html list, when i choose a region, it sends an ajax request to wordpress and returns the list of this region's province. The problem is that the get_terms() function of wordpress doesn't retrieve any province when we are not logged in, even if he correctly received the data.
Here is the code, hope someone can help me.
<?php
/*
Plugin Name: Ajax Départements
Description: Récupère les sous categories(départements) d'une categorie(région)
Version: 0.1
Author: EnessFr
*/
//Enregstrement du trigger et du callback
add_action( 'wp_ajax_dpt-ajax-request', 'ajaxGetDepartement' );
add_action( 'wp_ajax_nopriv_dpt-ajax-request', 'ajaxGetDepartement' );
//insertion du script
wp_enqueue_script( 'dpt-ajax-request', plugin_dir_url( __FILE__ ) . 'ajax.js', array( 'jquery' ), null, true );
//Enregistrement de l'url ajax
wp_localize_script( 'dpt-ajax-request', 'dptAjaxRequest', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
//callback ajax
function ajaxGetDepartement(){
$departements = null;
$regionToExclude = array();
$tmpRegionName = $_POST['region'];
//Récupération des régions parents
$regionsParents = get_terms('category',array(
'parent' => 0,
'hide_empty' => false,
));
foreach($regionsParents as $rg){
array_push($regionToExclude, $rg->term_id);
}
array_push($regionToExclude, 1); // 1 = catégorie non-classé
//Récupération de la région passée en arguments
if(strval($_POST['region']) != 'wqsftaxoall'){ // une région valable est reçus
$tmpRegion = get_terms('category',array(
'slug' => $tmpRegionName,
'hide_empty' => false,
)
);
$regionParent = $tmpRegion[0]->term_id;
//Récupération des départements
$departements = get_terms('category',array(
'child_of' => $regionParent,
'hide_empty' => false,
'orderby' => 'name',
'order' => 'ASC',
'exclude' => $regionToExclude,
));
}else{ //récupération de tous les départements
//Récupération des départements
$departements = get_terms('category',array(
'hide_empty' => false,
'orderby' => 'name',
'order' => 'ASC',
'exclude' => $regionToExclude,
));
}
//Construction et afichage des résultats
$options = '<option selected value="wqsftaxoall">Tous les départements</option>';
foreach($departements as $dpt){
$options .= '<option value="'.$dpt->slug.'">'.$dpt->name.'</option>';
}
echo $options;
die();
}
?>
EDIT: It seems the problem is the get_terms function which return an empty array when a not logged user perform tha ajax call. When the user is logged in, the array contains the category list.
EDIT2: I still have the problem, searched on google, I can't find anyone with the same problem. I don't know what to do now, maybe i've missed something but i don't know what. help me please :).
I got the answer! It was a plugin: Role Scope. It put a restriction on taxonomies, I've uncheck the restriction on this particular taxo and now it works.