How to add multiple links in the WordPress dashboard? - php

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.

Related

PHP include ability in WP post/pages via shortcode

I have a few .html and .php pages in my custom wordpress plugin directory/files that I'm using to output styles JSON/jQuery data basically.
I am wondering how I could essentially wrap these modules in a short code and insert that short code in my Wordpress posts or pages via the Wordpress wysiwyg editor. i.e. [module 1]. I do not want to do this in the theme's files or /functions.php I want to add this functionality from my plugin files, any thoughts?
So, like a php include in the form of a wordpress short code, that works in Wordpress pages.
Here is what I'm trying; within my main plugin php file:
function my_form_shortcode() {
include dirname( __FILE__ ) . 'https://absolute.path.com/wp-content/plugins/my-plugin-v4/assets/files/2019/results/index.php';
}
add_shortcode( 'my_form_shortcode', 'my_form_shortcode' );
Within my Wordpress page, I do: (although, does not display/work)
[my_form_shortcode]
You can add your shortcode function the same way you would in your theme
add shortcode function in plugin
Simplest example of a shortcode tag using the API: [footag foo="bar"]
function footag_func( $atts ) {
return "foo = {$atts['foo']}";
}
add_shortcode( 'footag', 'footag_func' );
Example with nice attribute defaults: [bartag foo="bar"]
function bartag_func( $atts ) {
$atts = shortcode_atts( array(
'foo' => 'no foo',
'baz' => 'default baz'
), $atts, 'bartag' );
return "foo = {$atts['foo']}";
}
add_shortcode( 'bartag', 'bartag_func' );
Example with enclosed content: [baztag]content[/baztag]
function baztag_func( $atts, $content = "" ) {
return "content = $content";
}
add_shortcode( 'baztag', 'baztag_func' );
If your plugin is designed as a class write as follows:
class MyPlugin {
public static function baztag_func( $atts, $content = "" ) {
return "content = $content";
}
}
add_shortcode( 'baztag', array( 'MyPlugin', 'baztag_func' ) );

Wordpress, adding Admin-Menu Items

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.

Wordpress Plugin Boilerplate & Submenu Priority

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?

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;
}

How to add a virtual page (created by a plugin) to the menu builder

i have been making a plugin which makes it possible to show a schedule in wordpress, but until now i did not find a good way to show the page in wordress.
until now what i did was check if certain querystring variable was set.
but now i found a way to add virtual pages to wordpress:
https://coderwall.com/p/fwea7g/create-wordpress-virtual-page-on-the-fly
i renamed it to virtualClass and used it like this:
new \helpers\VirtualPage(array(
'slug' => '1002',
'post_title' => 'Fake Page Title',
'post content' => 'This is the fake page content'
));
to tryout, i went to the page: mywordpress.site?page_id=1002
this did work, but when i went to the backend in the menu builder, i wont see this page, i want it to be shown there so my users can add the page to their website.
Is there good tutorial on how to do this? is there a hook i can use to add a section to the menu builder?
Like in the image below: add it to either place of the red arrow
Instead of using a virtual page, you should create a page with your plugin's activation and then overwrite that page's content. You will also want to delete
the page on deactivation. In case you want to protect that page from user deletion, I have included that code as well, but it uses and exit; so it's a bit harsh.
function pluginxyz_activate(){
$title = 'Whatever You Want To Call It';
$new_id = wp_insert_post(array(
'post_title'=>$title,
'post_status'=>'publish',
'post_type'=>'page'
));
update_option('pluginxyz_page_id',$new_id);
}
function pluginxyz_deactivate(){
$old_id = get_option('pluginxyz_page_id');
if($old_id)wp_delete_post($old_id,true);
delete_option('pluginxyz_page_id');
}
function pluginxyz_protect_page($post_id){
$old_id = get_option('pluginxyz_page_id');
if($post_id == $old_id)exit('You can not delete this page');
}
function pluginxyz_use_content($content){
$old_id = get_option('pluginxyz_page_id');
global $post;
if($post->ID == $old_id){
//que up your content
} else {
return $content;
}
}
register_activation_hook(__FILE__,'pluginxyz_activate');
register_deactivation_hook( __FILE__, 'pluginxyz_deactivate');
add_action('wp_trash_post', 'pluginxyz_protect_page', 10, 1);
add_action('before_delete_post', 'pluginxyz_protect_page', 10, 1);
add_filter( 'the_content', 'pluginxyz_use_content' );

Categories