Remove ids from Wordpress form and form elements - wordpress

When i use this inside of the loop on my bloglist page:
<?php comment_form( array( 'post_id' => get_the_ID() ) ); ?>
I get a form for each post wich is exactly what I want.
My problem is that since the form itself and the input fields and textarea has ids, the page won't validate. I get errors for multiple ids naturally.
For example does all my forms have id="commentform"
How can I make Wordpress remove all ids in these forms?

You can pass more options to the function see below. Replace <INSERT-UNIQUE-VALUE> with something unique or you can auto generate it with php for example md5(time());
<?php
// Specify options
$options = array(
'id_form' => '<INSERT-UNIQUE-VALUE>',
'id_submit' => '<INSERT-UNIQUE-VALUE>',
'comment_field' => '<p class="comment-form-comment"><label for="<INSERT-UNIQUE-VALUE>">' . _x( 'Comment', 'noun' ) . '</label><textarea id="<INSERT-UNIQUE-VALUE>" name="comment" cols="45" rows="8" aria-required="true"></textarea></p>'
);
// output the form
comment_form($options, get_the_ID());
?>
For more references see: http://codex.wordpress.org/Function_Reference/comment_form#.24args

You could solve this with jquery by replacing your ID with a new ID and adding a number to your ids so the form id's would be id="newId0", id="newId1", id="newId2 and so on. Then it will validate.
$('form').each(function(index){
$(this).attr("id","newId"+index);
});
Here's a jsfiddle

Related

Get Post meta to display on invoice in WordPress

I'm using a plugin called woo-get to add a product attribute called 'prod_hsn_id' which add a filed called HSN code at product edit page,
I'm also using a pdf invoice plugin called woocommerce pdf invoice to generate pdf invoice.
Now I want to display the HSN code on the invoice.
I am having a very hard time to get it to work, I tried searching online and contacting the plugin author, and he said that it can be retrieved using the WordPress get post meta.
But
This the function the plugin use to create the custom field.
public function fn_add_product_custom_meta_box() {
woocommerce_wp_text_input(
array(
'id' => 'hsn_prod_id',
'label' => __('HSN Code', 'woocommerce' ),
'description' => __( 'HSN Code is mandatory for GST.', 'woocommerce' ),
'custom_attributes' => array( 'required' => 'required' ),
'value' => get_post_meta( get_the_ID(), 'hsn_prod_id', true )
)
);
}
In the Invoice Template file I'm trying to display the HSN Code using
<?php $meta = get_post_meta( $post_id , 'hsn_prod_id', true ); ?>
<?php $meta = get_post_meta( get_the_ID(), 'hsn_prod_id', true ); ?>
Sources :
Support
1. https://wordpress.org/support/topic/print-hsn-in-invoice/
https://wordpress.org/support/topic/which-pdf-plugin-will-display-the-hsn-field-and-gst-number/
Product details will be under line_items of order. Can you share your invoice template code where you are trying to access product id?
You will have order object, using that object you can retrieve all products related to that order. You need to execute on foreach loop (that should be there already).
You can see $product at line 77 of original template file of plugin. You can get id using $product->get_id().

Programmatically construct a dynamic dropdown field in Contact Form 7

I looked around here and by search engine, but unfortunately I couldn't find a solution for myself.
So, I now ask assistance with a function that I need to customize for the Contact Form 7 WordPress plugin. The function was from another question.
In a drop-down menu (select) I need two details (workshop name and date) in one option field. Both details come from the same post of a custom post type. The first detail is a post_title, the second is a custom-field from Meta-Box plugin.
The following function works in principle, but it only returns the one or the other detail. Probably the solution is within the foreach construct. But I don't know how it works.
I would be very grateful for support!
[UPDATE 2018-08-12]
After further research, I've found the solution at this post and changed the function accordingly.
The solution should look like this:
<select>
<option value="workshop name – date">workshop name – date</option>
...
</select>
This is the function:
add_filter( 'wpcf7_form_tag', 'dynamic_field_choose_workshop', 10, 2);
function dynamic_field_choose_workshop ( $tag, $unused ) {
if ( $tag['name'] != 'workshop' )
return $tag;
$args = array (
'post_type' => 'workshop',
'post_status' => 'publish',
'orderby' => 'name',
'order' => 'ASC',
'numberposts' => - 1,
);
$custom_posts = get_posts($args);
if ( ! $custom_posts )
return $tag;
foreach ( $custom_posts as $custom_post ) {
$ID = $custom_post->ID;
$tag['values'][] = $custom_post->post_title . ' - ' . rwmb_get_value('workshop_meta_boxes_date', '', $ID);
$tag['raw_values'][] = $custom_post->post_title . ' - ' . rwmb_get_value('workshop_meta_boxes_date', '', $ID);
$tag['labels'][] = $custom_post->post_title . ' - ' . rwmb_get_value('workshop_meta_boxes_date', '', $ID);
}
return $tag;
}
There is CF7 extension that will do this for you. Checkout the Smart Grid-Layout for CF7, it introduces a new tag called dynamic_dropdown. This is is what you want to use. The dynamic_dropdown creates a select field and allows you to populate the field options using either a taxonomy, titles of a post type, or a filter. You want to use the filter option to actually construct the options as per your requirement. The tag popup window is self explanatory, however if you get stuck post a comment below and I'll give you some more tips.
Using the following dynamic_dropdown tag,
[dynamic_select workshop-date-select class:select2 "source:filter"]
it creates a <select name="workshop-date-select"> dropdown field which will be converted into a select2 jquery field on the front end, and its values dynamically created using the following function placed in the functions.php file,
add_filter('cf7sg_dynamic_dropdown_custom_options', 'filter_options',10,3);
function filter_options($options, $field_name, $form_key){
/*first we verify if this is the right field from the right form
in case multiple forms with similar fieldd exiss.
the $form_key is a unique key exposed by the Smart Grid-layout plugin
instead of using form IDs to make forms and code more portable across servers.*/
if($form_key != 'my-form' && $field_name != 'workshop-date-select') return $options;
$options = array();
//load your options programmatically, as $value=>$name pairs.
$args = array (
'post_type' => 'workshop',
'post_status' => 'publish',
'orderby' => 'name',
'order' => 'ASC',
'numberposts' => - 1,
);
$workshops = get_posts( $args );
foreach($workshops as $workshop){
$val = $workshop->post_title . ' - ' . rwmb_get_value('workshop_meta_boxes_date', '', $workshop->ID);
$options[$val]=$val;
}
return $options;
}
this will create the desired dropdown select field in the front end.
NOTE of CAUTION: I would populate the option values as the workshop post ID rather than the same text as the option label. When the form is submitted the value of the post ID can be used to populate the notification email with the desired workshop title and date. This gives more flexibility to expand the reported information in the future.

