WordPress rowactions for WP_List_Table ColSpan - wordpress

I'm currently using the WP_List_Table for displaying my data for my plugin and have rowactions tied to the first column (as show in multiple tutorials). I have approximately 9 columns in my table and would like for the actions row to span across all of the columns (or at least more than one) in the table so that when the browser is resized, the actions aren't wrapped in a single cell. Does anyone know how to accomplish this?

You can override the function called single_row() and write your row with colspan.
ie, Define this function in your child class which extends WP_List_Table.
public function single_row( $item ) {
static $row_class = '';
$row_class = ( $row_class == '' ? ' class="alternate"' : '' );
echo '<tr' . $row_class . '>';
$this->single_row_columns( $item );
echo '</tr>';
// You can add a new row here with custom styles and attributes or edit the above one.
}

Related

Why WP_Screeen doesn't show all options with admin_body_class

I was trying to add a specific class to the admin section. I created a new top-level menu page and now I want to add a specific class to this top-level menu and its submenu items.
What I have tried so far is that I used WP_Screen to get the data of the current screen.
If I check the submenu page and I did var_dump(get_current_screen()). It shows every detail.
But now the case is that If I use get_current_screen() with admin_body_class why it doesn't work.
As far as I know, the hook is early called before the submenu page details are loaded.
Now I want to ask, how it is possible to check if the submenu has a specific parent base,
Here is the code that I tried so far.
add_filter('admin_body_class', 'tw_admin_body_class');
function tw_admin_body_class( $classes ) {
$screen = get_current_screen();
var_dump($screen);
if ( $screen->parent_base == 'tw-top' ) {
return $classes . ' raashid';
}
}
But parent_base is set to null. Any idea how to add class to submenu pages if it is under the specific yop-level menu.
It worked by using the Id option.
Here is the working option.
add_filter( 'admin_body_class', 'my_admin_body_class' );
function my_admin_body_class( $classes ) {
$screen = get_current_screen();
$classes .= ' raashid';
if ( get_plugin_page_hook( 'tw-top', '' ) === $screen->id ) {
return $classes;
}
}

Hide elements based on a specific element value

I have a woocommerce store and I’m importing custom fields like “color”, “length”, etc (around 10 fields) but not all product have values for these fields. Is there a way to hide the whole element of the value is empty or null. On the front end the elements show like this:
Length: 10cm
Height: 20cm
So basically if there is no length for a specific product I want to hide it.
Link to see: (check product extra details. In this example I only want to show the diameter and hide the rest since they are empty)
https://qadeemarabia.com/product/3d-bird-cookie-jar-off-white-matt/
You must use the woocommerce_display_product_attributes filter to delete empty items. My hypothesis for the empty condition is that the value is zero. You can change the condition according to what you need:
add_filter( 'woocommerce_display_product_attributes', 'ywp_hide_ptoperties_with_empty_values', 10, 2 );
function ywp_hide_ptoperties_with_empty_values( $attributes, $product ) {
// Loop to check values
foreach( $attributes as $attr ) {
$new_attr = array();
// Your condition for check value is empty
// You can use 'trim' etc.
$value = floatval( $attr['value'] );
// Check value is 0 here means value is empty
// and empty value skipped
if( $value != 0 ) {
$new_attr[] = $attr;
}
}
return $new_attr;
}
This code goes into the functions.php file of your active theme/child theme. Tested and works.

Get number of siblings for a wp_nav_menu element

I am using a custom Walker function to customise the display of a menu in Wordpress.
To go one step forward I would like to have a different HTML markup for submenus with more than X elements.
Is there a way I can check the number of elements for a given item within the start_el function of the Walker Walker_Nav_Menu?
It's not the simplest thing using the Walker class, so I thought of a solution using DOMDocument to parse the menu HTML and count the submenus children after the menu has been built (using the wp_nav_menu_{$menu->slug}_items filter):
add_filter( 'wp_nav_menu_{MENU_SLUG}_items', function( $items, $args ) {
$dom = new DOMDocument();
$dom->loadHTML($items);
// get the .sub-menu elements
$submenus = $dom->getElementsByTagName('ul');
// for each one if it has children add a class
// like has-{num}-children
foreach ($submenus as $ul) {
if($ul->hasChildNodes()) {
// number of child nodes divided by 2
// to exclude the text nodes
$numChildren = $ul->childNodes->length / 2;
$class = "has-$numChildren-children";
$ul->setAttribute(
'class', $ul->getAttribute('class') . ' ' . $class
);
}
}
// save the changes
$html = $dom->saveHtml($dom->documentElement);
// remove html and body tags added by $dom->loadHTML
$items = preg_replace('~</?(html|body)>~', '', $html);
return $items;
}, 10, 2);
This should add a class has-{num}-children to every submenu with children (where {num} is the number of children), so that you can target whatever you want.
It can be easily changed to apply the class only to the submenus with a minimum number of children.
Remember to change {MENU_SLUG} with the slug of your menu.

