Wordpress, adding Admin-Menu Items - php

I'm using this tutorial (http://projects.tareq.co/wp-generator/index.php) and its creator to generate a WordPress CRUD administration. The problem, however, already arises at the point where I'm adding the new menu button. The code says
add_action('init', 'init_conference');
function init_conference () {
include(dirname(__FILE__).'/includes/class-conference-events-admin-menu.php');
$menu_dgvs = new Conference_Events_Admin_Menu();
}
Which seems to call the corresponant __construct in that class:
class Conference_Events_Admin_Menu {
public function __construct() {
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
}
public function admin_menu() {
// menu that does work
add_menu_page(__('Watu PRO', 'watupro'), __('Watu PRO', 'watupro'), WATUPRO_MANAGE_CAPS, "watupro_exams", 'watupro_exams');
// menu that does not work
add_menu_page( __( 'Konferenz', '' ), __( 'Konferenz', '' ), '', 'conference', array( $this, 'plugin_page' ), 'dashicons-groups', null );
add_submenu_page( 'conference', __( 'Konferenz', '' ), __( 'Konferenz', '' ), '', 'conference', array( $this, 'plugin_page' ) );
}
// here's some more code that has nothing to do with the menu
}
The first add_menu_page() is copied over from another plugin, it is shown as it should. The second is copied from the generator. It shows nothing, as if these calls weren't present.
What am I getting wrong here? I can't find a difference between the add_menu call from the other plugin and the generated one from the generator.
Thanks in advance.

Work on a problem for 5 hours, no solution, post to stackoverflow => solution found: it was the capabilities field. 'manage_options' in parameter 3 and everything works.

Related

ajax and woocommerce page loaded twice

add_action( 'admin_menu', array($this, 'admin_menu'), 100 );
public function admin_menu() {
$page = add_submenu_page( 'edit.php?post_type=product', __( 'ajaxmain' ), __( 'ajaxmainagain' ), apply_filters( 'woocommerce_csv_product_role', 'manage_woocommerce' ), 'ajaxmain-product', array( $this, 'output' ) );
}
public function output() {
include( 'ajaxmain.php' );
}
when i use the above code:
ajaxmain.php is getting loaded 2 times.
and then ajaxmain.php doesnt work either.
maybe because there are two elements with the same id now.
actually some elements make ajax request while some elements do not make an ajax request.
thanks for the help
the page is not getting loaded twice now.
i had a base class and an inherited class.
and i tried to instantiate both classes and my add_action( 'admin_menu', array($this, 'admin_menu'), 100 ); was in the constructor of the base class.
hence page was loaded two times.
however the single load of the page is not working with ajax.

How to add multiple links in the WordPress dashboard?

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.

Wordpress does not create multiple widget instances