Accessing Advanced Custom Fields by Page Name

I am trying to retrieve all advanced custom fields tied to a particular page. This is different than iterating through posts, I am familiar with the following:
$posts = get_posts(array(
'post_type' => 'post_name',
'meta_key' => 'color',
'meta_value' => 'red'
));
However this method is specific to posts and does not allow me to retrieve all ACF by page name.
I appreciate any suggestions on how to accomplish this.
The are too ways to do this that come to mind...
1. Using the Loop
Using WP_Query you can do something like this...
<?php
// WP_Query arguments
$args = array (
'pagename' => 'homepage',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
the_field( "field_name" );
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
?>
In place of the 'homepage' in 'pagename' => 'homepage', you want to put the page slug of your page. And of course in place of the_field( "text_field" ); you want to add your fields/content.
You can also query by Page ID and some other parameters. You can find all other parameters that you can use here:
https://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters
2. Adding second parameter to the_field() function
More simple way, without custom loop, is just to use ACF's built-in the_field() function with the $post->ID parameter added as a second parameter.
<?php the_field('field_name', 123);
This might be the way to go since you want to show the content of only one page, and therefore don't really need to loop.
Reference: http://www.advancedcustomfields.com/resources/how-to-get-values-from-another-post/
You can use ACF's get_fields() function -
<?php $fields = get_fields( $post->ID ); ?>
You could then loop through them or simply print the array for testing.
http://www.advancedcustomfields.com/resources/get_fields/

Search for Custom Field with Custom Post Types

There are tutorials that explain how to limit a search to a specific category.
My question is, is there a way to configure wordpress' search to, within a custom post type, search for a custom field value.
So for example, if I search for "hello", the results would come up with posts that have a certain custom field equal to "hello". The certain post would also be a certain custom post type.
Any help is appreciated.
To filter search by custom post type use:
<?php query_posts($query_string . '&post_type=custom-post-type-name' ); ?>
before the loop.. then within the loop add a condition similar to this
<?php if ($meta_data[ 'meta-name' ] == 'hello') {
//do something
} ?>
I think here is what you are looking for:
key is the custom field.
value is the value you are looking for
and compare is the operator you want to use.
you can also use LIKE if you want.
// WP_Query arguments
$args = array (
'post_type' => 'vendors',
'post_status' => 'published',
'meta_query' => array(
array(
'key' => 'state',
'value' => 'Misissipi',
'compare' => '=',
),
),
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// do something
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();

Wordpress - Custom fields & Class Identifiers

In Wordpress - how do you assign a class to custom fields? I am attempting to target multiple custom fields with custom CSS, but seem unable to give them separate classes.
This is probably not the BEST solution, but is worth a shot, I'd do a loop per field so your get_posts would look like this:
$args = array(
'meta_key' => 'custom_attribute',
'meta_value' => 'value1',
);
$value1 = get_posts($args);
$args = array(
'meta_key' => 'custom_attribute',
'meta_value' => 'value2',
);
$value2 = get_posts($args);
So that will give you 2 arrays, each one pulling different posts, from that you can do a foreach going through each array (value1 and value2) and adding classes as needed.
Thanks for your help.
I've since discovered a great bit of syntax that accomplishes the job.
<?php echo '<li class="address">' . get_post_meta($post->ID, 'Address', true) . '</li>'; ?>
This pulls in one single custom field at a time. In the example above, my custom field title was 'Address'. The will wrap the content from that field - thus accomplishing the task of wrapping each custom field in it's own class.

Resources