Is it possible to *optionally* override a theme in Drupal 6?

I want to override the theming of only one (custom) menu. I can do this with phptemplate_menu_tree() but - of course - it overrides the rendering of all menus.
I've tried returning FALSE (an obvious technique IMO) if the menu is not the specific one I want to override - but this doesn't cause the overridden theme function to be called.
My only alternative (when the menu is anything other than the specific one) is to call the overridden function from within phptemplate_menu_tree() - but this seems to defeat the whole point of the override system, since the default rendering function will be hard-coded therein.
I hope the explanation is clear, and any help is greatly appreciated - tks.
UPDATE
For the sake of future reference, I'll explain how I solved this.
First off, the menu rendering starts with this function in menu.module:
function menu_block($op = 'list', $delta = 0) {
$menus = menu_get_menus();
// The Navigation menu is handled by the user module.
unset($menus['navigation']);
if ($op == 'list') {
$blocks = array();
foreach ($menus as $name => $title) {
// Default "Navigation" block is handled by user.module.
$blocks[$name]['info'] = check_plain($title);
// Menu blocks can't be cached because each menu item can have
// a custom access callback. menu.inc manages its own caching.
$blocks[$name]['cache'] = BLOCK_NO_CACHE;
}
return $blocks;
}
else if ($op == 'view') {
$data['subject'] = check_plain($menus[$delta]);
$data['content'] = menu_tree($delta);
return $data;
}
}
If you only want to override how individual item (links) are rendered then you can use the theme system (there are loads of references on how do this) - but if you want complete control on how the entire menu tree is rendered (for example, wrapping the output in nested DIVs so it can be centred on the page) then there is no way to override menu_block().
Therefore, I removed the menu I wanted to render differently from the administer blocks page (site building->blocks) and rendered the menu directly in my page.tpl.php using code something like this: (angle brackets removed)
$m = menu_tree_page_data('my-menu-id');
$o = "DIV";
foreach($m as $k => $v){
$o .= "SPAN {$v['link']['title']} /SPAN";
}
$o .= "/DIV";
echo $o;
I hope this helps.
I've had mixed success doing template.php menu overrides to force CSS classes and ids or HTML into the output.
You could make use of Block Theme when enabling the menu as a block, but I've never tried it.
http://drupal.org/project/blocktheme
If you want to tackle the template way, here are the zen menu override funcitons...
function zen_menu_item_link($link) {
if (empty($link['localized_options'])) {
$link['localized_options'] = array();
}
// If an item is a LOCAL TASK, render it as a tab
if ($link['type'] & MENU_IS_LOCAL_TASK) {
$link['title'] = '<span class="tab">' . check_plain($link['title']) . '</span>';
$link['localized_options']['html'] = TRUE;
}
return l($link['title'], $link['href'], $link['localized_options']);
}
function zen_menu_local_tasks() {
$output = '';
if ($primary = menu_primary_local_tasks()) {
$output .= '<ul class="tabs primary clear-block">' . $primary . '</ul>';
}
if ($secondary = menu_secondary_local_tasks()) {
$output .= '<ul class="tabs secondary clear-block">' . $secondary . '</ul>';
}
return $output;
}
You could use sections module, or look at how it switches theme for certain menu-items.
what I did was register a new theme function in my template.php called primary_links (because I wanted to only customize this menu in certain way) created the function mytheme_primary_links() in my template.php refreshed the cache so Drupal would add my theme function to the system then changed theme function on primary_links from links to my custom theme function primary_links - this allows me to customize only this 1 menu - could you do this and hook into where ever to change the theme function being called for your links?
Chris

Wordpress sidebar classes

There any way i could add a class for the specific order of widget item? i.e. the class name would be widgetorder-1 (for the first appearing widget), widgetorder-2 (for the widget appearing in the 2nd order), etc. I looked into filters but wasn't sure how that worked.
// Set this to whatever number you'd like the ordering to start on.
$my_blog_widget_count = 0;
// put your blog sidebar here
$my_blog_sidebar_id = '';
function output_my_widget_info($a){
global $my_blog_sidebar_id, $my_blog_widget_count;
if($a[0]['id'] == $my_blog_sidebar_id){
global $my_blog_sidebar_id, $my_blog_widget_count;
$a[0]['before_widget'] = preg_replace( '/ class="widget /i', ' class="widget widget-'.$my_blog_widget_count.' ', $a[0]['before_widget'] );
$my_blog_widget_count++;
}
return $a;
}
add_filter('dynamic_sidebar_params','output_my_widget_info');
That should do it for you. Just stick that inside your theme's functions.php file and watch it work.

Resources