Move widget area to another section in WordPress Customizer and add controls to it - wordpress

I'm trying to move a widget area I have set up. It automatically displays in the Customizer's "Widgets" panel as it should but I would like to move it to another section that I have added.
So far, the below code is the closest I've found but unfortunately it causes an error in debug mode. Also, it only puts it in panel and I would like to put it in a section (which is inside a panel). I tried changing ->panel to ->section with no success.
Is there a way around this or another way of doing it? Once done, is there a way of adding controls to this section, in addition to the widgets?
add_action( 'customize_register', 'my_widget_area_move_widget_area' );
function my_widget_area_move_widget_area () {
global $wp_customize;
$wp_customize->get_section ('sidebar-widgets-my_widget_area')->panel = 'my_panel';
}

In order to change where a widget area is located within the customizer you would want to use the customizer_widgets_section_args filter. With this filter you can customize the customizer options for the widget.
add_filter('customizer_widgets_section_args', 'customizer_custom_widget_area', 10, 3);
function customizer_custom_widget_area($section_args, $section_id, $sidebar_id) {
if( $sidebar_id === 'my_widget_id' ) {
$section_args['panel'] = 'my_panel';
}
return $section_args;
}

Related

Wordpress admin - add item next to "Activate Edit Delete" list

In the plugins section of the admin screen, each plugin generally has a list of a few things you can do, usually "activate, edit, delete". How can I add an item to this list?
In your plugin file just add this code.
// Add settings link on plugin page
function your_plugin_settings_link($links) {
$settings_link = 'Settings';
array_unshift($links, $settings_link);
return $links;
}
$plugin = plugin_basename(__FILE__);
add_filter("plugin_action_links_$plugin", 'your_plugin_settings_link' );
EDIT: OK so i guess you are administrating the sites, and you want your users to report if something goes wrong with any plugin. Here are some of the options.
Use Jquery to add links.
Use the above function and add a loop around the add_filter, and then loop through `$all_plugins = get_plugins();

remove activity feed from Wordpress admin

I am looking for a way to hide activity feed from the dashboard using a function. Does anyone know how to do this? I want to completely remove it. I want to achieve this without a plugin.
You can use remove_meta_box() like;
function remove_dashboard_widgets(){
remove_meta_box('dashboard_activity', 'dashboard', 'normal');
}
add_action('wp_dashboard_setup', 'remove_dashboard_widgets');
add above code to functions.php
Dashboard widgets and other meta boxes can also be removed by using the unset function. You might need to play around with the array keys, or use var_dump() to find the path for the widget you're looking for.
// Removes dashboard activity widget.
function remove_dashboard_activity_widget() {
global $wp_meta_boxes;
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_activity']);
}
// Triggers dashboard widgets removal.
add_action('wp_dashboard_setup', 'remove_dashboard_activity_widget');
Additionally like Hüseyin BABAL mentioned, meta boxes can also be removed like this:
function remove_dashboard_widgets(){
remove_meta_box('dashboard_activity', 'dashboard', 'normal');
}
add_action('wp_dashboard_setup', 'remove_dashboard_widgets');
You'll either have to create a plugin or add a function to your theme functions.php file.
function remove_activity_dashboard_widget() {
remove_meta_box( 'dashboard_activity', 'dashboard', 'side' );
}
// Hook into the 'wp_dashboard_setup' action to register our function
add_action('wp_dashboard_setup', 'remove_activity_dashboard_widget' );
Here is the codex page on the subject:
http://codex.wordpress.org/Dashboard_Widgets_API#Advanced:_Removing_Dashboard_Widgets

how to get is_active_widget on active/runnning page? - wordpress function

I'm looking for a solution to check is_active_widget on particular page.
I got this https://wordpress.stackexchange.com/questions/2302/loading-scripts-only-if-a-particular-shortcode-or-widget-is-present
However the second way of sorich87 still loads the the scripts of a widget on every page where there's no certain active widget on the running/active page. Even when the sidebar of each page has different id & name.
For example:
I have page 'A' (with sidebar 'A1') which consists 'X' active widget and x_scripts in it.
Then when I check on page 'B' (with sidebar 'B1') which doesnt consist 'X' widget, it still loads the x_scripts of 'X' widget. Seems that this is_active_widget function only check for general activated widget, not on the running page.
This is my code:
class X extends WP_Widget
{
function X(){
$widget_ops = array('description' => 'X widget');
$control_ops = array('width' => 200, 'height' => 500);
parent::WP_Widget(false,$name='X-widget',$widget_ops,$control_ops);
if ( is_active_widget(false, false, $this->id_base, true) ){
add_action('wp_head', 'x_styles',0);
add_action( 'wp_footer', 'x_scripts' );
}
}
//rest of functions here
}
How to prevent this?
Yes it does, is_active_widget() get the value from options saved on database. Therefore it will return true for any widgets that is active. The is_active_sidebar() behaves the same, even though the sidebar is not loaded in specific pages, the returned value will always be true as long as the sidebar is registered.
Both functions check the global $wp_registered_widgets, $_wp_sidebars_widgets, $sidebars_widgets which values are generated from register_sidebar() and widgets that has been added to the sidebar.
Unfortunately, there's no workaround for this. Since dynamic_sidebar() is loaded after wp_head(), it's impossible to hook a script or stylesheet conditionally to it. The script or styles or anything you hook to wp_head will always there even the sidebar is loaded or not, unless widget is inactive.
Maybe, logically, a different possibility is there for adding a script on wp_footer() for a widget, in this case, you can set a global variable or define a constant in your widget function. Therefore, the scripts will be loaded only if widget is active.
Well, there's my two cents
I think the easiest solution to this problem would be to simply edit the sidebar widgets themselves. Just use wp_enqueue_script() & wp_enqueue_style() in the plugin files and it'll load them only on the pages where the separate widgets are displayed.
Thanks to AriePutranto, who pushed me in the right direction. The same way you tell if there's an active widget in the widget($instance) function is how you decide whether to include your js/css files.
If you're fine with your includes being in the footer, then this works!
// Create the widget output.
public function widget($args, $instance)
{
if (! empty($instance)) {
add_action('wp_footer', array(&$this, 'enqueueWidgetIncludes'));
$this->printWidget();
}
}
private function enqueueWidgetIncludes() {
//css:
wp_enqueue_style(/*[code removed]*/);
wp_enqueue_script(/*[code removed]*/);
}

How to programatically disable regions on a drupal 7 page?

I am working on a module where i have a page that must have no regions or extra content. A kind of "please wait" page.
How do i diable all extra content (regions menus...etc) ? i think Panels has this ability but i can't find the snippet it uses.
On another hand is it possible for a module to specify a special custom page ? like the maintenance-page for example ?
The page.tpl.php method is not flexible. It is based on a presentation logic. You should use hook_page_alter() for a business logic solution. For example:
function yourmodulename_page_alter(&$page) {
if (current_path() == 'node/add/yourcontenttype') {
unset($page['sidebar_first']);
}
}
Also look at very powefull Context module.
You can create a an extra page.tpl.php specifically for the page where you want to hide the regions. The naming principle is similar to the one for nodes.
Let's say you have a page with the url example.com/content/contact. A template named page--content--contact.tpl.php would serve that page and any page that starts with that url, i.e. the page example.com/content/contact/staff would also use that template (I think).
Check the classes of the body element for clues to what you can name your template, most themes will print that. In my example above, the body element would include the class page-content-contact.
Only thing i can think of is writing checks in your page.tpl.php file to see if you on that "page" your talking about and not printing out the regions/menus, or use a different template. http://drupal.org/node/223440
If you want to do this before the blocks are rendered:
/**
* Implements hook_block_list_alter()
*
* Hides the right sidebar on some pages.
*/
function THEME_NAME_block_list_alter(&$blocks) {
// This condition could be more interesting.
if (current_path() !== 'node/add/yourcontenttype') {
return;
}
// Go through all blocks, and hide those in the 'sidebar_second' region.
foreach ($blocks as $i => $block) {
if ('sidebar_second' === $block->region) {
// Hide this block.
unset($blocks[$i]);
}
}
}
Note: Interestingly, this hook seems to work no matter if you have it in your theme or in a module.
(Please correct me if I'm wrong)

How to hide Edit | View tabs?

Can I hide the
Edit | View
tabs on top of each node ?
I've searched for this option in theme settings (both global and standard theme but I couldn't find it).
I still want to be able my customer to edit / administer content, so I cannot just remove the permission for it.
thanks
here is a very easy solution for you. (Drupal 7)
Open your page.tpl.php in your current template and search for the $tabs variable.
Remove the render code if you want to hide it completely.
If you want to display it only to administrators use this code
<?php if ($tabs and $is_admin): ?>
<div class="tabs">
<?php print render($tabs); ?>
</div>
The above code checks if the user is administrator. If it is it will render the tabs. If not it wont render them.
This really is a presentational thing, not a functionality thing, so it should be done at the theme level.
The problem with overriding theme_menu_local_tasks() is that you override/take a hatchet to the entire local task display, when you really just want to get in there with a scalpel to remove two specific local tasks. So, you need to get a little more specific.
theme_menu_local_tasks() gets the current page's local tasks and passes them to menu_local_tasks(). Here, two theme functions are used:
theme_menu_item_link(), which gets the link markup for the task
theme_menu_local_task(), which gets the <li> element for the task.
So, you can get rid of the View and Edit local tasks in a really robust way by overriding theme_menu_item_link() and theme_menu_local_task() to include your check for them:
function mytheme_menu_item_link($link) {
// Local tasks for view and edit nodes shouldn't be displayed.
if ($link['type'] & MENU_LOCAL_TASK && ($link['path'] === 'node/%/edit' || $link['path'] === 'node/%/view')) {
return '';
}
else {
if (empty($link['localized_options'])) {
$link['localized_options'] = array();
}
return l($link['title'], $link['href'], $link['localized_options']);
}
}
function mytheme_menu_local_task($link, $active = FALSE) {
// Don't return a <li> element if $link is empty
if ($link === '') {
return '';
}
else {
return '<li '. ($active ? 'class="active" ' : '') .'>'. $link ."</li>\n";
}
}
This way, you're relying on the menu router path, not modifying the menu router item, and achieving the result you want with minimal changes to core functionality or theming.
On the module side, you could do something that decouples the Edit's menu entry from the local tasks for the node:
function custom_menu_alter(&$items) {
$items['node/%node/edit']['type'] = MENU_CALLBACK;
}
The edit path is still there, but now it is not associated with the View tab. This includes the edit page itself--no View tab there.
there is a module for that: tab tamer allows to hide or disable tabs and rename them as well.
I use the following in template.php by theme (which is perhaps a little hacky, I feel I should be considering unsetting $tabs instead):
function THEME_NAME_menu_local_tasks() {
return '';
}
Or you could ommit:
if ($tabs) echo $tabs;
from your page.tpl.php...
View and Edit are functional features. They have a reason for being there.
The best way to "remove" them, is to "remove" that functionality alltogether. After all: why remove the interface of a piece of functionality, but not the functionality itself?
Besides, simply not printing the tabs, does not remove the url endpoints. In other words: if you don't print the edit tab, people can still access the edit page.
Again: best is to remove that functionality: The fact that you don't want the edit tab, sounds as if you don't want the edit functionality for certain users.
If so, then just remove that permission for that role. That is all. The tabs will be gone.
If, however, you simply wish to display these tabs differently, Drupal is your friends. As you may have noticed, they are called local tasks and not tabs. That is because the theme decides how to render them: The theme is the thing that decides to show them as tabs.
Simply override the theme_menu_local_tasks() to create your own HTML for the "local-tasks". And in your page-tpl, simply move the $tabs variable around to a place, where you want them.
But again: Don't try to change the behavior of the app, by removing interface-elements. That is not the right thing to do: you should change the behavior, in order to change the behavior :)
For all the people stumbling upon this question while looking for a D7 solution: As stated on https://drupal.stackexchange.com/a/77964/15055 it's hook_menu_local_tasks_alter()
/**
* Implements hook_menu_local_tasks_alter() to unset unwanted tabs
*/
function MYMODULE_menu_local_tasks_alter(&$data) {
foreach ($data['tabs'][0]['output'] as $key => $value) {
if ($value['#link']['path'] == 'node/%/view') {
unset($data['tabs'][0]['output'][$key]);
}
}
}
This is not the answer to the question of what the author asked. But somehow it might be useful for others user who facing the similar problem with me. Please let me know if this is not suitable to put in here.
I get the answer from #grayside and modified a bit to hide the view | edit tab from node based on the content type I want.
function MYMODULE_menu_alter(&$items) {
$items['node/%node/view']['access callback'] = 'MYMODULE_disable_node_view';
$items['node/%node/view']['access arguments'] = array(1);
}
function MYMODULE_disable_node_view($node){
if($node->type == 'product'){
return false;
}
}
product is the machine name of my content type, I don't want anywant to access it including root user.
The simplest solution to hide the tabs is to add this class in your theme css
.tabs{ display:none;}
Thanks for the last answer. But be aware of this detail: if you try it as-is it cannot work: literals should be just rounded with " or ', not both of them altogether. So it should be:
/**
* Implements hook_menu_local_tasks_alter() to unset unwanted tabs
*/
function MYMODULE_menu_local_tasks_alter(&$data) {
foreach ($data['tabs'][0]['output'] as $key => $value) {
if ($value['#link']['path'] == "node/%/view") {
unset($data['tabs'][0]['output'][$key]);
}
}
}
Once taken care of it, it works :)
D8 solution: If you want to hide all "local" tabs on certain pages, remember that "Tabs" is listed in the block library: find it in the "Content" region and exclude by content type, page URL or user role.

Resources