Array from form - Wordpress Metadata - php

I got a custom post type, with a form for storing some data (name, url) to display in a template.
What I want to know is how can I store those values in an array?
An example of my code:
<? function files_metadata(){
global $post;
$custom = get_post_custom($post->ID);
$name = $custom["name"][0];
$url = $custom["url"][0];
echo '<input type="hidden" name="files_metadata" id="files_metadata" value="' .wp_create_nonce('files_m'). '" />'; ?>
<label>Name: </label><br/>
<input id="name" name="name" value="<?php echo $name; ?>" />
<label>Url: </label><br/>
<input id="url" name="url" value="<?php echo $url; ?>" />
<? function save_meta_files($post_id) {
if (!wp_verify_nonce($_POST['files_metadata'], 'files_m')) return $post_id;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
update_post_meta($post_id, "name", $_POST["url"]);
update_post_meta($post_id, "url", $_POST["url"]);
}
add_action('save_post', 'save_meta_files'); ?>
To this I want to add something like...
$url = $custom["url"][0];
$url2 = $custom["url"][1];
$url3 = $custom["url"][2];
<input id="url" name="url[0]" value="<?php echo $url; ?>" />
<input id="url2" name="url[1]" value="<?php echo $url2; ?>" />
<input id="url3" name="url[2]" value="<?php echo $url3; ?>" />
update_post_meta($post_id, "url", $_POST["url"][0]);
update_post_meta($post_id, "url2", $_POST["url"][1]);
update_post_meta($post_id, "url3", $_POST["url"][2]);
...but that actually works...

A passed array will be serialized into a string: http://codex.wordpress.org/Function_Reference/update_post_meta
update_post_meta(
$post_id,
'files_metadata',
array(
'name1' => $_POST['name1'],
'url1' => $_POST['url1']
'name2' => $_POST['name2'],
'url2' => $_POST['url2']
)
);

Related

Wp update post not updating

I'm trying to update a post title using wp_update_post()
First I get the ID of the post:
global $post;
$post_to_edit = get_post($_GET['post_id']);
Then the form to update which it actually gets the title from the post to edit:
<form action="" id="primaryPostForm" method="POST">
<label for="postTitle">Title of your post</label>
<input type="text" name="postTitle" id="postTitle" class="form-control" value="<?php echo esc_html( $post_to_edit->post_title ); ?> ">
<input type="hidden" name="postId" id="postId" value="<?php echo $_GET['post_id']; ?>">
<input type="submit" name="submit_post" value="Update">
</form>
Then below on the same page I have:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['submit_post'])) {
$data = array(
'ID' => $_POST['postId'],
'post_title' => $_POST('postTitle')
);
wp_update_post( $data );
}
}
?>
But it's not updating the post title.
Your code is wrong,
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['submit_post'])) {
$data = array(
'ID' => $_POST['postId'],
'post_title' => $_POST['postTitle'] //you used wrong brackets
);
wp_update_post( $data );
}
}
?>

How to pass multiple checkbox values to Wordpress Search? custom_tax[]=val1&custom_tax[]=val2

