Menu items (all menus) being deleted randomly on their own [duplicate]

Here are two loggers to help you to tackle the problem.

1) If you want to know when the number of nav menu items changes, you can use the following logger:

/**
 * A logger for nav menu items count - writes to the nav.log file.
 *
 * @link http://wordpress.stackexchange.com/a/149394/26350
 */

add_filter( 'wp_get_nav_menu_items', 
    function( $items, $menu, $args ) 
    {
        $file="/path/to/nav.log"; // Edit this filepath to your needs.
        if( file_exists( $file ) && is_writeable( $file ) )
        {
            $s = sprintf( " %s - menu: %s - count: %d %s" , date( "c" ),
                $menu->slug,
                $menu->count,
                PHP_EOL
            );
            file_put_contents( $file, $s, FILE_APPEND | LOCK_EX );
        }
        return $items;
    }
, PHP_INT_MAX, 3 );

I’ve suggested this logger on another site to a user having similar problem.

Here’s an example of the output:

 2014-06-12T13:14:47+00:00 - menu: primary - count: 11
 2014-06-12T13:14:47+00:00 - menu: primary - count: 11

2) You could try to log the INSERT, UPDATE and DELETE queries executed by the $wpdb object.

/**
 * Log the INSERT, UPDATE, DELETE database queries to the sql.log file.
 *
 * @link http://wordpress.stackexchange.com/a/149394/26350
 */

add_filter( 'query', 
    function( $query )
    {
        if( FALSE !== stripos( $query, 'UPDATE ' )
            || FALSE !== stripos( $query, 'INSERT ' )
            || FALSE !== stripos( $query, 'DELETE ' )
         ) {
                $file="/path/to/sql.log"; // Edit this filepath to your needs.  
                if( file_exists( $file ) && is_writeable( $file ) ) 
                    file_put_contents( 
                        $file, 
                        date( 'c' ) . ' - ' . $query . PHP_EOL, 
                        FILE_APPEND | LOCK_EX 
                    );            
        }
        return $query;
    }
, PHP_INT_MAX );

So you could try to use the info from nav.log to inspect the SQL queries from sql.log around the time the nav menu items changes.

Just remember to modify the log file paths and make sure the log files are writeable.

These loggers could be extended further. We could for example additionally log:

  • the current url,
  • the current user,
  • etc.

I hope this helps.

Leave a Comment