How can I add a Submenu to the WordPress Admin Bar - php

I need to add a dropdown menu to Wordpress in the admin bar to include multiple links. What is the best solution?

I was looking for the answer to this question for a while and couldn't find the solution on here so I thought this would help! I found a great blog post and the perfect solution to my question:
http://davidwalsh.name/add-submenu-wordpress-admin-bar
Like adding functionality to your theme and other admin area, the directives will go in your theme's functions.php file. The code itself should be self explanatory:
function create_dwb_menu() {
global $wp_admin_bar;
$menu_id = 'dwb';
$wp_admin_bar->add_menu(array('id' => $menu_id, 'title' => __('DWB'), 'href' => '/'));
$wp_admin_bar->add_menu(array('parent' => $menu_id, 'title' => __('Homepage'), 'id' => 'dwb-home', 'href' => '/', 'meta' => array('target' => '_blank')));
$wp_admin_bar->add_menu(array('parent' => $menu_id, 'title' => __('Drafts'), 'id' => 'dwb-drafts', 'href' => 'edit.php?post_status=draft&post_type=post'));
$wp_admin_bar->add_menu(array('parent' => $menu_id, 'title' => __('Pending Comments'), 'id' => 'dwb-pending', 'href' => 'edit-comments.php?comment_status=moderated'));
}
add_action('admin_bar_menu', 'create_dwb_menu', 2000);
Setting an id on the parent menu item allows you to use the parent key for submenu items; the rest of the keys are easy to figure out. With the menu created, you simply need to add the WordPress hook and specificity to add it!

Related

How to access an array variable from another PHP file in Wordpress?

