Wordpress - If Menu plugin - Custom visibilty rules in navigation menu bar - associative-array

I am using https://wordpress.org/plugins/if-menu/ to show/hide certain menu items in menu bar list only if device is mobile.
The problem is it recognizes even tablets as mobiles.
To tackle this situation, I have to create a new rule which I am unable to do.
An example of creating new custom visibility rule:
add_filter( 'if_menu_conditions', 'wpb_new_menu_conditions' );
function wpb_new_menu_conditions( $conditions ) {
$conditions[] = array(
'name' => 'If it is Custom Post Type archive', // name of the condition
'condition' => function($item) { // callback - must return TRUE or FALSE
return is_post_type_archive();
}
);
return $conditions;
}
This code needs to be added in function.php of child theme.
Result of the above code: here
I want to edit the above code for this condition:
window width <= 480 or window height <= 480
(i.e. if device's window width or height is less or equal to 480)
Can someone help me with the code? It would be very helpful.
[NOTE: Using #media is the easiest solution, still I want to add the above mentioned condition in the If Menu Plugin.]
Thank You.

Related

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

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

Wordpress , display resized image in theme with PHP

I'm coding a shortcode that accept one media ID as an attribute. This shortcode should then display the image at an existing size registered with add_image_size. Problem : my image is not displayed at the correct size.
Explanations :
My media image has been uploaded in the WP library. The original file size is huge (1227x924). The media has the ID 294.
I registered an image size in functions.php :
function my_after_setup_theme(){
add_image_size('my-image-size', 210, 136, true);
}
add_action('after_setup_theme', 'my_after_setup_theme');
I insert my shortcode in one of my pages : [my_shortcode imageid="294"]
My shortcode code is :
function my_shortcode_func($atts)
{
$a = shortcode_atts(array(
'imageid' => 0,
), $atts);
if ($a['imageid'] == 0) {
// default placeholder image
$img = 'img src="http://placehold.it/210x136"/>';
} else {
// get resized (NOT WORKING !)
$img = wp_get_attachment_image($a['imageid'], 'my-image-size');
}
return $img;
}
add_shortcode('my_shortcode', 'my_shortcode_func');
I expect the original image to be resized at the correct size (ie : 210x136) in a new thumbnail file. Instead, the $img displays the original image (1227x924) , displayed at an intermediate size via the width and height attributes of the HTML <img> tag :
What am I doing wrong ?
thanks for help.
Did you register the thumbnail size after uploading the media? If so, you will need to use a plugin to regenerate the thumbnails.
Additionally, I don't believe you need to wrap thumbnail size declarations as you have done, I have the following;
if (function_exists('add_theme_support'))
{
add_theme_support('post-thumbnails'); // Featured image support for posts - you may not need this bit.
add_image_size('large', 700, '', true); // Large Thumbnail - This is the bit you would need
}
So it would be worth checking if your thumbnails are actually be generated to start with, as I have never done it the way you have defined in your question.

[Wordpress][Nextgen Gallery] How reorder admin menu position

Is it possible to move Nextgen Gallery icon under Media in Admin Menu?
I know how to reorder Admin menu items:
function new_menu_order($new_order) {
if (!$new_order) return true;
return array(
'index.php',
'edit.php',
'edit.php?post_type=page',
'upload.php', //Media icon
'admin.php?page=nextgen-gallery', // Nextgen Gallery link
'edit.php?post_type=slide'
);
}
add_filter('custom_menu_order', 'new_menu_order');
add_filter('menu_order', 'new_menu_order');
But adding "admin.php?page=nextgen-gallery" in this code doesn't help. Gallery is still last position.
Ok, I've found a kind of solution: I had to edit admin.php in plugin admin folder using a add_menu_page
It seems due to the priority
Have you tried by setting lower priority to the filters? You can try by settings priority as 99999 as below.
add_filter('custom_menu_order', 'new_menu_order', 99999);
add_filter('menu_order', 'new_menu_order', 99999);

Remove/ trim a number of links from drupal navigation

How would I trim/ unset a certain number of menu from drupal navigation menu to avoid lengthy menus for a specific page? Say I want to remove the last 20 menu items from navigation menu.
Any advice would be very much appreciated.
Thanks
You could do it at the theme layer easy enough by theming your menu.
See theme_links()
http://api.drupal.org/api/function/theme_links/6
Perhaps something like:
function mytheme_links($links, $attributes = array('class' => 'links')) {
$length = variable_get('mytheme_menu_length', 22);
$links = array_slice($links, 0, $length);
return theme_links($links, $attributes);
}
There are other ways also. You could hide some with CSS or build the $links array yourself but the menu api doesn't really have an interface for this.
See menu_navigation_links().
http://api.drupal.org/api/function/menu_navigation_links/6

Drupal: automatically add menu items when new nodes are added

can I automatically add a menu item when I add a node to the page in Drupal?
In other words, can I associate a menu parent with a node content-type, and then automatically add the children if new nodes are added ?
thanks
You can do it with Rules on Drupal 7. This module: http://drupal.org/project/menu_rules adds some actions to rules. One of them is to create a menu item for a node. You select:
Event: Create a node | Update a node
Condition: Content type is "your content type"
Action: Update a menu item for node (there is a checkbox to create the menu item if it doesnt exist)
There's also the Menu Position module that allows to put content under specific menu entries, depending on their content type, their language and taxonomy. It also has a small API to add other criteria.
Yes.
I am sure there is a module do to something like that, but you could also create your own.
There are two ways you could go about it.
You could use hook_menu() to query for the items you want and return the correct menu structure. You would need to also make sure the menu cache is rebuilt on a node save using hook_nodeapi().
See henricks' comments below about why this is a bad idea
Alternitivly you could use hook_nodeapi() to add custom menu items with menu_link_save().
Edit
hook_menu should return an array of menu items, often these are pretty static however there is nothing wrong with these arrays being dynamically generated.
So you can query the node table to get a list of nodes you want, loop through these items and dynamically create an array which contains the correct menu items.
very roughly:
function example_menu() {
$result = db_query('select * from node where ...'); // put in your own select items and where clause
$menu = array();
while ($row = db_fetch_object($result)) {
$menu['my_path/' . $row->nid;] = array(
// See hook menu docs for what to put here.
);
}
return $menu;
}
You should take a look at the Auto Menu module - while the Drupal 6 version is still a dev release, it might cover your needs. If not, you can take it as an example of how to use menu_link_save() to create your own solution.
I would also go for a menu_link_save() call. Together with the Rules module, you can set up an action whenever a new node is saved, to create an appropriate menu item automatically.
You might want to have a look at the tutorial I wrote some time ago, which deals with programatically creating menu items using menu_link_save() and Rules: http://jan.tomka.name/blog/programmatically-creating-menu-items-drupal
Here is case where you can do this....
A node campaign creating menu item 'CAMPAIGN 001' when it is created. Using default_menu_link
Now another content type, 'Sub Campaign' creating a node, using campaign as EntityRef so its menu item should be under the Menu Item of campaign created earlier.
function mymodule_node_insert($node) {
if ($node->type == 'sub-campaign') {
if (isset($node->field_reference_campaign['und'][0]['target_id'])) {
$campaign_node_id = $node->field_photo_album_campaign['und'][0]['target_id'];
$campaign_loaded = node_load($campaign_node_id);
// Get menu link id for the campaign node.
$campaign_node_id_mlid = custom_node_mlid($campaign_node_id);
$campaign_loaded_title = strtolower(str_replace(' ', "-", $campaign_loaded->title));
$campaign_loaded_title_link_path = 'campaign/' . $campaign_loaded_title . '/photo-albums';
//I will query if it exist or not, if not then will create a sub menu item.
$link_exist = db_query("SELECT * FROM {menu_links} WHERE link_path = :link_path", array(':link_path' => $campaign_loaded_title_link_path))->fetchField();
dsm($link_exist);
if (!$link_exist) {
// Create menu item under campaign.
custom_create_menu_item($campaign_loaded_title_link_path, 'photo-albums', $campaign_node_id_mlid);
//watchdog('glue_site - Menu Item', 'Link Created');
}
else {
//dsm('Link Exist.');
watchdog('glue_site - Menu Item', 'Link Already Exist');
}
}
}
if ($node->type == 'campaign') {
}
}
Then a custom function to create menu item
function custom_create_menu_item($campaign_loaded_title_link_path, $type, $plid) {
switch ($type) {
case 'photo-albums':
$item = array(
'link_path' => $campaign_loaded_title_link_path,
// If changing the title here, change it in template.php as well.
'link_title' => 'Sub Campaign',
'menu_name' => 'menu-campaign-menu', // Menu machine name, for example: main-menu
'weight' => 0,
'plid' => $plid, // Parent menu item, 0 if menu item is on top level
'module' => 'menu',
'router_path' => 'campaign/%/sub-campaign',
'customized' => '1',
);
menu_link_save($item);
menu_cache_clear_all();
watchdog('glue_site - Menu Item', 'Link Created');
break;
}
}
To get the mlid of parent node. Campaign node...
function custom_node_mlid($nid) {
// Require menu node module.
$arr = menu_node_get_links($nid);
$mlid = array_keys($arr);
return $mlid[0];
}
For this you need menu_node
This is a simple problem that unfortunately the Drupal community has decided it wants to make complicated. Forget about all the hacky solutions with rules and hooks. There are two modules, depending on whether you're on Drupal 6 or Drupal 7, that solve the problem very elegantly. I advise against actually creating menu entries. Instead the two modules below dynamically render the nodes in the menu, so that your menu editor doesn't get filled with thousands of nodes. Then, for example, if you decide you want all the blog posts to be moved from [Our Blog] to [About Us]->[News] it's just a mater of changing one setting. No updating thousands of nodes.
D6 Menu Trails
D7 Menu Position
It looks like there's a Drupal module that does this: Auto Menu. Some more details about this module (from its project page):
The Auto Menu module automatically generates menu entries on node creation/edition. Parent menu item can be specified on a per content type basis.
This module acts when the menu section of a node is left empty only. So, users can still organize menus manually. Moreover, default setting for content types is to not create menu items automatically.
Menu Views is an interesting module for Drupal 7 to automatically generate menu links. It allows you to use the power of Views to create menu links and can be used out-of-the-box in combination with modules such as Superfish and Nice Menus.
(PS: my reputation is not high enough to provide more than two links, therefore I have marked the other modules bold instead of providing hyperlinks)

Resources