Adding CSS class as a variable for PHP loop - php

I have a PHP function that takes a table that has been filtered using gravity flow. This function loops through each row of the table. I want to change the styling of this table. I have a specific CSS class I want the table styling to have. This class is "tr-shadow". I would apply this CSS class to each row of the table. I have a $style variable that is used for the CSS. How do I add the CSS class as a string for the $style variable so that the table can print out with the CSS from that class? Here is the code below.
'''
public function single_row_columns( $item ) {
list( $columns, $hidden ) = $this->get_column_info();
foreach ( $columns as $column_name => $column_display_name ) {
$class = "class='$column_name column-$column_name'";
$style = "class='tr-shadow'";
if ( in_array( $column_name, $hidden ) ) {
$style = "class='tr-shadow'";
}
$data_label = ( ! empty( $column_display_name ) ) ? " data-label='$column_display_name'" : '';
$attributes = "$class$style$data_label";
if ( 'cb' == $column_name ) {
echo '<th data-label="' . esc_html__( 'Select', 'gravityflow' ) . '" scope="row" class="check-column">';
echo $this->column_cb( $item );
echo '</th>';
} elseif ( method_exists( $this, 'column_' . $column_name ) ) {
echo "<td $attributes>";
echo call_user_func( array( $this, 'column_' . $column_name ), $item );
echo '</td>';
} else {
echo "<td $attributes>";
echo $this->column_default( $item, $column_name );
echo '</td>';
}
}
}
'''
As you can see I have already tried adding "$style="class='tr-shadow". However, this doesn't add any style to the tables. I am assuming that I am not correctly formatting the $style variable in a way that the css class is recognizable. How can I use the $style variable to successfully output the css class to each row of the table.

Your $style variable is including the attribute style again, which will result in something like:
class="column_name column-bar class=' ...
which is invalid.
I'd recommend changing your code to something like this:
public function single_row_columns( $item ) {
list( $columns, $hidden ) = $this->get_column_info();
foreach ( $columns as $column_name => $column_display_name ) {
$class = "$column_name column-$column_name";
$class .= " tr-shadow";
if ( in_array( $column_name, $hidden ) ) {
$class .= " tr-shadow";
}
$data_label = ( ! empty( $column_display_name ) ) ? " data-label='$column_display_name'" : '';
$attributes = "$data_label";
if ( 'cb' == $column_name ) {
echo '<th data-label="' . esc_html__( 'Select', 'gravityflow' ) . '" scope="row" class="check-column">';
echo $this->column_cb( $item );
echo '</th>';
} elseif ( method_exists( $this, 'column_' . $column_name ) ) {
echo "<td class=\"$class\" $attributes>";
echo call_user_func( array( $this, 'column_' . $column_name ), $item );
echo '</td>';
} else {
echo "<td class=\"$class\" $attributes>";
echo $this->column_default( $item, $column_name );
echo '</td>';
}
}
}
Also, it looks like class is always tr-shadow regardless of what you do.

In this code fragment you set the classes:
$class = "class='$column_name column-$column_name'";
$style = "class='tr-shadow'";
And then you concatenate it in one string:
$attributes = "$class$style$data_label";
So that, I think your second class is ignored.

Related

WordPress PHP - get_the_term_list() without links