I'm adding some custom code to dynamically display Social Network icons in a Wordpress theme, by using the Customize API.
The only problem that prevents it to work as expected is that I need to find a way to access an array variable from another file, not only from the one where it is declared.
First, I've added some code to functions.php, to create some text fields in the Theme Customizer, as follows:
function my_customizer_options($wp_customize) {
// Section
$wp_customize -> add_section ( 'my_social', array(
'title' => __('Social', 'my-theme'),
'description' => __('Social Network profiles', 'my-theme'),
'priority' => 20,
)
);
// Social Networks list
$socialnetworks = array(
// Social Network => Icon Name
'facebook' => 'fa-facebook-f',
'linkedin' => 'fa-linkedin-in',
'instagram' => 'fa-instagram',
'twitter' => 'fa-twitter',
);
// Settings and controls
foreach ($socialnetworks as $key => $value) {
$wp_customize -> add_setting ( $key, array( 'default' => '' ) );
$wp_customize -> add_control ( $key, array(
'type' => 'url',
'label' => __(ucfirst($key), 'my-theme'),
'section' => 'my_social',
));
}
add_action( 'customize_register', 'my_customizer_options' );
This lets me add items to the $socialnetworks array at will, and automatically generates the code required to add settings and controls, without having to do it manually by writing down the same bits of code again and again. I.e., if I need to add more fields for more social networks, I can simply add entries to the $socialnetworks array without duplicating the whole // Settings and controls code.
Until now, it works perfectly, I now have four text fields in the customizer where I can insert URLs of social network profiles. Now I need to access these values from inside the theme template files.
This is the portion of my header.php file where I want to display the icons that link to the respective Social Network profiles:
/*
// Social Networks list
$socialnetworks = array(
// Social Network => Icon Name
'facebook' => 'fa-facebook-f',
'linkedin' => 'fa-linkedin-in',
'instagram' => 'fa-instagram',
'twitter' => 'fa-twitter',
);
*/
// Social Icon
foreach ($my_socialnetworks as $key => $value) {
if ( get_theme_mod($key, '')) { ?>
<i class="fab <?php echo $value ?>"></i>
<?php };
}
This is supposed to retrieve the values inserted in the Customizer with the get_theme_mod() WP builtin functions, and dinamically add the icons and relative links for each inserted URL.
The problem is that in order to make it work, I need to uncomment the $socialnetworks array, and basically redefine it also from inside header.php, while I want instead to access it from functions.php.
A possible problem could be that $socialnetworks is defined inside the my_customizer_options() function; I tried to move it outside, before the function itself, but in this way it even breaks the code in functions.php without solving anything.
I need the correct place and the correct way to define $socialnetworks once and being able to access it from any file of my theme, how should I modify my code in order to achieve this?
As suggested by an user in the comments, I put the array in a function, now I can access the array from anywhere by calling the function.
Here is the modified code:
functions.php:
// Social Networks list
function my_socialnetworks() {
$socialnetworks = array(
// Social Network => Icon Name
'facebook' => 'fa-facebook-f',
'linkedin' => 'fa-linkedin-in',
'instagram' => 'fa-instagram',
'twitter' => 'fa-twitter',
);
return $my_socialnetworks;
}
function my_customizer_options($wp_customize) {
// Section
$wp_customize -> add_section ( 'my_social', array(
'title' => __('Social', 'my-theme'),
'description' => __('Social Network profiles', 'my-theme'),
'priority' => 20,
)
);
$my_socialnetworks = my_socialnetworks();
// Settings and controls
foreach ($socialnetworks as $key => $value) {
$wp_customize -> add_setting ( $key, array( 'default' => '' ) );
$wp_customize -> add_control ( $key, array(
'type' => 'url',
'label' => __(ucfirst($key), 'my-theme'),
'section' => 'my_social',
));
}
add_action( 'customize_register', 'my_customizer_options' );
and header.php:
$my_socialnetworks = my_socialnetworks();
// Social Icon
foreach ($my_socialnetworks as $key => $value) {
if ( get_theme_mod($key, '')) { ?>
<i class="fab <?php echo $value ?>"></i>
<?php };
}

How can I add a link to the Wordpress Admin Bar that runs my function onclick?

I am wanting to add a link to the Wordpress admin bar that will eventually purge all cache. At the moment I am trying to add the link to the admin bar but seem to be having problems.
I have followed a few methods I had found on Google, but for some reason I couldn't get them to work. (I don't know if this is due to the examples being outdated with the version of Wordpress?)
Here is what I am using so far:
function time_to_purge($wp_admin_bar) {
$args = array(
'id' => 'DaylesPurge',
'title' => 'Purge Cache',
'href' => '#',
'meta' => array(
'class' => 'DaylesPurge'
)
);
$wp_admin_bar->add_node($args);
}
add_action('admin_bar_menu', 'time_to_purge', 50);
function PurgeConfirmation() {
document.getElementById("PurgeConfirmation").innerHTML = "Are you sure you
wish to purge the cash?";
}
[EDIT]
Never-mind, this works perfectly. It was a matter of a simple spelling mistake in my code that was preventing it from displaying.
function time_to_purge($wp_admin_bar) {
$args = array(
'id' => 'DaylesPurge',
'title' => 'Purge Cache',
'href' => '#',
'meta' => array(
'class' => 'DaylesPurge'
)
);
$wp_admin_bar->add_node($args);
}
add_action('admin_bar_menu', 'time_to_purge', 50);
function PurgeConfirmation() {
document.getElementById("PurgeConfirmation").innerHTML = "Are you sure you
wish to purge the cash?";
}

Change subpanel order in SugarCRM 7

How can one change the order on each subpanel either by code or through the GUI?
In Sugar 6 the user could change the order simply by dragging and dropping the subpanels under each module.
From what I can see this is not possible in 7.x.
I have tried to change
'order' => 1
in
custom/Extension/modules/Opportunities/Ext/Layoutdefs/some_file.php
with no luck at all..
UPDATE:
As UTAlan stated,
this will become part of the stock functionality of Sugar starting in version 7.5.0: https://web.sugarcrm.com/support/issues/66590
Until then, here is the reason and the solution:
The 'order' => 1, does not seem to work on Sugar 7 at the moment.
Solution
Copy the file
modules/Opportunities/clients/base/layouts/subpanels/subpanels.php
to
custom/modules/Opportunities/clients/base/layouts/subpanels/subpanels.php
Now, add your custom subpanel definition to the beginning of the array or in any order you desire.
My example looks like this now:
$viewdefs['Opportunities']['base']['layout']['subpanels'] = array(
'components' => array(
// This is my custom module
array(
'layout' => 'subpanel',
'label' => 'LBL_OPPORTUNITIES_FOOBAR_TITLE',
'context' => array(
'link' => 'opportunities_foobar_1',
),
),
.. // Code ommited
array(
'layout' => 'subpanel',
'label' => 'LBL_EMAILS_SUBPANEL_TITLE',
'context' => array (
'link' => 'archived_emails',
),
),
),
'type' => 'subpanels',
'span' => 12,
);
Long Answer:
Why is 'order' => 1 not working anymore?
Inside include/MetaDataManager/MetaDataConverter.php:327:
public function toLegacySubpanelLayoutDefs(array $layoutDefs, SugarBean $bean) {
..
foreach ($layoutDefs as $order => $def) {
..
$return[$def['context']['link']] = array(
'order' => $order,
..
}
The order that is being rendered in the view is based on which order each bean-name is inserted inside the 'components'-key inside this file:
modules/Opportunities/clients/base/layouts/subpanels/subpanels.php
Core modules are hard-coded inside the subpanel file for Opportunities.
This will become part of the stock functionality of Sugar starting in version 7.5.0: https://web.sugarcrm.com/support/issues/66590

add custom module under a menu stream

I am creating my own module and one of its requirement is to have it located in a certain menu. My problem is that the menu is generated using a plugin PyroStreams.
So first things first, I downloaded a copy of the sample module on the Github, then have it place in addons/default/modules/. I refreshed my PyroCMS Admin -> Add-ons -> Modules and see the sample module in there. As stated on the detail.php of this sample module
public function info()
{
return array(
'name' => array(
'en' => 'Test Modules'
),
'description' => array(
'en' => 'My custom module.'
),
'frontend' => FALSE,
'backend' => TRUE,
'menu' => 'content', // You can also place modules in their top level menu. For example try: 'menu' => 'Sample',
'sections' => array(
'items' => array(
'name' => 'Test', // These are translated from your language file
'uri' => 'admin/sample',
'shortcuts' => array(
'create' => array(
'name' => 'sample:create',
'uri' => 'admin/sample/create',
'class' => 'add'
)
)
)
)
);
}
It should appear on the Content menu which is correct. Now, I can't find anything on docs that states instructions on properly mapping the menu for custom modules, so out of initiative I tried to change the value for menu => "content" to menu => "Test Stream" but that didn't work. As shown in the screenshot below, that is where I wanted to place my custom module, under the menu "Test Stream". What am I missing?
Add this method to your detail.php file:
public function admin_menu(&$menu)
{
// Create main menu item
add_admin_menu_place('lang:module:title', 9); // 9 is the placement order of your menu item, it would be after profile
// Create sub-menu
$menu['lang:module:title']['lang:module:submeu1'] = 'admin/add';
$menu['lang:module:title']['lang:module:submeu2'] = 'admin/edit';
}
also set the 'menu'=> false, at your info() method.

Open a thickbox-style Lightbox from WP Admin Bar

Folks,
I'm currently setting up a WP install. In order for the client to access a custom help-document, I added an additional dropdown menu in the admin-bar as a WP plugin via the following php-file:
<?php
function pub_admin_bar_init() {
if (!is_super_admin() || !is_admin_bar_showing() )
return;
add_action('admin_bar_menu', 'pub_admin_bar_links', 500);
}
add_action('admin_bar_init', 'pub_admin_bar_init');
function pub_admin_bar_links() {
global $wp_admin_bar;
$links = array(
'Chapter 1' => 'http://manual.domain.com/index1.html',
'Chapter 2' => 'http://manual.domain.com/index2.html',
'Chapter 3' => 'http://manual.domain.com/index3.html',
'Chapter 4' => 'http://manual.domain.com/index4.html',
'Chapter ...' => 'http://manual.domain.com/index5.html',
);
$wp_admin_bar->add_menu( array(
'title' => 'Help-Document',
'href' => false,
'id' => 'pub_links',
'href' => false
));
foreach ($links as $label => $url) {
$wp_admin_bar->add_menu( array(
'title' => $label,
'href' => $url,
'parent' => 'pub_links',
'meta' => array('target' => '_blank')
));
}
}
?>
The dropdown works fine and the referenced html-files located in the subdomain of the same domain get called in a new tab.
However, in order to have everything neatly arranged within the WP admin, I'd like to call the menu items in the new admin-bar dropdown via an iframe Lightbox.
I managed to set this up in the Dashboard intro section using the built-in thickbox with the following syntax:
<a style="text-decoration:none;"
href="http://codex.wordpress.org/First_Steps_With_WordPress?
keepThis=true&TB_iframe=true&height=800&width=1200" class="thickbox"
title="sometitle">First Steps with WordPress</a>
This lets me call the html help-files (or any other url for that matter) as an iframe in a thickbox-style overlay.
Now my actual question:
Could someone point me to how I could make the links in the admin-bar dropdown ('Chapter 1' => 'http://manual.domain.com/index1.html', ...) as thickbox-style overlays instead of target=_blank?
Help very much appreciated. Thanks a lot!
'meta' => array('target' => '_blank'),
You forget of ","

Categories