{"id":2345,"date":"2021-02-21T19:55:20","date_gmt":"2021-02-21T18:55:20","guid":{"rendered":"https:\/\/codeart.studio\/?p=2345"},"modified":"2021-02-22T15:49:30","modified_gmt":"2021-02-22T14:49:30","slug":"remove-a-wordpress-menu-page-with-static-class","status":"publish","type":"post","link":"https:\/\/codeart.studio\/remove-a-wordpress-menu-page-with-static-class\/","title":{"rendered":"Remove a WordPress menu page added through a static method (inside a class)"},"content":{"rendered":"\n<p>Removing annoying and unnecessary WordPress menu pages added by 3rd party plugins that clutter your dashboard is one of those things that seem like they&#8217;re not worth the trouble, but when you do them, you realize just how nice it is to have a clutter-free work environment. It&#8217;s kind of like rearranging and cleaning up your work desk once in a while. <\/p>\n\n\n\n<p>And, while removing some WordPress menu pages is easy as pie, there are those that are incredibly stubborn and won&#8217;t go away no matter which filter or action hook you use. I&#8217;m sure you&#8217;ve been there. Normally, removing a menu page involves tracking down the name of the action hook a plugin or theme author used when hooking into the standard WordPress function &#8216;<strong>add_menu_page<\/strong>&#8216; which is usually initialized through &#8216;<strong>admin_init<\/strong>&#8216; action hook. Here&#8217;s an example from <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/add_menu_page\/\" target=\"_blank\" rel=\"noreferrer noopener\">WordPress Codex<\/a> so it&#8217;s easier to follow along:<\/p>\n\n\n<div id=\"code-highlight-blokblock_6032a51f7b899\" class=\"code-highlight-blok blokovi \">\n<div class=\"innerWrap\">\n    <pre class=\"line-numbers\" data-prismjs-copy=\"Copy\"><code class=\"language-php\">\n    add_action( 'admin_menu', 'register_my_custom_menu_page' );\r\nfunction register_my_custom_menu_page() {\r\n  \/\/ add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position );\r\n  add_menu_page( 'Custom Menu Page Title', 'Custom Menu Page', 'manage_options', 'custom.php', '', 'dashicons-welcome-widgets-menus', 90 );\r\n}<\/code><\/pre>\n<\/div><!--\/.innerWrap-->\n<\/div><!--\/.usluge-blok-->\n\n\n\n<p>Seems simple enough, right? Removing this page would be as simple as tracking down slug of the page and using the function &#8216;<strong>remove_menu_page<\/strong>&#8216; initialized through &#8216;<strong>admin_init<\/strong>&#8216; hook. So, a little bit like this:<\/p>\n\n\n<div id=\"code-highlight-blokblock_6032a6b67b89a\" class=\"code-highlight-blok blokovi \">\n<div class=\"innerWrap\">\n    <pre class=\"line-numbers\" data-prismjs-copy=\"Copy\"><code class=\"language-php\">\n    add_action( 'admin_init', 'remove_my_custom_menu_page' );\r\nfunction remove_my_custom_menu_page(){\r\n  remove_menu_page( 'custom.php' );\r\n}<\/code><\/pre>\n<\/div><!--\/.innerWrap-->\n<\/div><!--\/.usluge-blok-->\n\n\n\n<p>This would be an ideal scenario. But, things can get much more complicated than this. There are a lot of plugins out there that add menu pages through <a href=\"https:\/\/www.php.net\/manual\/en\/language.oop5.static.php\" target=\"_blank\" rel=\"noreferrer noopener\">static PHP methods<\/a> inside classes. It makes sense to use the object oriented approach to build your plugins, but it makes it that much more difficult to track down and remove a page initialized through a static method.  So, I&#8217;ll show you a real world example of a popular plugin that does this, and then I&#8217;ll show you how to remove a page added this way to the WordPress dashboard. <\/p>\n\n\n\n<p>The plugin I&#8217;ll use in this example is <a href=\"https:\/\/wordpress.org\/plugins\/yith-woocommerce-wishlist\/\" target=\"_blank\" rel=\"noreferrer noopener\">YITH WooCommerce Wishlist<\/a>, a very popular WooCommerce wishlist plugin. Let&#8217;s dive right in, here&#8217;s how this plugin adds its dashboard items. I&#8217;ll copy a block of code and explain things below the block.<\/p>\n\n\n<div id=\"code-highlight-blokblock_6032a9aa7b89b\" class=\"code-highlight-blok blokovi \">\n<div class=\"innerWrap\">\n    <pre class=\"line-numbers\" data-prismjs-copy=\"Copy\"><code class=\"language-php\">\n    class YITH_WCWL_Admin {\r\n\r\n\t\t\/**\r\n\t\t * Single instance of the class\r\n\t\t *\r\n\t\t * @var \\YITH_WCWL_Admin\r\n\t\t * @since 2.0.0\r\n\t\t *\/\r\n\t\tprotected static $instance;\r\n\t\t\r\n\t\tpublic static function get_instance() {\r\n\t\t\tif ( is_null( self::$instance ) ) {\r\n\t\t\t\tself::$instance = new self();\r\n\t\t\t}\r\n\r\n\t\t\treturn self::$instance;\r\n\t\t}\r\n\t\t\r\n\t\t\/**\r\n\t\t * Constructor of the class\r\n\t\t *\r\n\t\t * @return \\YITH_WCWL_Admin\r\n\t\t * @since 2.0.0\r\n\t\t *\/\r\n\t\tpublic function __construct() {\r\n\t\t\t\/\/ install plugin, or update from older versions.\r\n\t\t\tadd_action( 'init', array( $this, 'install' ) );\r\n\r\n\t\t\t\/\/ init admin processing.\r\n\t\t\tadd_action( 'init', array( $this, 'init' ) );\r\n\r\n\t\t\t\/\/ enqueue scripts.\r\n\t\t\tadd_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ), 20 );\r\n\r\n\t\t\t\/\/ plugin panel options.\r\n\t\t\tadd_filter( 'yith_plugin_fw_panel_wc_extra_row_classes', array( $this, 'mark_options_disabled' ), 10, 23 );\r\n\r\n\t\t\t\/\/ add plugin links.\r\n\t\t\tadd_filter( 'plugin_action_links_' . plugin_basename( YITH_WCWL_DIR . 'init.php' ), array( $this, 'action_links' ) );\r\n\t\t\tadd_filter( 'yith_show_plugin_row_meta', array( $this, 'add_plugin_meta' ), 10, 5 );\r\n\r\n\t\t\t\/\/ register wishlist panel.\r\n\t\t\tadd_action( 'admin_menu', array( $this, 'register_panel' ), 5 );\r\n\t\t\tadd_action( 'yith_wcwl_premium_tab', array( $this, 'print_premium_tab' ) );\r\n\r\n\t\t\t\/\/ add a post display state for special WC pages.\r\n\t\t\tadd_filter( 'display_post_states', array( $this, 'add_display_post_states' ), 10, 2 );\r\n\t\t}\r\n\t\t\r\n\t\t\/**\r\n * Unique access to instance of YITH_WCWL_Admin class\r\n *\r\n * @return \\YITH_WCWL_Admin\r\n * @since 2.0.0\r\n *\/\r\nfunction YITH_WCWL_Admin() {\r\n\treturn defined( 'YITH_WCWL_PREMIUM' ) ? YITH_WCWL_Admin_Premium::get_instance() : YITH_WCWL_Admin::get_instance();\r\n}<\/code><\/pre>\n<\/div><!--\/.innerWrap-->\n<\/div><!--\/.usluge-blok-->\n\n\n\n<p>I removed a few lines from the file, because we don&#8217;t need to see all the code. Anyway, as you&#8217;ll see, the plugin author is using <strong>YITH_WCWL_ADMIN<\/strong> class to add menu pages (and a lot of other things the plugin needs). What we&#8217;re most interested here in, are the lines 43 and 44, because these are the pages we want to remove. The page &#8216;<strong>register_panel<\/strong>&#8216; is added via &#8216;<strong>admin_menu<\/strong>&#8216; action hook, and the &#8216;<strong>print_premium_tab<\/strong>&#8216; page is added via <strong>&#8216;yith_wcwl_premium_tab<\/strong>&#8216; action hook. We need this information to be able to remove these pages. Also, as you can see, we have an additional parameter in the action hook, the <strong>$this<\/strong> object, which is a reference to the class in which the hook is defined in. This information is also of vital importance if we wish to remove these pages. <\/p>\n\n\n\n<p>Also, it&#8217;s important to notice that the class is instantiated through a static get_instance() function.<\/p>\n\n\n\n<p>Knowing all this, we can now write our removal code. I&#8217;ll just paste the code block and explain things below:<\/p>\n\n\n<div id=\"code-highlight-blokblock_6032aba87b89c\" class=\"code-highlight-blok blokovi \">\n<div class=\"innerWrap\">\n    <pre class=\"line-numbers\" data-prismjs-copy=\"Copy\"><code class=\"language-javascript\">\n    add_action('init', __NAMESPACE__ . '\\\\codeart_remove_yith_pages', 999);\r\n  function codeart_remove_yith_pages() {\r\n  if( class_exists( 'YWRR_Review_Reminder' ) ){\r\n  \/\/review reminder\r\n  $class = \\YWRR_Review_Reminder::get_instance();\r\n  remove_action( 'admin_menu', array( $class, 'add_menu_page' ), 5 );\r\n  remove_action( 'yith_review_reminder_premium', array( $class, 'premium_tab' ) );\r\n}<\/code><\/pre>\n<\/div><!--\/.innerWrap-->\n<\/div><!--\/.usluge-blok-->\n\n\n\n<p>So, a couple of things are going on here:<\/p>\n\n\n\n<div class=\"wp-block-group numberedList\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<ol class=\"wp-block-list\"><li>on line 1, we&#8217;re initializing our code with the action hook &#8216;<strong>init<\/strong>&#8216; with the priority of 999.  We want to use a large number here, just to be able to execute our hook as late as possible, to make sure we&#8217;re not removing something that&#8217;s not yet added.<\/li><li>on line 3 we&#8217;re checking whether the class we&#8217;re about the instantiate actually exists. We absolutely must do this, not just because it&#8217;s common sense when instantiating a class, but also if you ever deactivate a plugin whose class you&#8217;re instantiating, and don&#8217;t remove this code, your site is going to crash and burn.<\/li><li>on line 5 we&#8217;re instantiating our class with the static get_instance() function<\/li><li>on lines 6 &amp; 7 we&#8217;re removing the pages with our instantiated class in the form of the $class parameter<\/li><\/ol>\n<\/div><\/div>\n\n\n\n<p>And that&#8217;s it. Now you have a real world example of how to remove a WordPress menu page added through a class instantiated through a static method.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Removing annoying and unnecessary WordPress menu pages added by 3rd party plugins that clutter your dashboard is one of those things that seem like they&#8217;re not worth the trouble, but &hellip; <a href=\"https:\/\/codeart.studio\/remove-a-wordpress-menu-page-with-static-class\/\">More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-2345","post","type-post","status-publish","format-standard","hentry","category-blog"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Remove a WordPress menu page added through a static method (inside a class) - CodeArt Studio<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/codeart.studio\/remove-a-wordpress-menu-page-with-static-class\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Remove a WordPress menu page added through a static method (inside a class) - CodeArt Studio\" \/>\n<meta property=\"og:description\" content=\"Removing annoying and unnecessary WordPress menu pages added by 3rd party plugins that clutter your dashboard is one of those things that seem like they&#8217;re not worth the trouble, but &hellip; More\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codeart.studio\/remove-a-wordpress-menu-page-with-static-class\/\" \/>\n<meta property=\"og:site_name\" content=\"CodeArt Studio\" \/>\n<meta property=\"article:published_time\" content=\"2021-02-21T18:55:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-02-22T14:49:30+00:00\" \/>\n<meta name=\"author\" content=\"CodeArt\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"CodeArt\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/codeart.studio\/remove-a-wordpress-menu-page-with-static-class\/\",\"url\":\"https:\/\/codeart.studio\/remove-a-wordpress-menu-page-with-static-class\/\",\"name\":\"Remove a WordPress menu page added through a static method (inside a class) - CodeArt Studio\",\"isPartOf\":{\"@id\":\"https:\/\/codeart.studio\/#website\"},\"datePublished\":\"2021-02-21T18:55:20+00:00\",\"dateModified\":\"2021-02-22T14:49:30+00:00\",\"author\":{\"@id\":\"https:\/\/codeart.studio\/#\/schema\/person\/1b0cd5eeb1ee7bb87ff6d62387575879\"},\"breadcrumb\":{\"@id\":\"https:\/\/codeart.studio\/remove-a-wordpress-menu-page-with-static-class\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/codeart.studio\/remove-a-wordpress-menu-page-with-static-class\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/codeart.studio\/remove-a-wordpress-menu-page-with-static-class\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/codeart.studio\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Remove a WordPress menu page added through a static method (inside a class)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/codeart.studio\/#website\",\"url\":\"https:\/\/codeart.studio\/\",\"name\":\"CodeArt Studio\",\"description\":\"Samo jo\u0161 jedna WordPress web-stranica\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/codeart.studio\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/codeart.studio\/#\/schema\/person\/1b0cd5eeb1ee7bb87ff6d62387575879\",\"name\":\"CodeArt\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/codeart.studio\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/a9fe9f46fcf3e84a874ad29f30465e42?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/a9fe9f46fcf3e84a874ad29f30465e42?s=96&d=mm&r=g\",\"caption\":\"CodeArt\"},\"description\":\"Zelimir is a WordPress developer (11+ years) based in Croatia. He loves working with REST APIs, and building ecommerce stores based on Roots\/Sage &amp; WooCommerce. When he's not parsing JSON into XML, he's either working out in his home gym or spending time with his kids.\",\"sameAs\":[\"http:\/\/codeart.studio\/cms\"],\"url\":\"https:\/\/codeart.studio\/author\/zelimir83\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Remove a WordPress menu page added through a static method (inside a class) - CodeArt Studio","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/codeart.studio\/remove-a-wordpress-menu-page-with-static-class\/","og_locale":"en_US","og_type":"article","og_title":"Remove a WordPress menu page added through a static method (inside a class) - CodeArt Studio","og_description":"Removing annoying and unnecessary WordPress menu pages added by 3rd party plugins that clutter your dashboard is one of those things that seem like they&#8217;re not worth the trouble, but &hellip; More","og_url":"https:\/\/codeart.studio\/remove-a-wordpress-menu-page-with-static-class\/","og_site_name":"CodeArt Studio","article_published_time":"2021-02-21T18:55:20+00:00","article_modified_time":"2021-02-22T14:49:30+00:00","author":"CodeArt","twitter_card":"summary_large_image","twitter_misc":{"Written by":"CodeArt","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/codeart.studio\/remove-a-wordpress-menu-page-with-static-class\/","url":"https:\/\/codeart.studio\/remove-a-wordpress-menu-page-with-static-class\/","name":"Remove a WordPress menu page added through a static method (inside a class) - CodeArt Studio","isPartOf":{"@id":"https:\/\/codeart.studio\/#website"},"datePublished":"2021-02-21T18:55:20+00:00","dateModified":"2021-02-22T14:49:30+00:00","author":{"@id":"https:\/\/codeart.studio\/#\/schema\/person\/1b0cd5eeb1ee7bb87ff6d62387575879"},"breadcrumb":{"@id":"https:\/\/codeart.studio\/remove-a-wordpress-menu-page-with-static-class\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codeart.studio\/remove-a-wordpress-menu-page-with-static-class\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/codeart.studio\/remove-a-wordpress-menu-page-with-static-class\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codeart.studio\/"},{"@type":"ListItem","position":2,"name":"Remove a WordPress menu page added through a static method (inside a class)"}]},{"@type":"WebSite","@id":"https:\/\/codeart.studio\/#website","url":"https:\/\/codeart.studio\/","name":"CodeArt Studio","description":"Samo jo\u0161 jedna WordPress web-stranica","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/codeart.studio\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/codeart.studio\/#\/schema\/person\/1b0cd5eeb1ee7bb87ff6d62387575879","name":"CodeArt","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codeart.studio\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/a9fe9f46fcf3e84a874ad29f30465e42?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a9fe9f46fcf3e84a874ad29f30465e42?s=96&d=mm&r=g","caption":"CodeArt"},"description":"Zelimir is a WordPress developer (11+ years) based in Croatia. He loves working with REST APIs, and building ecommerce stores based on Roots\/Sage &amp; WooCommerce. When he's not parsing JSON into XML, he's either working out in his home gym or spending time with his kids.","sameAs":["http:\/\/codeart.studio\/cms"],"url":"https:\/\/codeart.studio\/author\/zelimir83\/"}]}},"_links":{"self":[{"href":"https:\/\/codeart.studio\/wp-json\/wp\/v2\/posts\/2345","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeart.studio\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeart.studio\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeart.studio\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeart.studio\/wp-json\/wp\/v2\/comments?post=2345"}],"version-history":[{"count":0,"href":"https:\/\/codeart.studio\/wp-json\/wp\/v2\/posts\/2345\/revisions"}],"wp:attachment":[{"href":"https:\/\/codeart.studio\/wp-json\/wp\/v2\/media?parent=2345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeart.studio\/wp-json\/wp\/v2\/categories?post=2345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeart.studio\/wp-json\/wp\/v2\/tags?post=2345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}