I have successfully managed to make a shortcode into my product description, to show all my $taxonomy and $attribute_label values in a nice table form. Unfortunately, it returns the $attribute_label values as a link. I want them to be displayed as text.
$html .= get_the_term_list( $product->get_id(), $taxonomy, '<tr><td>' . $attribute_label . '</td><td style="text-align:right">' , '</td><td>', '</td></tr>' );
I have tried to use wp_get_object_terms but it wont work for me , it returns "Array" in all my Columns in the <table> Form.
My full Code so far:
function so_39394127_attributes_shortcode( $atts ) {
global $product;
if( ! is_object( $product ) || ! $product->has_attributes() ){
return;
}
// parse the shortcode attributes
$args = shortcode_atts( array(
'attributes' => array_keys( $product->get_attributes() ), // by default show all attributes
), $atts );
// is pass an attributes param, turn into array
if( is_string( $args['attributes'] ) ){
$args['attributes'] = array_map( 'trim', explode( '|' , $args['attributes'] ) );
}
// start with a null string because shortcodes need to return not echo a value
$html = '';
if( ! empty( $args['attributes'] ) ){
foreach ( $args['attributes'] as $attribute ) {
// get the WC-standard attribute taxonomy name
$taxonomy = strpos( $attribute, 'pa_' ) === false ? wc_attribute_taxonomy_name( $attribute ) : $attribute;
if( taxonomy_is_product_attribute( $taxonomy ) ){
// Get the attribute label.
$attribute_label = wc_attribute_label( $taxonomy );
// Build the html string with the label followed by a list of terms.
// Updated for WC3.0 to use getters instead of directly accessing property.
$html .= wp_get_object_terms( $product->get_id(), $taxonomy, '<tr><td>' . $attribute_label . '</td><td style="text-align:right">' , '</td><td>', '</td></tr>' );
}
}
// if we have anything to display, wrap it in a <table> for proper markup
if( $html ){
$html = '<table class="product-attributes">' . $html . '</table>';
}
}
return $html;
}
add_shortcode( 'display_attributes', 'so_39394127_attributes_shortcode' );
I am new to PHP so any help is very appreciated!
function ts_add_text_short_descr($atts) {
global $product;
if (!is_object($product) || !$product->has_attributes()) {
return;
}
// parse the shortcode attributes
$args = shortcode_atts(array(
'attributes' => array_keys($product->get_attributes()), // by default show all attributes
), $atts);
// is pass an attributes param, turn into array
if (is_string($args['attributes'])) {
$args['attributes'] = array_map('trim', explode('|', $args['attributes']));
}
// start with a null string because shortcodes need to return not echo a value
$html = '';
if (!empty($args['attributes'])) {
foreach ($args['attributes'] as $attribute) {
// get the WC-standard attribute taxonomy name
$taxonomy = strpos($attribute, 'pa_') === false ? wc_attribute_taxonomy_name($attribute) : $attribute;
if (taxonomy_is_product_attribute($taxonomy)) {
// Get the attribute label.
$attribute_label = wc_attribute_label($taxonomy);
// Build the html string with the label followed by a list of terms.
// Updated for WC3.0 to use getters instead of directly accessing property.
$html .= "<tr><td>$attribute_label</td>";
$terms_list = wp_get_object_terms($product->get_id(), $taxonomy);
foreach ($terms_list as $term) {
$html .= '<td style="text-align:right">' . $term->name . '</td>';
}
$html .= '</tr>';
}
}
// if we have anything to display, wrap it in a <table> for proper markup
if ($html) {
$html = '<table class="product-attributes">' . $html . '</table>';
}
}
return $html;
}
This will print like
Tested with a sample attribute Color

Adding meta fields to attribute list on WooCommerce single product page