I thought Wordpress supports creating multiple widget instances in the same location but it looks like it does not in my code. I have created a plugin that shows recent posts. And in it I have created a widget that I want to be able to display multiple times in the same location.
class My_Recent_Posts_Widget extends WP_Widget {
function __construct() {
parent::__construct(
'recent_posts', // Base ID
esc_html__( 'Recent posts', 'text_domain' ), // Name
array( 'description' => esc_html__( 'Display posts via widgets', 'text_domain' ), ) // Args
);
}
And I register the widget like this;
function register_my_recent_posts_widget() {
register_widget( 'My_Recent_Posts_Widget' );
}
add_action( 'widgets_init', 'register_my_recent_posts_widget' );
The problem is if I try to place 2 instances of the same widget in the same location on the Widgets page in the admin the second widget does not show up.
Try this code
class My_Recent_Posts_Widget extends WP_Widget {
public function __construct() {
$widget_ops = array('classname' => 'my_widget_recent_entries', 'description' => esc_html__( "Your most recent Posts.",'text_domain') );
parent::__construct('my-recent-posts', esc_html__('My Recent Posts Widget','text_domain'), $widget_ops);
$this->alt_option_name = 'my_widget_recent_entries';
}

Edit WordPress menu with add_filter()

I am trying to use add_filter() to edit my menu and add two links ('login', 'logout'). However it never shows and I can't figure out what I am doing wrong.
register_nav_menus( array(
'primary' => __( 'Primary Menu', '_s' ),
'quick' => __( 'Quick Links', '_s' ),
) );
add_filter( 'quick', 'add_loginout_link', 10, 2 );
function add_loginout_link( $items, $args ) {
if (is_user_logged_in() && $args->theme_location == 'quick') {
$items .= '<li>Retailer Log Out</li>';
}
elseif (!is_user_logged_in() && $args->theme_location == 'quick') {
$items .= '<li>Retailer Log In</li>';
}
return $items;
}
This is from my functions.php, I wonder if someone knows the solution to my puzzle.
Thanks
Update:
I figured out part of the problem, this above code works fine, however my menu that displays this specific menu is inside the widget and for some reason it never gets the change. This menu is part of footer and its being loaded trough a widget.

WooCommerce Webhooks Custom/Action not saving

Every time you try and set a custom/action topic within webhooks (from WooCommerce > Settings > Webhooks) it would unset it as soon as you update your changes to the webhook. In other words, it will undo your custom topic and return it back to 'Select an option' for the topic dropdown.
Any help at all is appreciated. Thank you very much!
edit: In addition to my comment below, I've also attempted to create my own custom topic via the following filter woocommerce_webhook_topic_hooks, however, it doesn't show within the dropdown list as an option.
The below code runs from functions.php as with any WordPress hook..
Code
function custom_woocommerce_webhook_topics( $topic ) {
$topic['order.refunded'] = array(
'woocommerce_process_shop_order_meta',
'woocommerce_api_edit_order',
'woocommerce_order_edit_status',
'woocommerce_order_status_changed'
);
return $topic;
}
add_filter( 'woocommerce_webhook_topic_hooks', 'custom_woocommerce_webhook_topics', 10, 1 );
edit 2: Added more context
I was having the same issue. Selecting any of the built-in topics worked fine. But selection Action and entering any WooCommerce Subscription actions kept reverting. I had also tried creating a new custom topic in the same file (wp-content/plugins/woocommerce-subscriptions/includes/class-wcs-webhooks.php) that the built-in topics are created, mirroring 1:1 the code of one of the topics that 'stick' (e.g subscription.created) for a new 'subscription.paymentcomplete' topic. It appears in the drop down, but after saving the drop-down reverts to the default 'Selection an option...'.
//wp-content/plugins/woocommerce-subscriptions/includes/class-wcs-webhooks.php
public static function init() {
...
add_action( 'woocommerce_subscription_created_for_order', __CLASS__ . '::add_subscription_created_callback', 10, 2 );
add_action( 'woocommerce_subscription_payment_complete', __CLASS__ . '::add_subscription_payment_complete_callback', 10, );
...
}
public static function add_topics( $topic_hooks, $webhook ) {
if ( 'subscription' == $webhook->get_resource() ) {
$topic_hooks = apply_filters( 'woocommerce_subscriptions_webhook_topics', array(
'subscription.created' => array(
'wcs_api_subscription_created',
'wcs_webhook_subscription_created',
'woocommerce_process_shop_subscription_meta',
),
'subscription.paymentcomplete' => array(
'wcs_webhook_subscription_payment_complete'
'woocommerce_process_shop_subscription_meta',
),
...
), $webhook );
}
return $topic_hooks;
}
public static function add_topics_admin_menu( $topics ) {
$front_end_topics = array(
'subscription.created' => __( ' Subscription Created', 'woocommerce-subscriptions' ),
'subscription.paymentcomplete' => __( ' Subscription Payment Complete', 'woocommerce-subscriptions' ),
...
);
return array_merge( $topics, $front_end_topics );
}
public static function add_subscription_created_callback( $order, $subscription ) {
do_action( 'wcs_webhook_subscription_created', $subscription->id );
}
public static function add_subscription_payment_complete_callback( $order, $subscription ) {
do_action( 'wcs_webhook_subscription_payment_complete', $subscription->id );
}
The solution was:
add_filter( 'woocommerce_valid_webhook_events', __CLASS__ . '::add_event', 10, 1 );
public static function add_event( $events) {
$events[] = 'paymentcomplete';
return $events;
}

Categories