Skip to content

Instantly share code, notes, and snippets.

@rorymcdaniel
Last active March 7, 2017 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rorymcdaniel/2788e2aa77418d23969cb99734022cab to your computer and use it in GitHub Desktop.
Save rorymcdaniel/2788e2aa77418d23969cb99734022cab to your computer and use it in GitHub Desktop.
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