I am trying to build a custom search form in Wordpress where the user would be able to select custom taxonomies to filter the posts against.
I am unfortunately getting this warning:
Warning: urlencode() expects parameter 1 to be string, array given in /wordpress/wp-includes/formatting.php on line 5343, which is this function:
function wp_basename( $path, $suffix = '' ) {
return urldecode( basename( str_replace( array( '%2F', '%5C' ), '/', urlencode( $path ) ), $suffix ) );
}
Here is my code of the form:
<form action="/" method="get">
<?php $post_type = !empty( $_GET['post_type'] ) ? $_GET['post_type'] : 'company'; ?>
<label for="search" class="block"><?php echo __('Search', 'mydomain'); ?>:</label>
<input type="radio" name="post_type" value="company" <?php echo $post_type == 'company' ? 'checked' : ''; ?>>Companies</input>
<input type="radio" name="post_type" value="auditor" <?php echo $post_type == 'auditor' ? 'checked' : ''; ?>>Auditors</input>
<input type="text" name="s" id="search" value="<?php the_search_query(); ?>" class="block" placeholder="<?php echo __('Type here'); ?>" />
<div>
<?php $certificates = get_terms([
'taxonomy' => 'certificate',
'hide_empty' => false
]);
foreach($certificates as $certificate) :
?>
<input type="checkbox" name="certificate[]" id="certificate-<?php echo $certificate->slug; ?>" value="<?php echo $certificate->slug; ?>" <?php echo (!empty($_GET['certificate']) && in_array($certificate->slug, $_GET['certificate']) ) ? 'checked' : ''; ?>> <?php echo $certificate->name; ?>
<?php
endforeach;
?>
</div>
<input type="hidden" value="<?php echo pll_current_language(); ?>" name="lang" id="lang" />
<input type="submit" value="<?php echo __('Search'); ?>" />
</form>
and this is how I attempt to see the search results:
http://wordpress.local/?post_type=auditor&s=&certificate%5B%5D=iso-22000&certificate[]=iso-9001
Any ideas how to bypass this warning and still submit the checkboxes as an array? (custom_tax[])

Php Form inputs in email

I created a form which will be entered by user
<form id="wc-form-return" action="" method="post">
<label><?php _e('Select products for return','wc_return') ?></label>
<select id="wc_products[]" name="wc_products" class="wc_products" multiple="multiple">
<?php
if ( sizeof( $products ) > 0 ) {
foreach( $products as $item ) { ?>
<option value="<?php echo $item['item_meta']['_product_id'][0]; ?>"><?php echo __(esc_html($item['name']), 'wc_return'); ?></option>
<?php
}
}
?>
</select>
<small><?php _e('You can select multiple by holding down the CMD or Ctrl key.','wc_return'); ?></small>
<textarea name="wc_message" id="wc_message" cols="30" rows="10" placeholder="<?php _e('Explain the reasons for your return', 'wc_return') ?>"></textarea>
<input type="hidden" name="order" value="<?php echo $order->id; ?>" />
<input type="hidden" name="customer" value="<?php echo $order->billing_email; ?>" />
<input type="text" name="phone" id="wc_phone" value="<?php echo $order->billing_phone; ?>" />
<input type="checkbox" name="check1" value="Yes" required>Accept our return and exchange policy </br>
<input type="submit" name="submit" value="<?php _e('Submit','wc_return'); ?>" />
</form>
It will be entered by user and we will be sent to given email (that is already setted up)
Now to give these information in email i used
$note_form_email = '';
if ( $_POST['wc_message'] != '' ) {
$note .= '<br><br>';
$note .= '<p>'.__('And the explanation:', 'wc_return').'</p>';
$note_form_email .= '<p>'.__('And the explanation:', 'wc_return').'</p>';
$note .= apply_filters( 'the_content', $_POST['wc_message'] );
$note_form_email .= apply_filters( 'the_content', $_POST['wc_message'] );
$note .= '<p>'.__('Customer details:', 'wc_return').'</p>';
$note_form_email .= '<p>'.__('Customer details:', 'wc_return').'</p>';
$note .= '<p>'.__('Customer phone:','wc_return').'</p>';
$note_form_email .= '<p>'.__('Customer phone:','wc_return').'</p>';
$note .= apply_filters('the_content', $_POST['wc_phone'] );
$note_form_email .= apply_filters('the_content', $_POST['wc_phone'] );
}
But output was like this
And the explanation:
Huhhh
Customer details:
Customer phone:
That 'Huhhh' was written by user on wc_message. He also wrote wc_phone but it was not showing in email. Wc_message is working properly but wc_phone isnt. What am i doing wrong?

array blowing my mind

