HTML every 5 iterations on foreach - php

I´m using extra_user_details.php on wordpress to show user details in a private profile page. As I´m using a lot of extra fields I though about break the query and make the same output every X values in order to show as tabs:
function eud_extract_ExtraFields() {
if ( get_option( 'eud_fields' ) ) {
$all_fields = unserialize( get_option( 'eud_fields' ) );
if ( count( $all_fields ) > 0 ) {
$output = '';
foreach ( $all_fields as $key => $value ) {
if ( isset($value[3]) && ! empty($value[3]) ) {
if ( ($value[3] == 'disable') || ! current_user_can($value[3]) ) {
continue;
}
}
$output .= '<tr>
<th><label for="eud' . esc_attr( $value[1] ) . '">' . esc_attr( $value[0] ) . '</label></th>
<td><input name="eud' . esc_attr( $value[1] ) . '" id="eud' . esc_attr( $value[1] ) . '" type="text" value="' . esc_attr( get_user_meta( get_user_id(), $value[1], true ) ) . '" class="regular-text code" /> <span class="description">' . ( ( isset( $value[2] ) && $value[2] !== '' ) ? esc_attr( stripslashes( $value[2] ) ) : '' ) . '</span></td>
</tr>';
}
}
if ($output != '') {
echo '<div><table class="form-table">';
echo $output;
echo '</table></div>';
}
} }
Thanks!
I´m not sure if this is what I´m looking for. I´m just near...
function eud_extract_ExtraFields() {
if ( get_option( 'eud_fields' ) ) {
$all_fields = unserialize( get_option( 'eud_fields' ) );
if ( count( $all_fields ) > 0 ) {
$output = '';
$i=0;
foreach ($all_fields as $key => $value ) {
if ( isset($value[3]) && ! empty($value[3]) ) {
if ( ($value[3] == 'disable') || ! current_user_can($value[3]) ) {
continue;
}
}
$output .= '<tr>
<th><label for="eud' . esc_attr( $value[1] ) . '">' . esc_attr( $value[0] ) . '</label></th>
<td><input name="eud' . esc_attr( $value[1] ) . '" id="eud' . esc_attr( $value[1] ) . '" type="text" value="' . esc_attr( get_user_meta( get_user_id(), $value[1], true ) ) . '" class="regular-text code" /> <span class="description">' . ( ( isset( $value[2] ) && $value[2] !== '' ) ? esc_attr( stripslashes( $value[2] ) ) : '' ) . '</span></td>
</tr>';
++$i;
if(!($i % 2)) {
echo '<div><table class="form-table">';
echo $output;
echo '</table></div>';
}
}
}
}
}
But I need to split the echo, I mean, now the results are:
first tab echo 1, 2
second tab echo 1,2,3,4
third tab echo 1,2,3,4,5,6
and I need the $output to be just:
first tab echo 1,2
second tab echo 3,4
third tab echo 5,6
fourth tab echo 7 (if exists)

You could use modulus:
$i=0;
foreach ( $all_fields as $key => $value ) {
if( $i++%5 === 0 ){ echo 'I was number 5';}
}
or if you prefer a binary comparison (should be faster):
if( $i++&101 === 0 ){ echo 'I was number 5';}
I'll give you an example, you can piece it together for your code:
Lets say you have an N amount of span, and you want them grouped per 5 in a div:
// You start with:
echo '<div>';
for($i=1; $i<=23; $i++){
echo '<span> '.$i.' </span>'; // just an example, could be anything here
}
echo '</div>';
This will place 23 span in one big div. Now we add something to group them by 5:
// You start with:
echo '<div>';
for($i=1; $i<=23; $i++){
echo '<span> '.$i.' </span>'; // just an example, could be anything here
if( $i %5===0 ){
echo '</div><div>'; // every 5th, close the div, and open a fresh one.
}
}
echo '</div>';
This will result in 5 (=coincedence, nothing to do with %5) div's, 4 with 5 spans, and one with the remaining 3. You can do this trick with about any element.
Tip: in the modulus-if-statement you should add the max: $i %5===0 && $i!==23, to prevent </div></div><div> if $i is a number devidable by 5.

Solution came from a friend, thanks!
function eud_extract_ExtraFields() {
if (get_option('eud_fields')) {
$all_fields = unserialize(get_option('eud_fields'));
if (count($all_fields) > 0) {
$output = '';
$i = 1;
$htmlTotal = '';
foreach ($all_fields as $key => $value) {
if (isset($value[3]) && !empty($value[3])) {
if (($value[3] == 'disable') || !current_user_can($value[3])) {
continue;
}
}
$output .= '<tr>
<th><label for="eud' . esc_attr($value[1]) . '">' . esc_attr($value[0]) . '</label></th>
<td><input name="eud' . esc_attr($value[1]) . '" id="eud' . esc_attr($value[1]) . '" type="text" value="' . esc_attr(get_user_meta(get_user_id(), $value[1], true)) . '" class="regular-text code" /> <span class="description">' . ( ( isset($value[2]) && $value[2] !== '' ) ? esc_attr(stripslashes($value[2])) : '' ) . '</span></td>
</tr>';
$i++;
/* number of fields to show per tab */
if ($i % 2) {
$htmlTotal .= '<div><table class="form-table">' . $output . '</table></div>';
$output = '';
}
}
echo $htmlTotal;
}
}
}

Related

Add order numbers to children elements, 1,2,3,4,5