Based on Get product custom attributes to display them in WooCommerce product loop answer code.
I am displaying specific product attributes on the single product page with this:
add_action('woocommerce_single_product_summary', 'display_custom_attributes', 36 );
function display_custom_attributes() {
global $product;
$attributes_names = array('Brand', 'Color', 'Size');
$attributes_data = array();
foreach ( $attributes_names as $attribute_name ) {
if ( $value = $product->get_attribute($attribute_name) ) {
$attributes_data[] = $attribute_name . ': ' . $value;
}
}
if ( ! empty($attributes_data) ) {
echo '<h4>Details</h4><ul><li>' . implode( '</li><li>', $attributes_data ) . '</ul>';
}
Now I also need to add two custom meta fields ('Serial_Number' and 'MPN') to this list.
How can I add these?
To add two custom meta fields:
Serial_Number
MPN
to this list, you can use:
function action_woocommerce_single_product_summary() {
global $product;
$attributes_names = array( 'Brand', 'Color', 'Size' );
$attributes_data = array();
foreach ( $attributes_names as $attribute_name ) {
if ( $value = $product->get_attribute($attribute_name) ) {
$attributes_data[] = $attribute_name . ': ' . $value;
}
}
// NOT empty
if ( ! empty($attributes_data) ) {
echo '<h4>' . __( 'Details', 'woocommerce' ) . '</h4><ul><li>' . implode( '</li><li>', $attributes_data );
}
// Get meta
$sn = $product->get_meta( 'Serial_Number' );
$mpn = $product->get_meta( 'MPN' );
// NOT empty
if ( ! empty ( $sn ) ) {
echo '<li>' . __( 'My label 1: ', 'woocommerce' ) . $sn . '</li>';
}
// NOT empty
if ( ! empty ( $mpn ) ) {
echo '<li>' . __( 'My label 2: ', 'woocommerce' ) . $mpn . '</li>';
}
echo '</ul>';
}
add_action( 'woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 36, 0 );
Add this code on the function.php theme file
function call_product_meta_data()
{
global $post;
if ($post) {
$post_meta_value = get_post_meta($post->ID, 'post_meta_key', true);
if ($post_meta_value != null) {
echo "<div class='pr-meta'><p><strong>Meta Title</strong>: $post_meta_value</p></div>";
} else {
echo "<div class='pr-meta'><p><strong>Meta Title</strong>: N/A</p></div>";
}
}
}
add_action('woocommerce_single_variation', 'call_product_meta_data');

Wordpress Database query and Output as HTML list

I'm trying to get a PHP Code to work in order to make a Database query within a Wordpress database table. The results (all distinct values of one specific column) should be put out within an HTML list. I want to be able to place the output everywhere in my content, so it should be accessable via shortcode.
Here's what I got so far. This code does not produce any errors in wordpress or the console, but it also doesn't do the trick... Nothing appears at the desired place.
class my_Shortcode {
var $echo;
function __construct() {
add_shortcode( 'my_plugin', array( $this, 'shortcode' ) );
}
function shortcode( $atts ) {
global $wpdb;
$table = isset( $atts['table'] ) ? $atts['table'] : false;
$column = isset( $atts['column'] ) ? $atts['column'] : false;
$listDisplay = isset( $atts['listDisplay'] ) ? $atts['listDisplay'] : false;
if ( $listDisplay == true ) {
if ( false != $table && false != $column ) {
$this->echo = "";
$results = $wpdb->get_results(
prepare('SELECT DISTINCT ' . $column . ' FROM ' . $table)
);
$this->echo .= "<div class=\"list\"><ul>";
foreach ($results as $result) {
$this->echo .= "<li>$result</li>\n";
}
$this->echo .= "</ul></div>";
return $this->echo;
}
}
}
}
I'm glad about any suggestions!
Do you want likve this?
<?php
echo do_shortcode("[my_plugin table='wp_posts' column=id]");
?>
class my_Shortcode {
var $echo;
function __construct() {
add_shortcode( 'my_plugin', array( $this, 'shortcode' ) );
}
function shortcode( $atts ) {
global $wpdb;
$table = isset( $atts['table'] ) ? $atts['table'] : false;
$column = isset( $atts['column'] ) ? $atts['column'] : false;
$listDisplay = isset( $atts['listDisplay'] ) ? $atts['listDisplay'] : true;
if ( $listDisplay == true ) {
if ( false != $table && false != $column ) {
$this->echo = "";
$results = $wpdb->get_results("SELECT DISTINCT $column FROM $table ",ARRAY_A);
$this->echo .= "<div class=\"list\"><ul>";
foreach ($results as $key => $result) {
$keys = array_keys( $result);
$this->echo .= '<li>'.$result[ $keys[0] ].'</li>\n';
}
$this->echo .= "</ul></div>";
return $this->echo;
}
}
}
}
$test = new my_Shortcode();

How do I make default settings work for select fields with WordPress API?

std is the default value of an option if it is not set. I only need the default value for the select fields. I've tried many things but nothing seems to work. I really need help on this. I don't know where to start.
So here, I do my function to add the setting to my options page.
function nl_register_function_name() {
// Validation callback
register_setting( 'nl_theme_options', 'nl_theme_options', 'nl_validate_settings' );
// Add setting to section
add_settings_section( 'nl_styling_section', 'Styling', 'nl_display_styling_section', 'nl_theme_options.php' );
// Create selection field
$field_args = array(
'type' => 'select',
'id' => 'nl_function_name',
'name' => 'nl_function_name',
'desc' => 'Select border bottom size.',
'std' => 'Small',
'label_for' => 'nl_function_name',
'items' => array(
"None",
"Small",
"Medium",
"Large"
)
);
// Label
add_settings_field( 'label_function_name', 'function_name', 'nl_display_setting', 'nl_theme_options.php', 'nl_styling_section', $field_args );
}
// Registers the setting
add_action( 'admin_init', 'nl_register_function_name' );
And here, I create the HTML for the select field.
function nl_display_setting ( $args ) {
extract( $args );
$option_name = 'nl_theme_options';
$options = get_option( $option_name );
switch ( $args['type'] ) {
case 'select':
if( isset( $options[$id] ) ) {
$options[$id] = stripslashes( $options[$id] );
$options[$id] = esc_attr( $options[$id] );
echo "<select name='" . $option_name . "[$id]' value='$options[$id]'>";
foreach( $items as $item ) {
$selected = ($options[$id]==$item) ? 'selected="selected"' : '';
echo "<option value='$item' $selected>$item</option>";
}
} elseif ( !isset( $options[$id] ) ) {
$std = $args['std'];
echo "<select name='" . $option_name . "[$id]' value='$std'>";
foreach( $items as $item ) {
echo "<option value='$item'>$item</option>";
}
}
echo "</select>";
echo ($desc != '') ? "<br><span class='description'>$desc</span>" : "";
break;
}
}
How do I make default settings work for select fields with WordPress API?
I fixed it! I'm going to post the solution here for others that might have the same problem in the future because there doesn't seem to be any solution online.
case 'select':
if( isset( $options[$id] ) ) {
$options[$id] = stripslashes( $options[$id] );
$options[$id] = esc_attr( $options[$id] );
echo "<select name='" . $option_name . "[$id]' value='$options[$id]'>";
foreach( $items as $item ) {
$selected = ($options[$id]==$item) ? 'selected="selected"' : '';
echo "<option value='$item' $selected>$item</option>";
}
} elseif ( !isset( $options[$id] ) ) {
echo "<select name='" . $option_name . "[$id]' value='$std'>";
foreach( $items as $item ) {
$selected = ($std==$item) ? 'selected="selected"' : '';
echo "<option value='$item' $selected>$item</option>";
}
}
echo "</select>";
echo ($desc != '') ? "<br><span class='description'>$desc</span>" : "";
break;

How to modify one condition inside a function without arg input?

I need to call a function that output a html. Inside this function, there's one condition to exclude one row from the html, I need this row. This condition is not an argument of the function. It's hard coded. So, I have to code my own, or, is there a way to modify this condition?
function get_media_item( $attachment_id, $args = null ) {
//....lots stuffs, then, arrive this part:
$gallery = ( ( isset( $_REQUEST['tab'] ) && 'gallery' == $_REQUEST['tab'] ) || ( isset( $redir_tab ) && 'gallery' == $redir_tab ) );
$order = '';
foreach ( $form_fields as $key => $val ) {
if ( 'menu_order' == $key ) {
if ( $gallery )
$order = "<div class='menu_order'> <input class='menu_order_input' type='text' id='attachments[$attachment_id][menu_order]' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ). "' /></div>";
else
$order = "<input type='hidden' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ) . "' />";
unset( $form_fields['menu_order'] );
break;
}
}
//... other stuffs
}
I need to call this function in other tabs, not "gallery" tab. So, I can't get the $order input box.
If you don't want to pass a parameter to the function(which would be recommended btw) to modify the condition, you can use globalize variable(or session).
Example:
function test(){
global $instructions;
if (isset($instructions['fetch'])){
// new codes
} else {
// existing codes
}
return $result;
}
// implement:
$instructions = array();
$instructions['fetch'] = TRUE;
echo test();

Categories