$va_fields = array();
$c = 0;
$field_num = 'field-'.$c;
settype($c,"integer");
while (isset($_POST[$field_num])){
$posted_field = $_POST[$field_num];
$va_fields = array( $c => $posted_field);
echo $c.': ';
echo '$posted_field: '.$posted_field;
$c+=1;
$field_num = 'field-'.$c;
echo ' <br /> va_fields - c:'.$va_fields[$c];
echo ' <br /> ';
}
For some reason, I cannot for the life of me get the variable $posted_fields into an array.
The values of $posted_field are what I need them to be, so I'm getting the data from the post. Then I try to store them in an array, and check the array and they aren't there. What am I doing wrong?
Edit: here's my form:
<form method="post" action="<?php echo get_site_url(); ?>/wp-admin/admin-post.php">
<input type="hidden" name="action" value="cpt_field_opts" />
<!-- some inputs here ... -->
<?php wp_nonce_field( 'cpt_field_opts', '_wp_nonce' ); ?>
<input type="hidden" name="post_type" value="<?php echo $post_type; ?>">
<input type="hidden" name="origin" value="<?php echo "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; ?>" />
<?php
$c = 0;
foreach ($fields as $field){
?>
<input type="textarea" id="field-<?php echo $c; ?>" name="field-<?php echo $c; ?>" value="<?php echo $field; ?>" size="25" /></br>
<?php
$c += 1;
}
?>
<input type="submit" value="Submit" >
</form>
<form method="post" action="<?php echo get_site_url(); ?>/wp-admin/admin-post.php">
<?php wp_nonce_field( 'cpt_field_opts_new', '_wp_nonce_2' ); ?>
<input type="hidden" name="post_type" value="<?php echo $post_type; ?>" />
<input type="hidden" name="action" value="cpt_field_opts_new" />
<input type="hidden" name="origin" value="<?php echo "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; ?>" />
<input type="submit" value="New" />
</form>
Many people are telling me to rewrite the array as this line:
$va_fields[$c] = $posted_field;
Which I have done, but I also started out with that and it still wasn't working
If you have a bunch of values in $_POST with names like "field-0", "field-1", etc, and you're trying to get them into an array:
$c = 0;
while (isset($_POST["field-$c"])){
// This creates a new element in $va_fields with key=$c and value=$_POST["field-$c"]
$va_fields[$c] = $_POST["field-$c"];
}
var_dump($va_fields); // should show you the array you want
But really, it would be easier to just modify your form to use inputs with name=field[] instead of numbering them. Then you would already have the array in $_POST['fields'] without having to do anything.
It is simple just iterate the $_POST array
<?php
$va_fields = array();
$c=0;
foreach($_POST as $val){
echo '$posted_field: '. $val;
$va_fields[$c] = $val;
}
var_dump($va_fields);
?>

Wordpress widget not saving options... missing something obvious?

