Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save doubleedesign/a3a4e26b4922f74a01f78fd734464237 to your computer and use it in GitHub Desktop.
Save doubleedesign/a3a4e26b4922f74a01f78fd734464237 to your computer and use it in GitHub Desktop.
Unhook a Ninja Forms plugin action from within another plugin. The context for this example was using Ninja Forms for Expression of Interest forms associated with Jobs (a custom post type), and a customised admin screen for viewing submissions. (This isn't the whole plugin, it's just the parts required to show how to unhook a Ninja Forms functio…
/**
* Job listings functionality for Client Website
* Note: Requires Ninja Forms plugin
* Note: Truncated for use in a gist to demonstrate unhooking a Ninja Forms function
*
* @since 1.0.0
* @package MyPlugin
* @subpackage MyPlugin/admin
*/
class MyPlugin_Jobs extends MyPlugin_Settings {
// ... CPTs, taxonomies, custom metaboxes etc go here; truncated for gist simplicity
// ... More methods for the custom functionality go here
// Method this gist is demonstrating
// Context: I'm using the "legacy" submissions page (the WordPress base admin page for the nf_sub CPT),
// but also need the Ninja Forms one kept intact, so need to remove the redirect
public function submissions_v2_remove_ninja_redirect() {
// To remove an action added in a plugin using add_action('hook', array($this, 'function')), you have to pass the same instance of that plugin that it calls "$this" when adding the action
// In the case of Ninja Forms, you have to get the current Ninja Forms instance and possibly also the instance of an object inside it, depending where the action is you want to remove;
// in my case it's the submissions menu object (NF_Admin_Menus_Submissions)
$ninja_instance = Ninja_Forms::instance()->menus['submissions'];
// Specific to my use case:
// Make sure "Show legacy submissions page" is OFF,
// because if it is on, the new Ninja Forms submissions page will be disabled and the Submissions link will go to the "legacy" one, which isn't what I wanted in this case
// Note: I also disabled the checkbox in the settings page (hackily using JavaScript loaded on that page) to prevent confusion and unexpected behaviour
$ninja_instance->load_legacy = 0;
// The actual removal of the unwanted action
// Note: Priority must be the same as the add_action, so in this case 10
remove_action('current_screen', array($ninja_instance, 'remove_legacy_submissions_page'), 10);
}
}
<?php
/**
* The core plugin class.
*
* This is used to define admin-specific and public-facing site hooks.
* Also maintains the unique identifier of this plugin as well as the current version.
*
* @since 1.0.0
* @package MyPlugin
* @subpackage MyPlugin/includes
*/
class MyPlugin {
/**
* The loader that's responsible for maintaining and registering all hooks that power
* the plugin.
*
* @since 1.0.0
* @access protected
* @var MyPlugin_Loader $loader Maintains and registers all hooks for the plugin.
*/
protected $loader;
/**
* Define the core functionality of the plugin.
*
* Set the plugin name and the plugin version that can be used throughout the plugin.
* Load the dependencies, define the locale, and set the hooks for the admin area and
* the public-facing side of the site.
*
* @since 1.0.0
*/
public function __construct() {
$this->version = defined('MYPLUGIN_VERSION') ? MYPLUGIN_VERSION : '1.0.1';
$this->plugin_name = 'myplugin';
$this->load_dependencies();
// ... more method calls here; truncated for gist simplicity
}
/**
* Load the required dependencies for this plugin,
* and create an instance of the loader which will be used to register the hooks with WordPress.
*
* @since 1.0.0
* @access private
*/
private function load_dependencies() {
// The class responsible for defining custom post types, taxonomies, and functions for "Jobs"
require_once plugin_dir_path(dirname(__FILE__)) . 'admin/class-myplugin-jobs.php';
// ... Other dependencies
// Initialise the loader
$this->loader = new MyPlugin_Loader();
}
/**
* Register the hooks related to the admin area functionality
* @since 1.0.0
* @access private
*/
private function define_admin_hooks() {
// ... more actions here; truncated for gist simplicity
$jobs = new MyPlugin_Jobs($this->plugin_name, $this->version);
// Call the function that does the unhook that this gist is demonstrating
// Note that this is added to a hook (admin_init) that runs BEFORE the one the unwanted action is run on (current_screen)
// running it on the same one (current_screen in this case), even at a higher priority, does not work
$this->loader->add_action('admin_init', $jobs, 'submissions_v2_remove_ninja_redirect', 10);
// ... more actions here; truncated for gist simplicity
}
// ... more methods here; truncated for gist simplicity
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment