Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Deletes a specified menu item from all sites on all networks on a WordPress multisite.
<?php
// Script must be run with wp eval-file
if(!defined('ABSPATH')){
die('You shall not pass.');
}
class DeleteSpecifiedWordPressMenuItem {
protected $menuNameToBeDeleted;
protected $menuNames;
protected $networks;
public function __construct()
{
$this->menuNameToBeDeleted = "About Me";
$this->menuNames = ['Header Navigation', 'Footer Navigation'];
$this->networks = get_networks();
}
public function deleteMenuItem(){
foreach($this->networks as $network){
$sites = get_sites(['network_id' => $network->id]);
foreach($this->menuNames as $menuName){
foreach($sites as $site) {
$url = $site->domain . $site->path;
$output = shell_exec("wp menu item list '" . $menuName . "' --url=$url");
$array = explode("\n", $output);
$matches = preg_grep("/$this->menuNameToBeDeleted/", $array);
if($matches){
$match = array_shift($matches);
$pieces = explode("\t", $match);
$deletedOutput = shell_exec("wp menu item delete $pieces[0] --url=$url");
printf(str_replace(".\n", "", $deletedOutput) . " for $menuName in $url\n");
} else {
printf("No matches found for $menuName in $url\n");
}
}
}
}
}
}
$object = new DeleteSpecifiedWordPressMenuItem();
$object->deleteMenuItem();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.