I'm trying to edit a WordPress front end user form plugin so that it will write away multiple check box values (from one of my custom fields) in a single SQL record. I need to do this so my query search form can search for all values checked off. Right now, if a user checks off more than one option, only one single value is being stored. How can I adjust this PHP code so that every value checked will be stored?
/**
* Prints a checkbox field
*
* #param array $attr
* #param int|null $post_id */
function checkbox( $attr, $post_id, $type ) {
$selected = isset( $attr['selected'] ) ? $attr['selected'] : array();
if ( $post_id ) {
$selected = explode( self::$separator, $this->get_meta( $post_id, $attr['name'], $type, true ) );
}
?>
<div class="wpuf-fields">
<span data-required="<?php echo $attr['required'] ?>" data-type="radio"></span>
<?php
if ( $attr['options'] && count( $attr['options'] ) > 0 ) {
foreach ($attr['options'] as $option) {
?>
<label>
<input type="checkbox" name="<?php echo $attr['name']; ?>[]" value="<?php echo esc_attr( $option ); ?>"<?php echo in_array( $option, $selected ) ? ' checked="checked"' : ''; ?> />
<?php echo $option; ?>
</label>
<?php
}
}
?>
Thanks in advance!
Related
So I am trying to make a WordPress widget that echoes out the movie input. I've put the echo in line 10 of the widget function however it doesn't echo out the input. Now I know I could swap this out to echo $instance['movie']; , however I want to test that my movie_search method works, which it isn't. Any help is appreciated and sorry if this answer is really stupid I am still quite new to coding.
<?php
public function widget( $args, $instance) {
echo $args['before_widget'];
if (!empty( $instance['tmdbapikey'] ) ) {
echo $args['before_tmdbapikey'].apply_filters(
'widget_tmdbapikey',
$instance['tmdbapikey']
).$args['after_tmdbapikey'];
}
//widget content output
echo $movie_query;
// whatever you want to display after widget (<div>, etc)
echo $args['after_widget'];
}
public function form( $instance ) {
$tmdbapikey = !empty( $instance['tmdbapikey'] )
? $instance['tmdbapikey']
: esc_html__( '', 'mpd_domain' );
<p>
<label for="<?php echo esc_attr($this->get_field_id('movie')); ?>">
<?php esc_attr_e('movie:', 'mpd_domain'); ?>
</label>
<input
class="widefat"
id="<?php echo esc_attr( $this->get_field_id( 'movie' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'movie' ) ); ?>"
type="text"
value="<?php echo esc_attr( $movie ); ?>">
</p>
This is the method for saving inputted user string.
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['tmdbapikey'] = (!empty($new_instance['tmdbapikey']))
? sanitize_text_field( $new_instance['tmdbapikey'] )
: '';
$instance['movie'] = (!empty($new_instance['movie']))
? sanitize_text_field($new_instance['movie'])
: '';
$instance['size'] = (!empty( $new_instance['size']))
? sanitize_text_field($new_instance['size'])
: '';
$instance['description'] = (!empty( $new_instance['description']))
? sanitize_text_field($new_instance['description'])
: '';
return $instance;
}
Movie search method
public function movie_search(){
//copies inputted data to movie variable
$movie_query = $instance['movie'];
}
Had to pass the $instance into the $movie_search method. So the line should have been public function movie_search($instance) as the variable was not a global variable like I originally thought it was, leaving it an empty variable. Also needed to add a return $movie_search to the last line of the movie search method.
Hence I had to change line 10 to echo $this->movie_search($instance).
In WooCommerce, when I submit, how to catch a custom select field added in the order edit admin pages?
I have added this custom select field in the file class-wc-meta-box-order-data.php. I get this:
But I dont know how to catch or to save $_POST['vendor']
I have tried to add $_POST['vendor'] in wp-admin/post.php ,but it doesn't work.
This is the code that I have added:
<select class="wc-customer-search" id="customer_user" name="customer_user" data-placeholder="<?php esc_attr_e( 'Guest', 'woocommerce' ); ?>" data-allow_clear="true">
<option value="<?php echo esc_attr( $user_id ); ?>" selected="selected"><?php echo htmlspecialchars( $user_string ); ?></option>
</select>
<!--/email_off-->
</p>
<p> <label for="order_status">供應商: </label>
<select name="vendor">
<?php
global $wpdb;
$user_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->users" );
for($i=1;$i<=$user_count;$i++){
$user_info = get_userdata($i);
if (implode(', ', $user_info->roles)=='vendor')
echo "<option value=".$user_info->user_login.">$user_info->user_login</option>";
}
?>
</select></p>
How can I get the submitted value and how can I save it?
Overriding core files is something prohibited for developers. So this is not the correct way to do it.
The way to do it is using the available hooks in the source code, instead of overriding this core files, as you will loose everything when the plugin will be updated.
Replace all original core files
Add this code instead (I have make some minor necessary changes).
Here is the replacement code + a hook to save the data to the order meta data:
add_action( 'woocommerce_admin_order_data_after_order_details', 'custom_code_after_order_details', 10, 1 );
function custom_code_after_order_details ( $order ) {
// Get custom field value from '_vendor' meta key
$value = $order->get_meta('_vendor');
?>
<p> <label for="order_status">供應商: </label>
<select name="vendor">
<?php global $wpdb;
$user_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->users" );
echo '<option value="">Select a vendor</option>';
for ( $i=1; $i<=$user_count; $i++ ) {
$user_info = get_userdata($i);
if ( in_array('vendor', $user_info->roles) ){
$user_login = $user_info->user_login;
$selected = $value == $user_login ? 'selected' : '';
echo '<option '.$selected.' value="'.$user_login.'">'.$user_login.'</option>';
}
}
?>
</select></p>
<input type="hidden" name="custom_select_field_nonce" value="<?php echo wp_create_nonce(); ?>">
<?php
}
add_action( 'save_post', 'save_custom_code_after_order_details', 10, 1 );
function save_custom_code_after_order_details( $post_id ) {
// We need to verify this with the proper authorization (security stuff).
// Check if our nonce is set.
if ( ! isset( $_POST[ 'custom_select_field_nonce' ] ) ) {
return $post_id;
}
$nonce = $_REQUEST[ 'custom_select_field_nonce' ];
//Verify that the nonce is valid.
if ( ! wp_verify_nonce( $nonce ) ) {
return $post_id;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// Check the user's permissions.
if ( 'page' == $_POST[ 'post_type' ] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return $post_id;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
}
// Update the meta field in the database.
update_post_meta( $post_id, '_vendor', $_POST[ 'vendor' ] );
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works.
I am trying to write a function that has an if condition to first see which option of an input is checked and then to check that value. For example, in the code below, I have a radio input named "term_id". I want to first get the option that is checked and then check the value. And then if that value is 70, for example, then do something. I also want to accomplish this with php if possible and not jquery.
$terms = get_terms( 'heythere', $args );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo '<ul class="form-ul">';
$count = 0;
foreach ( $terms as $term ) {
$count++;
$t_id = $term->term_id;
$term_meta = get_option( "taxonomy_$t_id" );
?>
<li>
<label>
<input type="radio" name="term_id" value="<? php echo $term->term_id; ?>" <?php if ($count == 1) { echo 'checked="checked"'; } ?>>
</label>
</li>
I a very new to php and this is my go at it but I am sure it is incorrect.
if(isset($_GET[term_id]) && ($term->term_id === '70')) {
DO THIS
}
I'm using WP_Query to get posts from a custom post type to use the result in a metabox. Everything works great with my query. But after this query I can't get the other meta values from database.
This is my helper function to get custom field value:
function my_page_get_custom_field( $value ) {
global $post;
$custom_field = get_post_meta( $post->ID, $value, true );
if ( !empty( $custom_field ) )
return is_array( $custom_field ) ? stripslashes_deep( $custom_field ) : stripslashes( wp_kses_decode_entities( $custom_field ) );
return false;
}
Here's my query:
$sliderArgs = array(
'posts_per_page' => -1,
'post_type' => 'slider',
);
$slider = new WP_Query($sliderArgs);
if ($slider->have_posts()) {
?>
<select name="slider" id="slider">
$selectedSlide = my_page_get_custom_field('slider');
while($slider->have_posts()){
$slider->the_post();
$slideID = get_the_ID();
?><option value="<?php echo $slideID; ?>" <?php selected($selectedSlide, $slideID, true); ?>><?php the_title(); ?></option><?php
}
wp_reset_postdata(); ?>
</select>
}
And this is my other custom field which returns empty (there is a value in database and when I try to change it works great but not displaying in input value in admin):
<input type="text" name="meta_title" id="meta_title" value="<?php echo my_page_get_custom_field('meta_title'); ?>">
OK I solved it.
I used get_posts instead of WP_Query. This helped me a lot: https://core.trac.wordpress.org/ticket/18408#comment:5
I am trying to create a multiselect dropdown list using "jQuery UI MultiSelect Widget" (http://www.erichynds.com/blog/jquery-ui-multiselect-widget) on Wordpress template page and save checked options into database (Mysql) as a single string value.
And THE PROBLEM: duplicate values are appended (i.e A, B, A, B) each time I click Save form. The problem does not happen IF I select new options in this dropdown list. BTW, other fields like textbox, single-select dropdown or textarea are OK.
Below are 2 code segments that I believe the problem comes from, so someone please help me point out what's going wrong here. I have been worked around this problem for the whole week but NO luck! Thank you very much!
<?php
case 'maker_dropdown':
global $wpdb;
$makers = $wpdb->get_var( $wpdb->prepare( "SELECT field_values FROM ". $wpdb->prefix . "cp_ad_fields WHERE field_name = 'cp_maker';", "" ) );
$selected_makers = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM ". $wpdb->prefix . "usermeta WHERE meta_key = 'maker' AND user_id =" . $user->ID . ";", "" ) );
if ($makers) {
?>
<select name="<?php echo $field_id; ?>[]" class="regular-dropdown multiselect" multiple="multiple" >
<?php
$options = explode( ',', $makers);
$selected_options = explode( ',', $selected_makers);
foreach ( $options as $option) { // loop thru all available options
if ( in_array($option, $selected_options)) {
?>
<option name="<?php echo $option; ?>[]" selected="selected" value="<?php esc_attr_e($the_value); ?>"><?php esc_attr_e($option); ?></option>
<?php
}
else {
?>
<option value="<?php esc_attr_e($option); ?>"><?php esc_attr_e($option); ?></option>
<?php
}
} //endforeach
?>
</select>
<?php } //endif ?>
break;
This code segment is used to display multiselect dropdown list on front-end.
function ctm_profile_fields_save($user_id) {
global $ctm_extra_profile_fields;
foreach ($ctm_extra_profile_fields as $field_id => $field_values) :
$selected_options = implode(',', $_POST[$field_id]);
if ($field_values['type'] == 'maker_dropdown') {
update_user_meta( $user_id, $field_id, $selected_options );
}
else {
update_user_meta( $user_id, $field_id, sanitize_text_field( $_POST[$field_id] ) );
}
endforeach;
}
add_action('personal_options_update', 'ctm_profile_fields_save');
add_action('edit_user_profile_update', 'ctm_profile_fields_save');
This code segment is used to save multiselect dropdown list into database.
update_user_meta function allows for multiple meta entries with the same key.
When you read the existing makers for the user you may get ['A', 'B']. When you save the submitted form you add a new set of selected values ['A', 'B'] to what was there previously. This is why your values are duplicated.
There are two easy ways of solving this.
Option 1: Clear the meta key before saving it.
foreach ($ctm_extra_profile_fields as $field_id => $field_values) :
$selected_options = implode(',', $_POST[$field_id]);
delete_user_meta($user_id, $field_id);
if ($field_values['type'] == 'maker_dropdown') {
update_user_meta( $user_id, $field_id, $selected_options );
}
else {
update_user_meta( $user_id, $field_id, sanitize_text_field( $_POST[$field_id] ) );
}
endforeach;
Option 2: Specify previous value in the update_user_meta to not create duplicate entries
foreach ($ctm_extra_profile_fields as $field_id => $field_values) :
$selected_options = implode(',', $_POST[$field_id]);
if ($field_values['type'] == 'maker_dropdown') {
update_user_meta( $user_id, $field_id, $selected_options, $selected_options );
}
else {
update_user_meta( $user_id, $field_id, sanitize_text_field( $_POST[$field_id] ), sanitize_text_field( $_POST[$field_id] ) );
}
endforeach;