First of all, sorry for my lack of knowledge in PHP. I have tried googling for the answer, and also tried several answers here. For some reason passing argument in my function isn't working. However, if I directly call the default value in function, it works. Here is the function I am using.
function pnet_breadcrumbs( $seperator = '»', $home = true, $blog = true, $show_hierarchy = true ) {
global $post, $wp_query;
$html = '<div class="breadcrumbs" typeof="BreadcrumbList" vocab="http://schema.org/">';
$position = 1;
if ( $home ) {
$html .= '<span property="itemListElement" typeof="ListItem"><a property="item" typeof="WebPage" title="Go to ' . get_option( 'blogname' ) . '." href="' . home_url( '/' ) . '" class="home"><span property="name">' . get_option( 'blogname' ) . '</span></a><meta property="position" content="' . $position . '"></span>' . $seperator;
$html .= ' ' . $seperator . ' '; //Not Working
$html .= ' » '; //Working
$position = $position + 1;
}
if ( is_tax() || is_category() || is_tag() ) {
$obj = $wp_query->get_queried_object();
if ( $show_hierarchy ) {
$ancestors = get_ancestors( $obj->term_id, $obj->taxonomy, 'taxonomy' );
if ( is_array($ancestors) && !empty($ancestors) ) {
$ancestors = array_reverse( $ancestors );
foreach( $ancestors as $ancestor ) {
$term_obj = get_term($ancestor);
$term_url = get_term_link($term_obj->term_id);
$html .= '<span property="itemListElement" typeof="ListItem"><a property="item" typeof="WebPage" title="' . sprintf( __( 'View Archive for %s.', 'pnet' ), $term_obj->name ) . '." href="' . $term_url . '" class="home"><span property="name">' . $term_obj->name . '</span></a><meta property="position" content="' . $position . '"></span>';
$html .= $seperator;
$position++;
}
}
}
$html .= '<span property="itemListElement" typeof="ListItem"><span property="name">' . $obj->name . '</span><meta property="position" content="' . $position . '"></span>';
}
if ( is_search() ) {
$html .= '<span property="itemListElement" typeof="ListItem"><span property="name">' . sprintf( __( 'Search results for %s.', 'pnet' ), get_search_query() ) . '</span><meta property="position" content="' . $position . '"></span>';
}
$html .= '</div>';
echo $html;
}
add_action( 'wp_footer', 'pnet_breadcrumbs', 0 );
The variable $seperator is returning empty.
I know this is basic PHP, but I really can't figure out why I can't use a default value. What am I doing wrong?
Related
I have a problem. I use a filter with thumbnails. At least this filter does not include get category thumbnail and you have to upload all thumbnails manually into the filter which is not a good solution.
What I try to do is to get the thumbnail automatically from the category thumbnail. So far I have a code which adds a thumbnail from the current query which is also not a good solution. I need to get the thumbnail for each category separately.
This code works with current query and display the same image for all categories from the current opened category. Do you have any idea how could I get thumbnail for each category separately?
if ( is_product_category() ){
global $wp_query;
// get the query object
$cat = $wp_query->get_queried_object();
// get the thumbnail id using the queried category term_id
$thumbnail_id = get_term_meta( $cat->term_id, 'thumbnail_id', true );
// get the image URL
$image = wp_get_attachment_url( $thumbnail_id );
return '<span class="prdctfltr_customize_block prdctfltr_customize"><span class="prdctfltr_customize_image"><img src="' . $image . '" alt="' . $cat->name . '"/></span>' . ( $cnt !== false ? ' <span class="prdctfltr_customize_count">' . absint( $cnt ) . '</span>' : '' ) . ( $tip !== false ? '<span class="prdctfltr_tooltip"><span>' . wp_kses_post( $tip ) . '</span></span>' : '' ) . ( $checked !== '' ? '<input type="checkbox" value="' . esc_attr( $term_slug ) . '"' . esc_html( $checked ) . '/>' : '' ) . '<span class="prdctfltr_customization_search">' . esc_html( $term_name ) . '</span>' . wp_kses_post( $sublevel ) . '</span>';
}
Thank you in advance.
UPDATE
PHP CODE
Note: The following snippet has been fully tested on wordpress 5.8 and woocommerce 5.2 and it works for the default woocommerce product categories. If you're trying to use it in third party plugins, you may encounter some discrepancies. Therefore, feel free to customize it as you see fit!
If i understood you correctly, you need all of your product categories thumbnails.
You could get the all of your products categories by using get_terms. Then use a foreach loop to output your thumbnails. Like so:
$args = array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
);
$terms = get_terms($args);
foreach ($terms as $product_cat) {
$thumbnail_id = get_woocommerce_term_meta($product_cat->term_id, 'thumbnail_id', true);
$image = wp_get_attachment_url($thumbnail_id);
echo "<img src='" . $image . "'>";
}
This section is related to the plugin used in the question!
UPDATE
Like i said in the comments, the php file that you provided is one giant file with "4634" lines of "spaghetti" code with multiple references to other external files. However, on line 1957, there is a function that seems what we're interested in, and called "get_customized_term_700" and takes in $term_id, $term_slug, $term_name, $cnt, $checked and $sublevel.
In the same function, there is also a switch case statement. One of the case conditions checks for image. If there is a filter, it'll output the categories. So we'll use it to output the image.
So go ahead and REPLACE the entire function called "get_customized_term_700" on line 1957 with the following function.
Note:
This has not been tested, since i don't have access to the plugin, this has been purely written based on the logic in the php file. Hope it helps you!
public static function get_customized_term_700($term_id, $term_slug, $term_name, $cnt, $checked = '', $sublevel = '')
{
if (!empty($term_id)) {
$data = array(
'tooltip' => '',
'data' => '',
);
if (!empty(self::$filter['style']['terms'])) {
$key = self::__find_customized_term($term_id, self::$filter['style']['terms']);
if ($key !== false) {
$data = array_merge(array(
'tooltip' => '',
'data' => '',
), self::$filter['style']['terms'][$key]);
}
}
} else {
$data = array(
'value' => '',
'title' => self::__get_none_string(),
'tooltip' => self::__get_none_tooltip_string(),
'data' => self::__get_customized_term_none(self::$filter['style']['style']['type']),
);
}
if (!empty($data['title'])) {
$term_name = $data['title'];
}
$tip = empty($data['tooltip']) ? false : $data['tooltip'];
switch (self::$filter['style']['style']['type']) {
case 'text':
return '<span class="prdctfltr_customize_' . esc_attr(self::$filter['style']['style']['css']) . ' prdctfltr_customize"><span class="prdctfltr_customize_name">' . esc_html($term_name) . '</span>' . ($cnt !== false ? ' <span class="prdctfltr_customize_count">' . absint($cnt) . '</span>' : '') . ($tip !== false ? '<span class="prdctfltr_tooltip"><span>' . wp_kses_post($tip) . '</span></span>' : '') . ($checked !== '' ? '<input type="checkbox" value="' . esc_attr($term_slug) . '"' . esc_html($checked) . '/>' : '') . wp_kses_post($sublevel) . '</span>';
break;
case 'color':
if (!empty(self::$filter['style']['label']) && self::$filter['style']['label'] == 'side') {
return '<span class="prdctfltr_customize_block prdctfltr_customize"' . (!empty(self::$filter['style']['size']) ? sprintf(' style="line-height:%1$spx;', absint(self::$filter['style']['size'])) : '') . '><span class="prdctfltr_customize_color_text"><span style="background-color:' . Prdctfltr()->esc_color($data['data']) . ';' . (!empty(self::$filter['style']['size']) ? sprintf('width:%1$spx;height:%1$spx;', absint(self::$filter['style']['size'])) : '') . '"></span></span>' . ($tip !== false ? '<span class="prdctfltr_tooltip"><span>' . wp_kses_post($tip) . '</span></span>' : '') . ($checked !== '' ? '<input type="checkbox" value="' . esc_attr($term_slug) . '"' . esc_html($checked) . '/>' : '') . '<span class="prdctfltr_customization_search">' . esc_html($term_name) . '</span><span class="prdctfltr_customize_color_text_tip">' . esc_html($term_name) . '</span>' . ($cnt !== false ? ' <span class="prdctfltr_count">' . absint($cnt) . '</span>' : '') . wp_kses_post($sublevel) . '</span>';
} else {
return '<span class="prdctfltr_customize_block prdctfltr_customize"><span class="prdctfltr_customize_color" style="background-color:' . Prdctfltr()->esc_color($data['data']) . ';' . (!empty(self::$filter['style']['size']) ? sprintf('width:%1$spx;height:%1$spx;', absint(self::$filter['style']['size'])) : '') . '"></span>' . ($cnt !== false ? ' <span class="prdctfltr_customize_count">' . absint($cnt) . '</span>' : '') . ($tip !== false ? '<span class="prdctfltr_tooltip"><span>' . wp_kses_post($tip) . '</span></span>' : '') . ($checked !== '' ? '<input type="checkbox" value="' . esc_attr($term_slug) . '"' . esc_html($checked) . '/>' : '') . '<span class="prdctfltr_customization_search">' . esc_html($term_name) . '</span>' . wp_kses_post($sublevel) . '</span>';
}
break;
case 'image':
if (!empty(self::$filter['style']['label']) && self::$filter['style']['label'] == 'side') {
$args = array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
);
$output = '';
$terms = get_terms($args);
foreach ($terms as $product_cat) {
$thumbnail_id = get_woocommerce_term_meta($product_cat->term_id, 'thumbnail_id', true);
$image = wp_get_attachment_url($thumbnail_id);
$output .= '<span class="prdctfltr_customize_block prdctfltr_customize"' . (!empty(self::$filter['style']['size']) ? sprintf(' style="line-height:%1$spx;"', absint(self::$filter['style']['size'])) : '') . '><span class="prdctfltr_customize_image_text"><img src="' . $image . '"/></span>' . ($tip !== false ? '<span class="prdctfltr_tooltip"><span>' . wp_kses_post($tip) . '</span></span>' : '') . ($checked !== '' ? '<input type="checkbox" value="' . esc_attr($product_cat->slug) . '"' . esc_html($checked) . '/>' : '') . '<span class="prdctfltr_customization_search">' . esc_html($product_cat->name) . '</span><span class="prdctfltr_customize_image_text_tip">' . esc_html($product_cat->name) . '</span>' . ($cnt !== false ? ' <span class="prdctfltr_count">' . absint($cnt) . '</span>' : '') . wp_kses_post($sublevel) . '</span>';
}
return $output;
}
break;
case 'select':
return '<span class="prdctfltr_customize_select prdctfltr_customize">' . ($checked !== '' ? '<input type="checkbox" value="' . esc_attr($term_slug) . '"' . esc_html($checked) . '/>' : '') . '<span class="prdctfltr_customize_name">' . esc_html($term_name) . '</span>' . ($cnt !== false ? ' <span class="prdctfltr_customize_count">' . absint($cnt) . '</span>' : '') . wp_kses_post($sublevel) . '</span>' . ($tip !== false ? '<span class="prdctfltr_tooltip"><span>' . wp_kses_post($tip) . '</span></span>' : '');
break;
case 'html':
if (!empty($data['data'])) {
return wp_kses_post(stripslashes($data['data'])) . '<span class="prdctfltr_customization_search">' . esc_html($term_name) . '</span>' . ($tip !== false ? '<span class="prdctfltr_tooltip"><span>' . wp_kses_post($tip) . '</span>' . wp_kses_post($sublevel) . '</span>' : '');
} else {
return esc_html($term_name);
}
break;
default:
return '';
break;
}
}
I'm new to PHP, still learning, so please excuse and kindly tell me what would be the appropriate fix. I used the breadcrumb script from here answered by Pieter Goosen. I want the breadcrumbs to work with google and schema, the part that isn't working is the <meta itemprop="position" content="number" /> tag. The content="number" needs to be set for each crumb, in order (1,2,3...). On line 8 I added $i = 1;, then changed $link_after = '<meta itemprop="position" content="' . $i++ . '" /></li>';. But $i++ is not printing a new sequential number, it is just repeating itself as if there was no other $i++. My guess is to apply the rule globally and to search for $i++ used, as it's probably being applied closed within the rule, but wouldn't know how to do this.
Code:
function get_hansel_and_gretel_breadcrumbs()
{
// Set variables for later use
$here_text = __( 'You are currently here!' );
$home_link = home_url('/');
$home_text = __( 'Home' );
$link_before = '<span typeof="v:Breadcrumb">';
$i = 1;
$link_after = '<meta itemprop="position" content="' . $i++ . '" /></span>'
$link_attr = ' rel="v:url" property="v:title"';
$link = $link_before . '<a' . $link_attr . ' href="%1$s">%2$s</a>' . $link_after;
$delimiter = ' » '; // Delimiter between crumbs
$before = '<span class="current">'; // Tag before the current crumb
$after = '</span>'; // Tag after the current crumb
$page_addon = ''; // Adds the page number if the query is paged
$breadcrumb_trail = '';
$category_links = '';
/**
* Set our own $wp_the_query variable. Do not use the global variable version due to
* reliability
*/
$wp_the_query = $GLOBALS['wp_the_query'];
$queried_object = $wp_the_query->get_queried_object();
// Handle single post requests which includes single pages, posts and attatchments
if ( is_singular() )
{
/**
* Set our own $post variable. Do not use the global variable version due to
* reliability. We will set $post_object variable to $GLOBALS['wp_the_query']
*/
$post_object = sanitize_post( $queried_object );
// Set variables
$title = apply_filters( 'the_title', $post_object->post_title );
$parent = $post_object->post_parent;
$post_type = $post_object->post_type;
$post_id = $post_object->ID;
$post_link = $before . $title . $after;
$parent_string = '';
$post_type_link = '';
if ( 'post' === $post_type )
{
// Get the post categories
$categories = get_the_category( $post_id );
if ( $categories ) {
// Lets grab the first category
$category = $categories[0];
$category_links = get_category_parents( $category, true, $delimiter );
$category_links = str_replace( '<a', $link_before . '<a' . $link_attr, $category_links );
$category_links = str_replace( '</a>', '</a>' . $link_after, $category_links );
}
}
if ( !in_array( $post_type, ['post', 'page', 'attachment'] ) )
{
$post_type_object = get_post_type_object( $post_type );
$archive_link = esc_url( get_post_type_archive_link( $post_type ) );
$post_type_link = sprintf( $link, $archive_link, $post_type_object->labels->singular_name );
}
// Get post parents if $parent !== 0
if ( 0 !== $parent )
{
$parent_links = [];
while ( $parent ) {
$post_parent = get_post( $parent );
$parent_links[] = sprintf( $link, esc_url( get_permalink( $post_parent->ID ) ), get_the_title( $post_parent->ID ) );
$parent = $post_parent->post_parent;
}
$parent_links = array_reverse( $parent_links );
$parent_string = implode( $delimiter, $parent_links );
}
// Lets build the breadcrumb trail
if ( $parent_string ) {
$breadcrumb_trail = $parent_string . $delimiter . $post_link;
} else {
$breadcrumb_trail = $post_link;
}
if ( $post_type_link )
$breadcrumb_trail = $post_type_link . $delimiter . $breadcrumb_trail;
if ( $category_links )
$breadcrumb_trail = $category_links . $breadcrumb_trail;
}
// Handle archives which includes category-, tag-, taxonomy-, date-, custom post type archives and author archives
if( is_archive() )
{
if ( is_category()
|| is_tag()
|| is_tax()
) {
// Set the variables for this section
$term_object = get_term( $queried_object );
$taxonomy = $term_object->taxonomy;
$term_id = $term_object->term_id;
$term_name = $term_object->name;
$term_parent = $term_object->parent;
$taxonomy_object = get_taxonomy( $taxonomy );
$current_term_link = $before->labels->singular_name . '<li class="curr-cat" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><span itemprop="name">' . $term_name . '</span><meta itemprop="position" content="' . $i++ . '" /></li>';
$parent_term_string = '';
if ( 0 !== $term_parent )
{
// Get all the current term ancestors
$parent_term_links = [];
while ( $term_parent ) {
$term = get_term( $term_parent, $taxonomy );
$parent_term_links[] = sprintf( $link, esc_url( get_term_link( $term ) ), $term->name );
$term_parent = $term->parent;
}
$parent_term_links = array_reverse( $parent_term_links );
$parent_term_string = implode( $delimiter, $parent_term_links );
}
if ( $parent_term_string ) {
$breadcrumb_trail = $parent_term_string . $delimiter . $current_term_link;
} else {
$breadcrumb_trail = $current_term_link;
}
} elseif ( is_author() ) {
$breadcrumb_trail = __( 'Author archive for ') . $before . $queried_object->data->display_name . $after;
} elseif ( is_date() ) {
// Set default variables
$year = $wp_the_query->query_vars['year'];
$monthnum = $wp_the_query->query_vars['monthnum'];
$day = $wp_the_query->query_vars['day'];
// Get the month name if $monthnum has a value
if ( $monthnum ) {
$date_time = DateTime::createFromFormat( '!m', $monthnum );
$month_name = $date_time->format( 'F' );
}
if ( is_year() ) {
$breadcrumb_trail = $before . $year . $after;
} elseif( is_month() ) {
$year_link = sprintf( $link, esc_url( get_year_link( $year ) ), $year );
$breadcrumb_trail = $year_link . $delimiter . $before . $month_name . $after;
} elseif( is_day() ) {
$year_link = sprintf( $link, esc_url( get_year_link( $year ) ), $year );
$month_link = sprintf( $link, esc_url( get_month_link( $year, $monthnum ) ), $month_name );
$breadcrumb_trail = $year_link . $delimiter . $month_link . $delimiter . $before . $day . $after;
}
} elseif ( is_post_type_archive() ) {
$post_type = $wp_the_query->query_vars['post_type'];
$post_type_object = get_post_type_object( $post_type );
$breadcrumb_trail = $before . $post_type_object->labels->singular_name . $after;
}
}
// Handle the search page
if ( is_search() ) {
$breadcrumb_trail = __( 'Search query for: ' ) . $before . get_search_query() . $after;
}
// Handle 404's
if ( is_404() ) {
$breadcrumb_trail = $before . __( 'Error 404' ) . $after;
}
// Handle paged pages
if ( is_paged() ) {
$current_page = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : get_query_var( 'page' );
$page_addon = $before . sprintf( __( ' ( Page %s )' ), number_format_i18n( $current_page ) ) . $after;
}
$breadcrumb_output_link = '';
$breadcrumb_output_link .= '<div class="breadcrumb">';
if ( is_home()
|| is_front_page()
) {
// Do not show breadcrumbs on page one of home and frontpage
if ( is_paged() ) {
$breadcrumb_output_link .= $here_text . $delimiter;
$breadcrumb_output_link .= '' . $home_text . '';
$breadcrumb_output_link .= $page_addon;
}
} else {
$breadcrumb_output_link .= $here_text . $delimiter;
$breadcrumb_output_link .= '' . $home_text . '';
$breadcrumb_output_link .= $delimiter;
$breadcrumb_output_link .= $breadcrumb_trail;
$breadcrumb_output_link .= $page_addon;
}
$breadcrumb_output_link .= '</div><!-- .breadcrumbs -->';
return $breadcrumb_output_link;
}
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 )
Im trying to put a conditional into a function. this is the original code
function do_meta_box( $meta_field_def, $key = '' ) {
$content = '';
$esc_form_key = esc_attr( self::$form_prefix . $key );
$post = $this->get_metabox_post();
$meta_value = self::get_value( $key, $post->ID );
// other stuff
$content .= '<input type="text"' . $placeholder . ' id="' . $esc_form_key . '" ' . $ac . 'name="' . $esc_form_key . '" value="' . esc_attr( $meta_value ) . '" class="large-text' . $class . '"/><br />';
// other sutff
}
Now i'm trying to put a conditional where the value is.
$my_custom_title = get_the_title() . ' My Custom Stuff';
if ( $post->post_type == 'post' ) { echo esc_attr( $my_custom_title ); } else { echo esc_attr( $meta_value ); };
Exactly right here: value="' . esc_attr( $meta_value ) . '"
Thanks in advance.
You can assign it to a variable...
$value = $post->post_type == 'post' ? esc_attr( $post->title ) : esc_attr( $meta_value );
$content .= '<input type="text"' . $placeholder . ' id="' . $esc_form_key . '" ' . $ac . 'name="' . $esc_form_key . '" value="' . $value . '" class="large-text' . $class . '"/><br />';
I am using top 10 -popular post plugin.
I added the following lines but it wont display...
$output .= '<li>' .wpautop(wp_trim_words($result["post_content"], 15)). '</li>';
code:
function tptn_pop_posts( $args ) {
global $wpdb, $siteurl, $tableposts, $id, $tptn_settings;
$defaults = array(
'is_widget' => FALSE,
'daily' => FALSE,
'echo' => FALSE,
'strict_limit' => FALSE,
'posts_only' => FALSE,
'is_shortcode' => FALSE,
'heading' => 1,
);
$defaults = array_merge( $defaults, $tptn_settings );
// Parse incomming $args into an array and merge it with $defaults
$args = wp_parse_args( $args, $defaults );
// OPTIONAL: Declare each item in $args as its own variable i.e. $type, $before.
extract( $args, EXTR_SKIP );
if ($daily) {
$table_name = $wpdb->prefix . "top_ten_daily";
} else {
$table_name = $wpdb->prefix . "top_ten";
}
$limit = ( $strict_limit ) ? $limit : ( $limit * 5 );
$exclude_categories = explode( ',', $exclude_categories );
$target_attribute = ( $link_new_window ) ? ' target="_blank" ' : ' '; // Set Target attribute
$rel_attribute = ( $link_nofollow ) ? ' nofollow' : ''; // Set nofollow attribute
parse_str( $post_types, $post_types ); // Save post types in $post_types variable
if ( ! $daily ) {
$args = array();
$sql = "SELECT postnumber, cntaccess as sumCount, ID, post_type, post_status ";
$sql .= "FROM {$table_name} INNER JOIN ". $wpdb->posts ." ON postnumber=ID " ;
$sql .= "AND post_status = 'publish' ";
if ( '' != $exclude_post_ids ) {
$sql .= "AND ID NOT IN ({$exclude_post_ids}) ";
}
$sql .= "AND ( ";
$multiple = false;
foreach ( $post_types as $post_type ) {
if ( $multiple ) $sql .= ' OR ';
$sql .= " post_type = '%s' ";
$multiple = true;
$args[] = $post_type; // Add the post types to the $args array
}
$sql .= " ) ";
$sql .= "ORDER BY sumCount DESC LIMIT %d";
$args[] = $limit;
} else {
$current_time = current_time( 'timestamp', 0 );
$current_time = $current_time - ( $daily_range - 1 ) * 3600 * 24;
$current_date = date( 'Y-m-j', $current_time );
$args = array(
$current_date,
);
$sql = "SELECT postnumber, SUM(cntaccess) as sumCount, dp_date, ID, post_type, post_status ";
$sql .= "FROM {$table_name} INNER JOIN ". $wpdb->posts ." ON postnumber=ID " ;
$sql .= "AND post_status = 'publish' AND dp_date >= '%s' ";
if ( '' != $exclude_post_ids ) {
$sql .= "AND ID NOT IN ({$exclude_post_ids}) ";
}
$sql .= "AND ( ";
$multiple = false;
foreach ( $post_types as $post_type ) {
if ( $multiple ) $sql .= ' OR ';
$sql .= " post_type = '%s' ";
$multiple = true;
$args[] = $post_type; // Add the post types to the $args array
}
$sql .= " ) ";
$sql .= "GROUP BY postnumber ";
$sql .= "ORDER BY sumCount DESC LIMIT %d";
$args[] = $limit;
}
if ( $posts_only ) { // Return the array of posts only if the variable is set
return apply_filters( 'tptn_pop_posts_array', $wpdb->get_results( $wpdb->prepare( $sql , $args ) , ARRAY_A ) );
}
$results = $wpdb->get_results( $wpdb->prepare( $sql , $args ) );
$counter = 0;
$output = '';
$shortcode_class = $is_shortcode ? ' tptn_posts_shortcode' : '';
$widget_class = $is_widget ? ' tptn_posts_widget' : '';
if ( $heading ) {
if ( ! $daily ) {
$output .= '<div id="tptn_related" class="tptn_posts ' . $widget_class . $shortcode_class . '">' . apply_filters( 'tptn_heading_title', $title );
} else {
$output .= '<div id="tptn_related_daily" class="tptn_posts_daily' . $shortcode_class . '">' . apply_filters( 'tptn_heading_title', $title_daily );
}
} else {
if ( ! $daily ) {
$output .= '<div class="tptn_posts' . $widget_class . $shortcode_class . '">';
} else {
$output .= '<div class="tptn_posts_daily' . $widget_class . $shortcode_class . '">';
}
}
if ( $results ) {
$output .= apply_filters( 'tptn_before_list', $before_list );
foreach ( $results as $result ) {
$sumcount = $result->sumCount;
$result = get_post( apply_filters( 'tptn_post_id', $result->ID ) ); // Let's get the Post using the ID
$categorys = get_the_category( apply_filters( 'tptn_post_cat_id', $result->ID ) ); //Fetch categories of the plugin
$p_in_c = false; // Variable to check if post exists in a particular category
foreach ( $categorys as $cat ) { // Loop to check if post exists in excluded category
$p_in_c = ( in_array( $cat->cat_ID, $exclude_categories ) ) ? true : false;
if ( $p_in_c ) break; // End loop if post found in category
}
$title = tptn_max_formatted_content( get_the_title( $result->ID ), $title_length );
if ( ! $p_in_c ) {
$output .= apply_filters( 'tptn_before_list_item', $before_list_item, $result->ID );
if ( 'after' == $post_thumb_op ) {
$output .= '<a href="' . get_permalink( $result->ID ) . '" rel="bookmark' . $rel_attribute . '" ' . $target_attribute . 'class="tptn_link">'; // Add beginning of link
$output .= '<span class="tptn_title">' . $title . '</span>'; // Add title if post thumbnail is to be displayed after
$output .= '</a>'; // Close the link
}
if ( 'inline' == $post_thumb_op || 'after' == $post_thumb_op || 'thumbs_only' == $post_thumb_op ) {
$output .= '<a href="' . get_permalink( $result->ID ) . '" rel="bookmark' . $rel_attribute . '" ' . $target_attribute . 'class="tptn_link">'; // Add beginning of link
$output .= tptn_get_the_post_thumbnail( array(
'postid' => $result->ID,
'thumb_height' => $thumb_height,
'thumb_width' => $thumb_width,
'thumb_meta' => $thumb_meta,
'thumb_html' => $thumb_html,
'thumb_default' => $thumb_default,
'thumb_default_show' => $thumb_default_show,
'thumb_timthumb' => $thumb_timthumb,
'thumb_timthumb_q' => $thumb_timthumb_q,
'scan_images' => $scan_images,
'class' => "tptn_thumb",
'filter' => "tptn_postimage",
) );
$output .= '</a>'; // Close the link
}
if ( 'inline' == $post_thumb_op || 'text_only' == $post_thumb_op ) {
$output .= '<span class="tptn_after_thumb">';
$output .= '<a href="' . get_permalink( $result->ID ) . '" rel="bookmark' . $rel_attribute . '" ' . $target_attribute . 'class="tptn_link">'; // Add beginning of link
$output .= '<span class="tptn_title">' . $title . '</span>'; // Add title when required by settings
// $output .= '<li>' .wpautop(wp_trim_words($result["post_content"], 15)). '</li>';
$output .= '</a>'; // Close the link
}
if ( $show_author ) {
$author_info = get_userdata( $result->post_author );
$author_name = ucwords( trim( stripslashes( $author_info->display_name ) ) );
$author_link = get_author_posts_url( $author_info->ID );
$output .= '<span class="tptn_author"> ' . __( ' by ', TPTN_LOCAL_NAME ).'' . $author_name . '</span> ';
}
if ( $show_date ) {
$output .= '<span class="tptn_date"> ' . mysql2date( get_option( 'date_format', 'd/m/y' ), $result->post_date ).'</span> ';
}
if ( $show_excerpt ) {
$output .= '<span class="tptn_excerpt"> ' . tptn_excerpt( $result->ID, $excerpt_length ).'</span>';
}
if ( $disp_list_count ) $output .= ' <span class="tptn_list_count">(' . number_format_i18n( $sumcount ) . ')</span>';
if ( 'inline' == $post_thumb_op || 'text_only' == $post_thumb_op ) {
$output .= '</span>';
}
$output .= apply_filters( 'tptn_after_list_item', $after_list_item, $result->ID );
$counter++;
}
if ( $counter == $limit/5 ) break; // End loop when related posts limit is reached
}
if ( $show_credit ) {
$output .= apply_filters( 'tptn_before_list_item', $before_list_item, $result->ID );
$output .= sprintf( 'Popular posts by Top 10 plugin', TPTN_LOCAL_NAME );
$output .= apply_filters( 'tptn_after_list_item', $after_list_item, $result->ID );
}
$output .= apply_filters( 'tptn_after_list', $after_list );
} else {
$output .= ( $blank_output ) ? '' : $blank_output_text;
}
$output .= '</div>';
return apply_filters( 'tptn_pop_posts', $output );
}
You seem to be returning $output. You will need to use echo in order for your HTML to display.
Ref: http://php.net/manual/en/function.echo.php
to show content in popular post by adding the following line below the code.
$output .= '<li><p>' .wp_trim_words(get_post($result->ID)->post_content,15). '</p></li>';
top10.php (top10 popular post plugin)
code:
if ( 'inline' == $post_thumb_op || 'text_only' == $post_thumb_op ) {
$output .= '<span class="tptn_after_thumb">';
$output .= '<a href="' . get_permalink( $result->ID ) . '" rel="bookmark' . $rel_attribute . '" ' . $target_attribute . 'class="tptn_link">'; // Add beginning of link
$output .= '<span class="tptn_title">' . $title . '</span>'; // Add title when required by settings
$output .= '</a>'; // Close the link
$output .= '<li><p>' .wp_trim_words(get_post($result->ID)->post_content,15). '</p></li>';
}