Apply a filter only once

Question

I want to remove automatically the Home menu item, but only once, more precisely at the first run of WordPress or at the first child theme activation/loading. Now I have a function in the functions.php of my child theme that checks if the Home menu item exists and delete it from the menu. Of course, this function runs every time when WordPress loads. How to make it to run only once? I tried the [add_filter_once()][1] function, but I got only a PHP Fatal error: Call to undefined function add_filter_once().

function filter_wp_nav_menu_objects( $sorted_menu_items, $args ) { 

    foreach( $sorted_menu_items as $data ) {
        if ( in_array( "menu-item-home", $data->classes ) ) {
            wp_delete_post( $data->ID );
        }
    }

    return $sorted_menu_items;
} 

add_filter( 'wp_nav_menu_objects', 'filter_wp_nav_menu_objects', 10, 2 );
0
Iurie Malai 2 years 2020-12-14T12:10:38-05:00 0 Answers 10 views 0

Leave an answer

Browse
Browse