Novice Wordpress developer here, so I could really use some assistance. I'm building a widget that displays a feature, but the options aren't saving. When you hit "Save", all the boxes go blank again. I think I'm missing something obvious!
Here's the relevant parts of my widget code:
// First create the widget for the admin panel
class icon_features_widget extends WP_Widget {
function icon_features_widget() {
$widget_ops = array( 'description' => __( 'Displays icon features in a widget', 'icon-feature-widget' ) );
$this->WP_Widget( 'icon_feature_widget', __( 'Icon Feature', 'icon-feature-widget' ), $widget_ops );
}
function form( $instance ) {
$icon_feature_title = isset( $instance['icon_feature_title'] ) ? $instance['icon_feature_title'] : '';
$icon_feature_text = isset( $instance['icon_feature_text'] ) ? $instance['icon_feature_text'] : '';
$icon_feature_link_url = isset( $instance['icon_feature_link_url'] ) ? $instance['icon_feature_link_url'] : '';
$icon_feature_link_text = isset( $instance['icon_feature_link_text'] ) ? $instance['icon_feature_link_text'] : '';
$radioValue = isset( $instance['radioValue'] ) ? $instance['radioValue'] : '';
$icon_feature_icon_class = isset( $instance['icon_feature_icon_class'] ) ? $instance['icon_feature_icon_class'] : '';
$icon_feature_custom_icon_url = isset( $instance['icon_feature_custom_icon_url'] ) ? $instance['icon_feature_custom_icon_url'] : '';
?>
<p>
<label for="icon_feature_title">Title:</label><br />
<input type="text" name="icon_feature_title" id="icon_feature_title" value="<?php echo $icon_feature_title ; ?>" />
</p>
<p>
<label for="icon_feature_text">Text:</label><br />
<textarea name="icon_feature_text" id="icon_feature_text" value="<?php echo $icon_feature_text ; ?>" rows="6" cols="21"/></textarea>
</p>
<p>
<label for="icon_feature_link_url">Link URL:</label><br />
<input type="text" name="icon_feature_link_url" id="icon_feature_link_url" value="<?php echo $icon_feature_link_url ; ?>" />
</p>
<p>
<label for="icon_feature_link_text">Link Text (defaults to "Read More"):</label><br />
<input type="text" name="icon_feature_link_text" id="icon_feature_link_text" value="<?php echo $icon_feature_link_text ; ?>" />
</p>
<p>Choose which icon source you'd like to use.</p>
<p>
<label>
<input type="radio" id="iconRadio" name="whichToDisplay" value="icon" <?php echo ($radioValue == 'icon' || $radioValue == '') ? 'checked="checked"':''; ?>/>
Choose from included icons
</label><br />
<label>
<input type="radio" id="customIconRadio" name="whichToDisplay" value="customIcon" <?php echo ($radioValue == 'customIcon') ? 'checked="checked"':''; ?>/>
Display custom icon
</label><br />
<label>
<input type="radio" id="nothingRadio" name="whichToDisplay" value="nothing" <?php echo ($radioValue == 'nothing') ? 'checked="checked"':''; ?>/>
Display neither
</label><br /><br />
</p>
<p> <!-- REPLACE THIS WITH SOME KIND OF ICON CHOOSER DROPDOWN THING -->
<label for="icon_feature_icon_class">Icon Class:</label><br />
<input type="text" name="icon_feature_icon_class" id="icon_feature_icon_class" value="<?php echo $icon_feature_icon_class ; ?>" />
</p>
<p> <!-- REPLACE THIS WITH A WORDPRESS IMAGE CHOOSER / MEDIA LIBRARY CHOOSER -->
<label for="icon_feature_custom_icon_url">Custom Icon URL:</label><br />
<input type="text" name="icon_feature_custom_icon_url" id="icon_feature_custom_icon_url" value="<?php echo $icon_feature_custom_icon_url ; ?>" />
</p>
<?php
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['icon_feature_title'] = $new_instance['icon_feature_title'];
$instance['icon_feature_text'] = $new_instance['icon_feature_text'];
$instance['icon_feature_link_url'] = $new_instance['icon_feature_link_url'];
$instance['icon_feature_link_text'] = $new_instance['icon_feature_link_text'];
$instance['radioValue'] = $new_instance['radioValue'];
$instance['icon_feature_icon_class'] = $new_instance['icon_feature_icon_class'];
$instance['icon_feature_custom_icon_url'] = $new_instance['icon_feature_custom_icon_url'];
return $instance;
}
Thanks much!
in your function update try use $instance as blank array not $old_instance and check once some thing like..
function update( $new_instance, $old_instance ) {
$instance = array();
.........
}
Thanks for the help! Fixed my problem though, which was in the form code: I wasn't using the proper Wordpress methods for id, name & value for the input elements:
I had this:
<input type="text" name="icon_feature_link_text" id="icon_feature_link_text" value="<?php echo $icon_feature_link_text ; ?>" />
And I changed it to this:
<input type="text" name="<?php echo $this->get_field_name('icon_feature_link_text') ; ?>" id="<?php echo $this->get_field_id( 'icon_feature_link_text' ); ?>" value="<?php echo esc_attr( $icon_feature_link_text ) ; ?>" />
I did that for all the inputs (as well as the labels), rather than hard-coding in id & name, and now it saves perfectly.
Thanks!

Categories