I wanted to create a submenu but setting the priority to 11 doesn't seem to work.
I have this on my class-plugin-name.php >> define_admin_hooks()
$plugin_admin = new Plugin_Name_Admin( $this->get_plugin_name(), $this->get_version() );
$this->loader->add_action( 'admin_menu' , $plugin_admin, 'make_menu' );
And on my Plugin_Name_Admin class, I've got:
public function make_menu() {
add_submenu_page('wp-plugin-name', 'Test', 'Test', 'manage_options', 'wp-plugin-name-test', array($this, 'test_page') );
}
public function safe_page() {
echo "test";
}
My problem is that the submenu item shows up but gives me an Insufficient Permissions wp_die(); error.
Now I did a lot of google search and I found out you need to put 11 on the priority instead of 10 like so:
$this->loader->add_action( 'admin_menu' , $plugin_admin, 'make_menu', 11 );
But it doesn't work and the submenu item won't show up instead. I tried putting everything on the wo-plugin-name.php on the root file and just placed simple:
add_action('admin_menu', 'wp_backitup_safemenu', 11 );
function wp_backitup_safemenu() {
add_submenu_page('wp-plugin-name', 'Test', 'Test', 'manage_options', 'manage_options', 'wp_plugin_name_menu_content' );
}
function wp_plugin_name_menu_content() {
echo '<h1>FOO</h1>';
}
And it works like a charm. So what did I do wrong? or is this a boiler plate loader problem?
Related
We use a plugin which add a filter to hide some order meta. Now we want to remove them via child theme instead of editing the plugin file. I tried every answer from here but the action is not removed: https://wordpress.stackexchange.com/questions/240929/how-to-remove-action-from-plugin and Removing action added by a plugin in Wordpress
This is the Plugin function:
public function __construct() {
add_filter( 'woocommerce_order_item_get_formatted_meta_data', [ $this, 'hide_extra_data' ] );
}
I tried with that already, but I'm not sure if this is the right way to do it.
add_action( 'woocommerce_order_item_get_formatted_meta_data', 'remove_my_action', 1 );
function remove_my_action(){
remove_action('woocommerce_order_item_get_formatted_meta_data', 'hide_extra_data', 10, 1);
}
remove_action( 'wp_fwoocommerce_order_item_get_formatted_meta_dataooter', 'hide_extra_data', 1 );
Try like this:
add_action( 'plugins_loaded', 'webearth_remove_plugin_filter' );
function webearth_remove_plugin_filter() {
remove_filter( 'woocommerce_order_item_get_formatted_meta_data',
array('ClassName', 'hide_extra_data'),
10 );
}
Change ClassName with the name of the class used by the plugin, the Class where the constructor is.
I have some php which adds a link in my WordPress dashboard. The problem is that m, whilst it works for one link, it doesn't work for two or more. So how do I add more than one link? Repeating the below php (which works for one link) simply breaks the site.
Php:
add_action( 'admin_menu', 'linked_url' );
function linked_url() {
add_menu_page('linked_url', 'Jobs', 'read', 'my_slug', '', 'dashicons-text', 1);
}
add_action( 'admin_menu' , 'linkedurl_function' );
function linkedurl_function() {
global $menu;
$menu[1][2] = "https://adsler.co.uk/jobs-dashboard/";
}
Also tried:
add_action('admin_menu', 'wpso_custom_links_admin_menu');
function wpso_custom_links_admin_menu() {
global $submenu;
$submenu['index.php'][] = array( 'Link One', 'read', 'https://www.example.com/' );
$submenu['index.php'][] = array( 'Link Two', 'read', 'https://asdf.com/' );
}
This didn't work at all and broke the site immediately.
I'm switching to a new Wordpress plugin I've written myself that will run along side an old plugin on top of the Woocommerce cart. While I'm running the two plugins I want to remove the old plugin's action that calls it's table to display in the users my account page. I will be adding logic after to work out if it's needed, but for now I just need to remove it.
This is how the action is being called in the old plugin.
class WC_Subscriptions {
public static function init() {
// Display Subscriptions on a User's account page
add_action( 'woocommerce_before_my_account', __CLASS__ . '::get_my_subscriptions_template' );
}
}
WC_Subscriptions::init();
So far in my own plugin I have called the following and none of them work.
remove_action( 'woocommerce_before_my_account', array('WC_Subscriptions', 'get_my_subscriptions_template' ) );
// no error but still shows the table
and the last one
remove_action( 'woocommerce_before_my_account', array( WC_Subscriptions::init(), 'get_my_subscriptions_template' ) );
// Fatal error: Class 'WC_Subscriptions' not found in /var/sites/XXXXX on line 45
I have tried changing/adding the $priority from 1, 9, 10, 11 and 99 and that doesn't work either.
It's frustrating as I'm sure it would work if the old plugin was initiated with a new instance so I could do this
global $my_class;
remove_action( 'woocommerce_before_my_account', array( $my_class, 'get_my_subscriptions_template' ) );
Can anyone help?
This always happens, just worked it out.
I needed to hook the remove_action into another action that is called much later.
In the end I did this
class My_new_plugin {
public function __construct() {
add_action( 'wp_head', array($this, 'remove_action_woosubscription_table' ) );
}
public function remove_action_woosubscription_table() {
remove_action( 'woocommerce_before_my_account', array( 'WC_Subscriptions', 'get_my_subscriptions_template' ) );
}
}
You can set third attribute 'priority' as 999
<?php remove_action( $tag, $function_to_remove, $priority ); ?>
I'm trying to create a plugin in wordpress and want to include a JS script in the tags area only when i'm viewing the menu page.
add_action('admin_menu', 'register_custom_menu_page');
function register_custom_menu_page() {
add_menu_page('Home', 'PCPAL', 'manage_options', 'pcpalmain', 'da_controller', '', 99);
}
function DA_controller()
{
add_action('admin_head', 'da_admin_head');
}
function da_admin_head()
{
echo "<script type='text/javascript' src='".plugins_url('js/pcpal.js', __FILE__)."'></script>";
}
This script work if i move the add_action('admin_head', 'da_admin_head'); outside the DA_function function.
you are including JS the wrong way you need to enqueue it -> http://codex.wordpress.org/Function_Reference/wp_enqueue_script
your first add_action goes to the admin_head you need to use admin_init
tutorial about enqueue-ing
http://halfelf.org/2012/jquery-why-u-no-enqueued/
Your Code has a lot off issues.... Let me help you to correct it....
Here is your code
1. add_action('admin_menu', 'register_custom_menu_page');
2. function register_custom_menu_page() {
3. add_menu_page('Home', 'PCPAL', 'manage_options', 'pcpalmain', 'da_controller', '', 4. 99);
5. }
6. function DA_controller()
7. {
8. add_action('admin_head', 'da_admin_head');
9. }
10. function da_admin_head()
{
11. echo "<script type='text/javascript' src='".plugins_url('js/pcpal.js', __FILE__)."'></script>";
}
I have added some numbering so that I can point you where is the problems....
On line 1, you added a function to admin_menu hook. ON line 3, you added a function named da_controller for creating a admin menue.
But one number 6 you just misspelled it.
Well, I do not have enough time for today... But this edited codes must work for now....
add_action('admin_menu', 'register_custom_menu_page');
function register_custom_menu_page() {
add_menu_page('Home', 'PCPAL', 'manage_options', 'pcpalmain', 'da_controller', '', 99);
}
function da_controller()
{
wp_enqueue_script( 'pcpal', 'js_url', array(), '1.0.0', false );
}
I am working on a wordpress plugin and i decided to use OOP instead of functional programming,however;I am receiving this weird error:
Error Message:
Call to undefined function add_menu_page() while am pretty sure everything is working as intended
The Code:
class bdmin{
public function __construct(){
add_action('admin_menu', $this->create_admin_menu());
}
public function create_admin_menu(){
// Create NEW top-level menu
add_menu_page('bdmin Settings', 'bdmin Settings', 'administrator', __FILE__, array( &$this, 'create_admin_page'));
// Register our Settings
add_action( 'admin_init', $this->register_admin_settings() );
}
}
and i initiate the code with $admin = new bdmin();
Best Regards
This should remove the error:
<?php
if(!empty($value['id'])) {
class bdmin{
public function __construct(){
add_action('admin_menu', $this->create_admin_menu());
}
public function create_admin_menu(){
// Create NEW top-level menu
add_menu_page('bdmin Settings', 'bdmin Settings', 'administrator', __FILE__, array( &$this, 'create_admin_page'));
// Register our Settings
add_action( 'admin_init', $this->register_admin_settings() );
}
}
}
?>
To solve this problem all I needed to do is invoke a function named add_action with these parameters.
add_action('admin_menu', array( &$this , 'create_admin_menu'));
Make sure you include the header file at the top so you can make WP function calls
<?php require('{correct_path}/wp-blog-header.php');