PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, July 8, 2022

[FIXED] how can I condense my repetitive/redundant php code?

 July 08, 2022     custom-post-type, php, posts, wordpress     No comments   

Issue

I have created a function in my WordPress functions.php file to highlight a number of links when you are on/viewing custom post type pages.

I am a newbie with php, so I wanted to ask someone how can I change this piece of code to be less repetitive/redundant?

As you can see below, I have doubles of the same post types and the class statement is being set four times. The links are also positioned in two different menu locations, hence the doubling up.

// filter current_page_parent to events menu link
function menu_class_filter_event($classes) {
    // Remove "current_page_parent" class
    $classes = array_diff($classes, array('current_page_parent'));

    // If this is the "event" custom post type, highlight the correct menu items
    if (in_array('menu-item-36', $classes) && get_post_type() === 'event') {
        $classes[] = 'current_page_parent';
    }
    if (in_array('menu-item-54', $classes) && get_post_type() === 'event') {
        $classes[] = 'current_page_parent';
    }

    // If this is the "program" custom post type, highlight the correct menu items
    if (in_array('menu-item-124', $classes) && get_post_type() === 'program') {
        $classes[] = 'current_page_parent';
    }
    if (in_array('menu-item-126', $classes) && get_post_type() === 'program') {
        $classes[] = 'current_page_parent';
    }
    return $classes;
}
add_filter('nav_menu_css_class', 'menu_class_filter_event', 10, 2);

Solution

I'm not very fluent in PHP, but I would probably try to move it all under one if:

// filter current_page_parent to events menu link
function menu_class_filter_event($classes) {
    // Remove "current_page_parent" class
    $classes = array_diff($classes, array('current_page_parent'));
    
    $add_class = 
        get_post_type() === 'event' && (
            in_array('menu-item-36', $classes) || 
            in_array('menu-item-54', $classes))
        || get_post_type() === 'program' && (
            in_array('menu-item-124', $classes) ||
            in_array('menu-item-126', $classes));

    if ($add_class) {
        $classes[] = 'current_page_parent';
    }
    return $classes;
}

You could try to combine the in_array like they did here, but I don't think it adds to readability in this case.



Answered By - Kryštof Vosyka
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing