0

I have written the function below to return parent id's of all categories in Worpress by category id. Everything works fine except it does not return the array... Any suggestions would be great! :-)

$current_category = (int) $_GET['current_category'];
$cat_ids = array($current_category);

function getParentCatIds($current_category,$cat_ids){
    $child = get_category($current_category);
    $parent_id = $child->parent;
    if($parent_id !== 0){
        array_push($cat_ids, $parent_id);
        getParentCatIds($parent_id,$cat_ids);
    }else{
        var_dump($cat_ids); // <--- this returns the right array
        return $cat_ids; // <--- this returns NULL
    }
}

if($current_category){
    $cat_ids = getParentCatIds($current_category,$cat_ids);
    var_dump($cat_ids); // <--- this returns NULL
}
Design B
  • 11
  • 3

3 Answers3

1

When you call getParentCatIds() (line 9), you didn't do anything with the return of the function. You should assign it or return it.

0

You should always sanitize input. A good starting point is filter_var(). Using the right flags for validation and sanitization and makes your input save(r). Keep in mind that FILTER_VALIDATE_* only tells you if something is valid, while FILTER_SANITIZE_* actually cleans your data from unwanted and potentially malicious data.

$currentCat = intval( $_GET['current_category'] );
$currentCat = filter_var( $currentCat, FILTER_SANITIZE_NUMBER_INT );
if ( empty( $currentCat ) ) {
    // Abort – no valid data
    return;
}

Then you can build an array, containing your original Category parent ID. This you can pass to array_walk(), with a callback as second parameter. The callback itself has assigned a collecting/ final array that is passed as reference and serves as target for your results. Now you can loop recursively over an indefineatly nested WordPress category hierarchy.

// Base array
$parents = [];
$callback = function( $index, $id ) use ( &$parent ) {
    0 !== get_category( $id )->parent and $parents[] = $id;
};
array_walk( [ get_category( $currentCat )->parent ], $callback );
kaiser
  • 21,817
  • 17
  • 90
  • 110
  • Thanx! For the feedback... Are you saying that (int) $_GET['current_category'] is not safe? $_GET['current_category'] is a number when I cast it to an int it would be zero or the correct number? Or am I missing something? Sanitation would not be necessary? – Design B Sep 26 '16 at 08:23
  • @DesignBytes There are a lot of things that can be a number – for e.g. an IP address. You should always validate and sanitize input. – kaiser Sep 26 '16 at 18:03
-4

1) You should have asked at wordpress.stackexchange.com

2) I think solution is: on line 9 , instead of
getParentCatIds($parent_id,$cat_ids); you should have
return getParentCatIds($parent_id,$cat_ids);

T.Todua
  • 53,146
  • 19
  • 236
  • 237
  • 2
    I can't believe that you have 13.5 K points and giving such replies :/ – Hyder B. Sep 23 '16 at 18:34
  • Great pointer! It is more a general php question than a Wordpress question. That's why I post it here... :-) – Design B Sep 23 '16 at 18:35
  • @HyderB. You dont have to believe :) Deisgn Bytes - you've made a small mistake that you have asked here, and i just advised to post it on appropriate place. There are posted PHP questions too, with more advanced knowledge of WP related functions. I've updated answer. – T.Todua Sep 23 '16 at 18:37
  • There are close vote flags for this. And the last thing you should do is to use (1) an answer to **push** people at this (2) headline markup to stand out (this is public shaming) and (3) CAPS LOCK (which you gladly left out). – kaiser Sep 23 '16 at 18:50
  • My mistake! You are so right! The return was the fix! :-) – Design B Sep 23 '16 at 19:03
  • Did not downvote your answer... I just gave you an upvote! Thank you for your help. – Design B Sep 26 '16 at 08:26