I'm trying to output order numbers to elements that are generated from gallery captions.
foreach ( $item['gallery'] as $image ) {
$lines = intval( apply_filters( 'caption_line', 1 ) );
for ( $line = 1; $line <= $lines; $line++ ) {
$attachment_post = get_post( $image['id'] );
$image_caption = $attachment_post->post_excerpt;
echo '<span class="dot1" onclick="currentSlide' . $item['unit_link'] . '(' . esc_attr( $line ) . ')">' . $image_caption . '</span>';
}
}
So I need 1,2,3,4...to be generated for $line, but for what I have tried it only output 1 for all elements, changing 1 $lines = intval( apply_filters( 'caption_line', 1 ) ); to higher number shows the number, but also duplicate them.
Just figured it out, this does exactly what I was looking for!
foreach ( $item['gallery'] as $key => $image ) {
$attachment_post = get_post( $image['id'] );
$image_caption = $attachment_post->post_excerpt;
$keyplus = $key+1;
echo '<span class="dot1" onclick="currentSlide' . $item['unit_link'] . '(' . $keyplus . ')">' . $image_caption . '</span>';
}

Custom Gravity Forms Field with Multiple Inputs

I have been working on a Gravity Forms extension for a client. The concept is to add a new field type with 4 inputs. I have tried about 10 different variations on how people build custom gravity form fields, but I keep running into the same issue.
When creating a custom field, if I use only 1 input under the naming convention of input_{field_id} the form will save and validate properly. But the moment I try to add more than one field using the names input_{field_id}.{i} just like the built in fields the form will no longer save my data.
<?php if ( ! class_exists( 'GFForms' ) ) { die(); }
class GF_Field_Attendees extends GF_Field {
public $type = 'attendees';
public function get_form_editor_field_title() { return esc_attr__( 'Attendees', 'gravityforms' ); }
public function get_form_editor_button() {
return array(
'group' => 'advanced_fields',
'text' => $this->get_form_editor_field_title(),
'onclick' => "StartAddField('".$this->type."');",
);
}
public function get_form_editor_field_settings() {
return array(
'conditional_logic_field_setting',
'prepopulate_field_setting',
'error_message_setting',
'label_setting',
'admin_label_setting',
'rules_setting',
'duplicate_setting',
'description_setting',
'css_class_setting',
);
}
public function is_conditional_logic_supported() { return true; }
public function get_field_input( $form, $value = '', $entry = null ) {
$form_id = $form['id'];
$field_id = intval( $this->id );
$first = esc_attr( GFForms::get( 'input_' . $this->id . '_1', $value ) );
$last = esc_attr( GFForms::get( 'input_' . $this->id . '_2', $value ) );
$email = esc_attr( GFForms::get( 'input_' . $this->id . '_3', $value ) );
$phone = esc_attr( GFForms::get( 'input_' . $this->id . '_4', $value ) );
$disabled_text = $is_form_editor ? "disabled='disabled'" : '';
$class_suffix = $is_entry_detail ? '_admin' : '';
$first_tabindex = GFCommon::get_tabindex();
$last_tabindex = GFCommon::get_tabindex();
$email_tabindex = GFCommon::get_tabindex();
$phone_tabindex = GFCommon::get_tabindex();
$required_attribute = $this->isRequired ? 'aria-required="true"' : '';
$invalid_attribute = $this->failed_validation ? 'aria-invalid="true"' : 'aria-invalid="false"';
$first_markup = '<span id="input_'.$field_id.'_'.$form_id.'.1_container" class="attendees_first">';
$first_markup .= '<input type="text" name="input_'.$field_id.'.1" id="input_'.$field_id.'_'.$form_id.'_1" value="'.$first.'" aria-label="First Name" '.$first_tabindex.' '.$disabled_text.' '.$required_attribute.' '.$invalid_attribute.'>';
$first_markup .= '<label for="input_'.$field_id.'_'.$form_id.'_1">First Name</label>';
$first_markup .= '</span>';
$last_markup = '<span id="input_'.$field_id.'_'.$form_id.'.2_container" class="attendees_last">';
$last_markup .= '<input type="text" name="input_'.$field_id.'.2" id="input_'.$field_id.'_'.$form_id.'_2" value="'.$last.'" aria-label="Last Name" '.$last_tabindex.' '.$disabled_text.' '.$required_attribute.' '.$invalid_attribute.'>';
$last_markup .= '<label for="input_'.$field_id.'_'.$form_id.'_2">Last Name</label>';
$last_markup .= '</span>';
$email_markup = '<span id="input_'.$field_id.'_'.$form_id.'.3_container" class="attendees_email">';
$email_markup .= '<input type="text" name="input_'.$field_id.'.3" id="input_'.$field_id.'_'.$form_id.'_3" value="'.$email.'" aria-label="Email" '.$email_tabindex.' '.$disabled_text.' '.$required_attribute.' '.$invalid_attribute.'>';
$email_markup .= '<label for="input_'.$field_id.'_'.$form_id.'_3">Email</label>';
$email_markup .= '</span>';
$phone_markup = '<span id="input_'.$field_id.'_'.$form_id.'.4_container" class="attendees_phone">';
$phone_markup .= '<input type="text" name="input_'.$field_id.'.4" id="input_'.$field_id.'_'.$form_id.'_4" value="'.$phone.'" aria-label="Phone #" '.$phone_tabindex.' '.$disabled_text.' '.$required_attribute.' '.$invalid_attribute.'>';
$phone_markup .= '<label for="input_'.$field_id.'_'.$form_id.'_4">Phone #</label>';
$phone_markup .= '</span>';
$css_class = $this->get_css_class();
return "<div class='ginput_complex{$class_suffix} ginput_container {$css_class} gfield_trigger_change' id='{$field_id}'>
{$first_markup}
{$last_markup}
{$email_markup}
{$phone_markup}
<div class='gf_clear gf_clear_complex'></div>
</div>";
}
public function get_css_class() {
$first_input = GFFormsModel::get_input( $this, $this->id . '_2' );
$last_input = GFFormsModel::get_input( $this, $this->id . '_3' );
$email_input = GFFormsModel::get_input( $this, $this->id . '_4' );
$phone_input = GFFormsModel::get_input( $this, $this->id . '_5' );
$css_class = '';
$visible_input_count = 0;
if ( $first_input && ! rgar( $first_input, 'isHidden' ) ) {
$visible_input_count++;
$css_class .= 'has_first_name ';
} else {
$css_class .= 'no_first_name ';
}
if ( $last_input && ! rgar( $last_input, 'isHidden' ) ) {
$visible_input_count++;
$css_class .= 'has_last_name ';
} else {
$css_class .= 'no_last_name ';
}
if ( $email_input && ! rgar( $email_input, 'isHidden' ) ) {
$visible_input_count++;
$css_class .= 'has_email ';
} else {
$css_class .= 'no_email ';
}
if ( $phone_input && ! rgar( $phone_input, 'isHidden' ) ) {
$visible_input_count++;
$css_class .= 'has_phone ';
} else {
$css_class .= 'no_phone ';
}
$css_class .= "gf_attendees_has_{$visible_input_count} ginput_container_attendees ";
return trim( $css_class );
}
public function get_value_submission( $field_values, $get_from_post ) {
if(!$get_from_post) {
return $field_values;
}
return $_POST;
}
}
GF_Fields::register( new GF_Field_Attendees() );
I have spend about 20 hours trying different fixes and searching the internet to get this working, with no luck to show for it. At one point I was able to get the form fields to save using a different method (see below), but I could not make the field required or use conditional login on it, which is a must.
$group_title = "Attendees";
$group_name = "attendees";
$group_fields = array(
'attendee_first' => 'First Name',
'attendee_last' => 'Last Name',
'attendee_email' => 'Email',
'attendee_phone' => 'Phone'
);
$group_values = array();
add_filter('gform_add_field_buttons', add_field);
function add_field($field_group)
{
global $group_title, $group_name;
foreach ($field_group as &$group) {
if ($group['name'] == 'advanced_fields') {
$group['fields'][] = array (
'class' => 'button',
'value' => __($group_title, 'gravityforms'),
'onclick' => "StartAddField('".$group_name."');",
'data-type' => $group_name
);
break;
}
}
return $field_group;
}
add_filter('gform_field_type_title', add_field_title, 10, 2);
function add_field_title($title, $field_type)
{
global $group_title, $group_name;
if ($field_type == $group_name) {
$title = __($group_title, 'gravityforms');
}
return $title;
}
add_filter('gform_field_input', 'render_fields', 10, 5);
function render_fields($input, $field, $value, $entry_id, $form_id)
{
global $group_name, $group_fields;
if ($field->type == $group_name)
{
$i = 1;
$input = '<div class="ginput_complex ginput_container">';
foreach ($group_fields as $key => $val) {
$input .= '<span id="input_'.$field['id'].'_'.$form_id.'_'.$i.'_container" class="name_suffix ">';
$input .= '<input type="text" name="input_'.$field['id'].'_'.$i.'" id="input_'.$field['id'].'_'.$form_id.'_'.$i.'" value="'.$value[$field['id'].'.'.$i].'" class="'.esc_attr($key).'" aria-label="'.$val.'">';
$input .= '<label for="input_'.$field['id'].'_'.$form_id.'_'.$i.'">'.$val.'</label>';
$input .= '</span>';
$i ++;
if ($i % 10 == 0) { $i++; }
}
$input .= '</div>';
}
return $input;
}
add_action('gform_editor_js_set_default_values', set_default_values);
function set_default_values()
{
global $group_title, $group_name, $group_fields;
?>
case '<?php echo $group_name; ?>' :
field.label = '<?php _e($group_title, 'gravityforms'); ?>';
field.inputs = [
<?php
$i = 1;
foreach ($group_fields as $key => $val) { ?>
new Input(field.id + 0.<?php echo $i; ?>, '<?php echo esc_js(__($val, 'gravityforms')); ?>'),
<?php
$i++;
if ($i % 10 == 0) { $i++; }
} ?>
];
break;
<?php
}
add_filter( 'gform_entry_field_value', 'category_names', 10, 4 );
function category_names( $value, $field, $lead, $form )
{
global $group_name, $group_values;
if($field->type == $group_name)
{
$array = array();
$output = "";
foreach($field->inputs as $input)
{
$array[$input['label']] = $value[$input['id']];
$output .= "<strong>".$input['label'].":</strong> ";
$output .= $value[$input['id']]."<br>";
}
$group_values[] = $array;
return $output;
}
return $value;
}
If anyone can help me with either issue, it would be greatly appreciated.
Class update:
Cleaned Up get_field_input
Added get_value_submission
After working with the Gravity Forms support team for a few days, we were able to come up with this solution. Everything seems to be working now. Hope this helps someone in the future.
class GF_Field_Attendees extends GF_Field {
public $type = 'attendees';
public function get_form_editor_field_title() {
return esc_attr__( 'Attendees', 'gravityforms' );
}
public function get_form_editor_button() {
return array(
'group' => 'advanced_fields',
'text' => $this->get_form_editor_field_title(),
);
}
public function get_form_editor_field_settings() {
return array(
'conditional_logic_field_setting',
'prepopulate_field_setting',
'error_message_setting',
'label_setting',
'admin_label_setting',
'rules_setting',
'duplicate_setting',
'description_setting',
'css_class_setting',
);
}
public function is_conditional_logic_supported() {
return true;
}
public function get_field_input( $form, $value = '', $entry = null ) {
$is_entry_detail = $this->is_entry_detail();
$is_form_editor = $this->is_form_editor();
$form_id = $form['id'];
$field_id = intval( $this->id );
$first = $last = $email = $phone = '';
if ( is_array( $value ) ) {
$first = esc_attr( rgget( $this->id . '.1', $value ) );
$last = esc_attr( rgget( $this->id . '.2', $value ) );
$email = esc_attr( rgget( $this->id . '.3', $value ) );
$phone = esc_attr( rgget( $this->id . '.4', $value ) );
}
$disabled_text = $is_form_editor ? "disabled='disabled'" : '';
$class_suffix = $is_entry_detail ? '_admin' : '';
$first_tabindex = GFCommon::get_tabindex();
$last_tabindex = GFCommon::get_tabindex();
$email_tabindex = GFCommon::get_tabindex();
$phone_tabindex = GFCommon::get_tabindex();
$required_attribute = $this->isRequired ? 'aria-required="true"' : '';
$invalid_attribute = $this->failed_validation ? 'aria-invalid="true"' : 'aria-invalid="false"';
$first_markup = '<span id="input_' . $field_id . '_' . $form_id . '.1_container" class="attendees_first">';
$first_markup .= '<input type="text" name="input_' . $field_id . '.1" id="input_' . $field_id . '_' . $form_id . '_1" value="' . $first . '" aria-label="First Name" ' . $first_tabindex . ' ' . $disabled_text . ' ' . $required_attribute . ' ' . $invalid_attribute . '>';
$first_markup .= '<label for="input_' . $field_id . '_' . $form_id . '_1">First Name</label>';
$first_markup .= '</span>';
$last_markup = '<span id="input_' . $field_id . '_' . $form_id . '.2_container" class="attendees_last">';
$last_markup .= '<input type="text" name="input_' . $field_id . '.2" id="input_' . $field_id . '_' . $form_id . '_2" value="' . $last . '" aria-label="Last Name" ' . $last_tabindex . ' ' . $disabled_text . ' ' . $required_attribute . ' ' . $invalid_attribute . '>';
$last_markup .= '<label for="input_' . $field_id . '_' . $form_id . '_2">Last Name</label>';
$last_markup .= '</span>';
$email_markup = '<span id="input_' . $field_id . '_' . $form_id . '.3_container" class="attendees_email">';
$email_markup .= '<input type="text" name="input_' . $field_id . '.3" id="input_' . $field_id . '_' . $form_id . '_3" value="' . $email . '" aria-label="Email" ' . $email_tabindex . ' ' . $disabled_text . ' ' . $required_attribute . ' ' . $invalid_attribute . '>';
$email_markup .= '<label for="input_' . $field_id . '_' . $form_id . '_3">Email</label>';
$email_markup .= '</span>';
$phone_markup = '<span id="input_' . $field_id . '_' . $form_id . '.4_container" class="attendees_phone">';
$phone_markup .= '<input type="text" name="input_' . $field_id . '.4" id="input_' . $field_id . '_' . $form_id . '_4" value="' . $phone . '" aria-label="Phone #" ' . $phone_tabindex . ' ' . $disabled_text . ' ' . $required_attribute . ' ' . $invalid_attribute . '>';
$phone_markup .= '<label for="input_' . $field_id . '_' . $form_id . '_4">Phone #</label>';
$phone_markup .= '</span>';
$css_class = $this->get_css_class();
return "<div class='ginput_complex{$class_suffix} ginput_container {$css_class} gfield_trigger_change' id='{$field_id}'>
{$first_markup}
{$last_markup}
{$email_markup}
{$phone_markup}
<div class='gf_clear gf_clear_complex'></div>
</div>";
}
public function get_css_class() {
$first_input = GFFormsModel::get_input( $this, $this->id . '.1' );
$last_input = GFFormsModel::get_input( $this, $this->id . '.2' );
$email_input = GFFormsModel::get_input( $this, $this->id . '.3' );
$phone_input = GFFormsModel::get_input( $this, $this->id . '.4' );
$css_class = '';
$visible_input_count = 0;
if ( $first_input && ! rgar( $first_input, 'isHidden' ) ) {
$visible_input_count ++;
$css_class .= 'has_first_name ';
} else {
$css_class .= 'no_first_name ';
}
if ( $last_input && ! rgar( $last_input, 'isHidden' ) ) {
$visible_input_count ++;
$css_class .= 'has_last_name ';
} else {
$css_class .= 'no_last_name ';
}
if ( $email_input && ! rgar( $email_input, 'isHidden' ) ) {
$visible_input_count ++;
$css_class .= 'has_email ';
} else {
$css_class .= 'no_email ';
}
if ( $phone_input && ! rgar( $phone_input, 'isHidden' ) ) {
$visible_input_count ++;
$css_class .= 'has_phone ';
} else {
$css_class .= 'no_phone ';
}
$css_class .= "gf_attendees_has_{$visible_input_count} ginput_container_attendees ";
return trim( $css_class );
}
public function get_form_editor_inline_script_on_page_render() {
// set the default field label for the field
$script = sprintf( "function SetDefaultValues_%s(field) {
field.label = '%s';
field.inputs = [new Input(field.id + '.1', '%s'), new Input(field.id + '.2', '%s'), new Input(field.id + '.3', '%s'), new Input(field.id + '.4', '%s')];
}", $this->type, $this->get_form_editor_field_title(), 'First Name', 'Last Name', 'Email', 'Phone' ) . PHP_EOL;
return $script;
}
public function get_value_entry_detail( $value, $currency = '', $use_text = false, $format = 'html', $media = 'screen' ) {
if ( is_array( $value ) ) {
$first = trim( rgget( $this->id . '.1', $value ) );
$last = trim( rgget( $this->id . '.2', $value ) );
$email = trim( rgget( $this->id . '.3', $value ) );
$phone = trim( rgget( $this->id . '.4', $value ) );
$return = $first;
$return .= ! empty( $return ) && ! empty( $last ) ? " $last" : $last;
$return .= ! empty( $return ) && ! empty( $email ) ? " $email" : $email;
$return .= ! empty( $return ) && ! empty( $phone ) ? " $phone" : $phone;
} else {
$return = '';
}
if ( $format === 'html' ) {
$return = esc_html( $return );
}
return $return;
}
}
GF_Fields::register( new GF_Field_Attendees() );
I've been having difficulties getting the value from rgget for the fields and I had to change it to the following to get it to work:
rgget( 'input_' . $this->id . '_1', $value )

Aggregate img url function not functional with one blog built with blogger

I am trying to aggregate the latest posts by several blogs using their rss feed. The code below is working fine for most platforms including blogg.se . Although it is strangely not working with a blog from blogg.se called, http://sofiasinredning.blogg.se/index.rss .
function wpse_187819_get_feed_html( $url, $bloggauthor ) {
if ( !is_admin() ) {
if ( is_wp_error( $rss = fetch_feed( $url ) ) )
return;
$maxitems = $rss->get_item_quantity( 1 );
$rss_items = $rss->get_items( 0, $maxitems );
$html = '<ul class="rss-items rss-old" id="wows-feeds">';
if (!empty($bloggauthor)) {
$authorcredit = '<span class="field-label">' . $bloggauthor . '</span>';
} else { $authorcredit = '<span class="field-label"> Name </span>';}
if ( $maxitems ) {
foreach ( $rss_items as $item ) {
$title = esc_attr( $item->get_title() );
$link = esc_url( $item->get_permalink() );
$stringses = $item-> get_description();
global $wp_query;
$lengthses = '';
if ( $wp_query->is_page ) {
$lengthses = '100';
} elseif ( $wp_query->is_home ) {
$lengthses = '100';
} elseif ( $wp_query->is_single ) {
$lengthses = '700';
} elseif ( $wp_query->is_category ) {
$lengthses = '100';
} elseif ( $wp_query->is_tag ) {
$lengthses = '50';
} elseif ( $wp_query->is_tax ) {
$lengthses = '30';
} elseif ( $wp_query->is_archive ) {
if ( $wp_query->is_day ) {
$lengthses = '60';
} elseif ( $wp_query->is_month ) {
$lengthses = '60';
} elseif ( $wp_query->is_year ) {
$lengthses = '60';
} elseif ( $wp_query->is_author ) {
$lengthses = '400';
} else {
$lengthses = '500';
}
} elseif ( $wp_query->is_search ) {
$lengthses = '400';
} elseif ( $wp_query->is_404 ) {
$lengthses = '';
}
$date = $item->get_date('F Y');
$suffix = '…';
$short_desc = trim(str_replace(array(" ", "/r", "/n", "/t"), '', strip_tags($stringses)));
$desc = trim(substr($short_desc, 0, $lengthses));
$lastchar = substr($desc, 0, 1);
if ($lastchar == '.' || $lastchar == '!' || $lastchar == '?') $suffix='';
$desc .= $suffix;
$html .= '<li class="item"><a target="_blanc" href="' . $link . '" title="' . $title . '">';
if ( preg_match( '/<img.+?src="(.+?)"/', $item->get_content(), $matches ) ) {
$html .= '<span class="rss-image"><div class="square attachment-special" style="background-image: url(' . $matches[1] . ');"><img style="opacity: 0;" class="attachment-special" src="' . $matches[1] . '"/></div></span></a>';
}
elseif ($enclosure = $item->get_enclosure()) {
$html .= '<span class="rss-image"><div class="square attachment-special" style="background-image: url(' . $enclosure->get_link() . ');"><img style="opacity: 0;" class="attachment-special" src="' . $enclosure->get_link() . '"/></div></span></a>';
}
else {
$html .= '<img class="attachment-special" src="' . get_stylesheet_directory_uri() . '/img/logga/500x500.png"/></a>';
}
$html .= '<div class="item-content"><h5 class="pink-text">' . $authorcredit . '</h5><a target="_blanc" href="' . $link . '" title="' . $title . '"><span class="data"><h1 class="entry-title">' . $title . '</h1></span></a>';
$html .= '<p>' . $desc . '</p>';
$html .= '</li>';
}
} else {
$html .= '<li>No items</li>';
}
$html .= '</ul>';
echo $html;
}
}
Thanks in advance

Update wp_term_relationships table by using the value of wp_postmeta

Hello There I have created a meta box for date in my functions.php of admin. The code is here:-
function ep_eventposts_metaboxes() {
//add_meta_box( 'ept_event_date_start', 'Start Date and Time', 'ept_event_date', 'event', 'side', 'default', array( 'id' => '_start') );
add_meta_box( 'ept_event_date_end', 'Expiratory Date and Time', 'ept_event_date', 'post', 'side', 'default', array('id'=>'_end') );
//add_meta_box( 'ept_event_location', 'Event Location', 'ept_event_location', 'event', 'normal', 'default', array('id'=>'_end') );
}
add_action( 'admin_init', 'ep_eventposts_metaboxes' );
// Metabox HTML
function ept_event_date($post, $args) {
$metabox_id = $args['args']['id'];
global $post, $wp_locale;
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), 'ep_eventposts_nonce' );
$time_adj = current_time( 'timestamp' );
$month = get_post_meta( $post->ID, $metabox_id . '_month', true );
if ( empty( $month ) ) {
$month = gmdate( 'm', $time_adj );
}
$day = get_post_meta( $post->ID, $metabox_id . '_day', true );
if ( empty( $day ) ) {
$day = gmdate( 'd', $time_adj );
}
$year = get_post_meta( $post->ID, $metabox_id . '_year', true );
if ( empty( $year ) ) {
$year = gmdate( 'Y', $time_adj );
}
$hour = get_post_meta($post->ID, $metabox_id . '_hour', true);
if ( empty($hour) ) {
$hour = gmdate( 'H', $time_adj );
}
$min = get_post_meta($post->ID, $metabox_id . '_minute', true);
if ( empty($min) ) {
//$min = '00';
$min = gmdate( 'i', $time_adj );
}
$month_s = '<select name="' . $metabox_id . '_month">';
for ( $i = 1; $i < 13; $i = $i +1 ) {
$month_s .= "\t\t\t" . '<option value="' . zeroise( $i, 2 ) . '"';
if ( $i == $month )
$month_s .= ' selected="selected"';
$month_s .= '>' . $wp_locale->get_month_abbrev( $wp_locale->get_month( $i ) ) . "</option>\n";
}
$month_s .= '</select>';
echo $month_s;
echo '<input type="text" name="' . $metabox_id . '_day" value="' . $day . '" size="2" maxlength="2" />';
echo '<input type="text" name="' . $metabox_id . '_year" value="' . $year . '" size="4" maxlength="4" /> # ';
echo '<input type="text" name="' . $metabox_id . '_hour" value="' . $hour . '" size="2" maxlength="2"/>:';
echo '<input type="text" name="' . $metabox_id . '_minute" value="' . $min . '" size="2" maxlength="2" />';
}
/*
function ept_event_location() {
global $post;
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), 'ep_eventposts_nonce' );
// The metabox HTML
$event_location = get_post_meta( $post->ID, '_event_location', true );
echo '<label for="_event_location">Location:</label>';
echo '<input type="text" name="_event_location" value="' . $event_location . '" />';
}
*/
// Save the Metabox Data
function ep_eventposts_save_meta( $post_id, $post ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( !isset( $_POST['ep_eventposts_nonce'] ) )
return;
if ( !wp_verify_nonce( $_POST['ep_eventposts_nonce'], plugin_basename( __FILE__ ) ) )
return;
// Is the user allowed to edit the post or page?
if ( !current_user_can( 'edit_post', $post->ID ) )
return;
// OK, we're authenticated: we need to find and save the data
// We'll put it into an array to make it easier to loop though
$metabox_ids = array('_end' );
foreach ($metabox_ids as $key ) {
$aa1 = $_POST[$key . '_year'];
$mm1 = $_POST[$key . '_month'];
$jj1 = $_POST[$key . '_day'];
$hh = $_POST[$key . '_hour'];
$mn = $_POST[$key . '_minute'];
$aa1 = ($aa1 <= 0 ) ? date('Y') : $aa1;
$mm1 = ($mm1 <= 0 ) ? date('n') : $mm1;
$jj1 = sprintf('%02d',$jj1);
$jj1 = ($jj1 > 31 ) ? 31 : $jj1;
$jj1 = ($jj1 <= 0 ) ? date('j') : $jj1;
$hh = sprintf('%02d',$hh);
$hh = ($hh > 23 ) ? 23 : $hh;
$hh = ($hh <= 0 ) ? date('H') : $hh;
$mn = sprintf('%02d',$mn);
$mn = ($mn > 59 ) ? 59 : $mn;
$mn = ($mn <= 0 ) ? date('i') : $mn;
$events_meta[$key . '_year'] = $aa1;
$events_meta[$key . '_month'] = $mm1;
$events_meta[$key . '_day'] = $jj1;
$events_meta[$key . '_hour'] = $hh;
$events_meta[$key . '_minute'] = $mn;
$events_meta[$key . '_eventtimestamp'] = $aa1 ."-" .$mm1 ."-". $jj1." " . $hh.":" . $mn.":".date('s');
echo $events_meta;
}
// Add values of $events_meta as custom fields
foreach ( $events_meta as $key => $value ) { // Cycle through the $events_meta array!
if ( $post->post_type == 'revision' ) return; // Don't store custom data twice
$value = implode( ',', (array)$value ); // If $value is an array, make it a CSV (unlikely)
if ( get_post_meta( $post->ID, $key, FALSE ) ) { // If the custom field already has a value
update_post_meta( $post->ID, $key, $value );
} else { // If the custom field doesn't have a value
add_post_meta( $post->ID, $key, $value );
}
if ( !$value ) delete_post_meta( $post->ID, $key ); // Delete if blank
}
}
add_action( 'save_post', 'ep_eventposts_save_meta', 1, 2 );
/**
* Helpers to display the date on the front end
*/
// Get the Month Abbreviation
function eventposttype_get_the_month_abbr($month) {
global $wp_locale;
for ( $i = 1; $i < 13; $i = $i +1 ) {
if ( $i == $month )
$monthabbr = $wp_locale->get_month_abbrev( $wp_locale->get_month( $i ) );
}
return $monthabbr;
}
// Display the date
function eventposttype_get_the_event_date() {
global $post;
$eventdate = '';
$month = get_post_meta($post->ID, '_month', true);
$eventdate = eventposttype_get_the_month_abbr($month);
$eventdate .= ' ' . get_post_meta($post->ID, '_day', true) . ',';
$eventdate .= ' ' . get_post_meta($post->ID, '_year', true);
$eventdate .= ' at ' . get_post_meta($post->ID, '_hour', true);
$eventdate .= ':' . get_post_meta($post->ID, '_minute', true);
echo $eventdate;
}
It works fine and adding the metabox on post. And Putting the value in wp_postmeta.
Now i have created a function to change the value add a TaxonomyID for the post after the condition satisfies. The code is here:-
function wp_insert_category_to_post()
{
$postid=get_the_ID();
echo $meta_values = get_post_meta( $postid, '_end_minute', true )."</br>";
echo $meta_values = get_post_meta( $postid, '_end_hour', true )."</br>";
echo $meta_values1 = get_post_meta( $postid, '_end_month', true )."</br>";
echo $meta_values2 = get_post_meta( $postid, '_end_day', true )."</br>";
echo $meta_values3 = get_post_meta( $postid, '_end_year', true )."</br>";
echo $meta_values3 = get_post_meta( $postid, '_end_eventtimestamp', true )."</br>";
global $wpdb;
foreach( $wpdb->get_results("SELECT * FROM wp_postmeta where meta_key='_end_eventtimestamp' And meta_value between '2015-07-01' and now() ;") as $key => $row) {
// each column in your row will be accessible like this
$my_column = $row->post_id;
//$wpdb->insert($wpdb->wp_term_relationships, array("object_id" => $row->post_id, "term_taxonomy_id" => 3, "term_order" => 0), array("%d", %d", "%d"));
$sql = $wpdb->prepare( "INSERT INTO $wpdb->wp_term_relationships (object_id, term_taxonomy_id,term_order ) VALUES ( %d, %d, %d )", $row->post_id, 3, 0 );
print_r($sql);
$res= $wpdb->query($sql);
//$wpdb->query($sql);
//echo "<br> My value = ".$my_column."<br>";
}
return $res;
}
add_action)('init','wp_insert_category_to_post');
It is written in post.php of wp-admin. But Its not working. can any one tell me the reason. And give the solution.

WordPress - How to fix WooCommerce breadcrumbs on shop category page?

In WooCommerce > Settings > Products, I have set "Shop Page Display" to "Show subcategories" - so that on my main shop page (http://example.com/shop/) only categories (and no individual products) are shown.
I have also used this code snippet to make the product categories show in my breadcrumbs as my theme uses WooTheme's "Simplicity" theme as its parent.
The problem I have is that the breadcrumbs are not displaying correctly. The breadcrumbs look fine on the shop home page...
You are here: Home > Products
But when I then click on a category from that page, the breadcrumbs change to...
You are here: Home > Chocolate
...when it should really be...
You are here: Home > Products > Chocolate
To confirm the issue, when I then click on a product, the breadcrumbs look fine again...
You are here: Home > Products > Chocolate > Vegan Chocolate bar
Does anyone know how I can fix the problematic breadcrumbs on the categories page?
As this seems like a bug, I have asked WooCommerce for their support, but they're not willing to fix it.
Thanks in advance.
Add this to your funtions.php file
// Breadcrumbs Display Category Name
// ====================================================================
function get_breadcrumb_category( $cat ) {
$post = get_post( $post->ID );
$post_type = $post->post_type;
$taxonomy = $cat;
$f_categories = wp_get_post_terms( $post->ID, $taxonomy );
$f_category = $f_categories[0];
if ( $f_category->parent != 0 ) {
$f_category_id = $f_category->parent;
$parent_array = get_term_by('id', $f_category_id, $taxonomy, 'ARRAY_A');
$f_category_name = $parent_array["name"];
$term_link = get_term_link( $f_category_id, $taxonomy );
} else {
$f_category_id = $f_category->term_id;
$f_category_name = $f_category->name;
$term_link = get_term_link( $f_category_id, $taxonomy );
}
if ( $f_categories && ! is_wp_error($f_categories) ) {
return '' . $f_category_name . '';
} else {
return '';
}
}
function x_breadcrumbs() {
if ( x_get_option( 'x_breadcrumb_display', '1' ) ) {
GLOBAL $post;
$is_ltr = ! is_rtl();
$stack = x_get_stack();
$delimiter = x_get_breadcrumb_delimiter();
$home_text = x_get_breadcrumb_home_text();
$home_link = home_url();
$current_before = x_get_breadcrumb_current_before();
$current_after = x_get_breadcrumb_current_after();
$page_title = get_the_title();
$blog_title = get_the_title( get_option( 'page_for_posts', true ) );
$post_parent = $post->post_parent;
if ( X_WOOCOMMERCE_IS_ACTIVE ) {
$shop_url = x_get_shop_link();
$shop_title = x_get_option( 'x_' . $stack . '_shop_title', __( 'The Shop', '__x__' ) );
$shop_link = '' . $shop_title . '';
}
echo '<div class="x-breadcrumbs">' . $home_text . '' . $delimiter;
if ( is_home() ) {
echo $current_before . $blog_title . $current_after;
} elseif ( is_category() ) {
$the_cat = get_category( get_query_var( 'cat' ), false );
if ( $the_cat->parent != 0 ) echo ''.get_the_title(102) .'';
echo $current_before . single_cat_title( '', false ) . $current_after;
} elseif ( x_is_product_category() ) {
if ( $is_ltr ) {
echo $shop_link . $delimiter . $current_before . single_cat_title( '', false ) . $current_after;
} else {
echo $current_before . single_cat_title( '', false ) . $current_after . $delimiter . $shop_link;
}
} elseif ( x_is_product_tag() ) {
if ( $is_ltr ) {
echo $shop_link . $delimiter . $current_before . single_tag_title( '', false ) . $current_after;
} else {
echo $current_before . single_tag_title( '', false ) . $current_after . $delimiter . $shop_link;
}
} elseif ( is_search() ) {
echo $current_before . __( 'Search Results for ', '__x__' ) . '“' . get_search_query() . '”' . $current_after;
} elseif ( is_singular( 'post' ) ) {
if ( get_option( 'page_for_posts' ) == is_front_page() ) {
echo $current_before . $page_title . $current_after;
} else {
if ( $is_ltr ) {
$f_category = get_the_category();
if ( $f_category[0]->parent != 0 ) {
$f_category_id = $f_category[0]->parent;
$f_category_name = get_cat_name( $f_category_id );
} else {
$f_category_id = $f_category[0]->term_id;
$f_category_name = $f_category[0]->name;
}
echo '' . $f_category_name . '' . $delimiter . $current_before . $page_title . $current_after;
} else {
echo $current_before . $page_title . $current_after . $delimiter . '' . $blog_title . '';
}
}
} elseif ( x_is_portfolio() ) {
echo $current_before . get_the_title() . $current_after;
} elseif ( x_is_portfolio_item() ) {
$link = x_get_parent_portfolio_link();
$title = x_get_parent_portfolio_title();
if ( $v = get_breadcrumb_category('portfolio-category') ) {
$portfolio_category = $delimiter . $v;
} else {
$portfolio_category = '';
}
if ( $is_ltr ) {
echo '' . $title . '' . $portfolio_category . $delimiter . $current_before . $page_title . $current_after;
} else {
echo $current_before . $page_title . $current_after . $portfolio_category . $delimiter . '' . $title . '';
}
} elseif ( x_is_product() ) {
if ( $v = get_breadcrumb_category('product_cat') ) {
$product_category = $delimiter . $v;
} else {
$product_category = '';
}
if ( $is_ltr ) {
echo $shop_link . $product_category . $delimiter . $current_before . $page_title . $current_after;
} else {
echo $current_before . $page_title . $current_after . $product_category . $delimiter . $shop_link;
}
} elseif ( x_is_buddypress() ) {
if ( bp_is_group() ) {
echo '' . x_get_option( 'x_buddypress_groups_title', __( 'Groups', '__x__' ) ) . '' . $delimiter . $current_before . x_buddypress_get_the_title() . $current_after;
} elseif ( bp_is_user() ) {
echo '' . x_get_option( 'x_buddypress_members_title', __( 'Members', '__x__' ) ) . '' . $delimiter . $current_before . x_buddypress_get_the_title() . $current_after;
} else {
echo $current_before . x_buddypress_get_the_title() . $current_after;
}
} elseif ( x_is_bbpress() ) {
remove_filter( 'bbp_no_breadcrumb', '__return_true' );
if ( bbp_is_forum_archive() ) {
echo $current_before . bbp_get_forum_archive_title() . $current_after;
} else {
echo bbp_get_breadcrumb();
}
add_filter( 'bbp_no_breadcrumb', '__return_true' );
} elseif ( is_page() && ! $post_parent ) {
echo $current_before . $page_title . $current_after;
} elseif ( is_page() && $post_parent ) {
$parent_id = $post_parent;
$breadcrumbs = array();
if ( is_rtl() ) {
echo $current_before . $page_title . $current_after . $delimiter;
}
while ( $parent_id ) {
$page = get_page( $parent_id );
$breadcrumbs[] = '' . get_the_title( $page->ID ) . '';
$parent_id = $page->post_parent;
}
if ( $is_ltr ) {
$breadcrumbs = array_reverse( $breadcrumbs );
}
for ( $i = 0; $i < count( $breadcrumbs ); $i++ ) {
echo $breadcrumbs[$i];
if ( $i != count( $breadcrumbs ) -1 ) echo $delimiter;
}
if ( $is_ltr ) {
echo $delimiter . $current_before . $page_title . $current_after;
}
} elseif ( is_tag() ) {
echo $current_before . single_tag_title( '', false ) . $current_after;
} elseif ( is_author() ) {
GLOBAL $author;
$userdata = get_userdata( $author );
echo $current_before . __( 'Posts by ', '__x__' ) . '“' . $userdata->display_name . $current_after . '”';
} elseif ( is_404() ) {
echo $current_before . __( '404 (Page Not Found)', '__x__' ) . $current_after;
} elseif ( is_archive() ) {
if ( x_is_shop() ) {
echo $current_before . $shop_title . $current_after;
} else {
echo $current_before . __( 'Archives ', '__x__' ) . $current_after;
}
}
echo '</div>';
}
}
starting from the } elseif ( x_is_product() ) { and finishing at the } elseif ( x_is_buddypress() ) {
} elseif ( x_is_product() ) {
$product_categories = wp_get_post_terms( get_the_ID(), 'product_cat' );
$parent = '';
$sub_category = '';
foreach ($product_categories as $category ) {
$term_id = $category->term_id;
$term_name = get_term( $term_id, 'product_cat' );
if ( $category->parent != 0 ) {
$sub_category .= $parent_id . '' . $term_name->name . '' . $delimiter ;
} else {
$parent .= '' . $term_name->name . '' . $delimiter ;
}
}
if ( $v = get_breadcrumb_category('product_cat') ) {
$product_category = $delimiter . $v;
} else {
$product_category = '';
}
if ( $is_ltr ) {
if(is_array($product_categories)){
echo $shop_link . $product_category . $delimiter . $sub_category . $current_before . $page_title . $current_after;
}
else {
echo $shop_link . $delimiter . $current_before . $page_title . $current_after;
}
} else {
echo $current_before . $page_title . $current_after . $delimiter . $shop_link;
}
} elseif ( x_is_buddypress() ) {

Categories