0

I know that i should use update_post_meta() function but don't know where put it in the code. I tried on the diferent ways but it's don't work how it should.

if ( ! function_exists( 'noo_render_location_picker_field' ) ) :
    function noo_render_location_picker_field( $field = array(), $field_id = '', $value = '', $form_type = '', $object = array() ) {
        wp_enqueue_script( 'location-picker' );

        $post_id  = ! empty( $object ) ? $object['ID'] : 0;
        $input_id = $field_id;
        $class    = isset( $field['required'] ) && $field['required'] ? 'class="noo-mb-location-address form-control jform-validate" required aria-required="true"' : ' class="noo-mb-location-address form-control"';

        $address = noo_get_post_meta( $post_id, $field_id, '' );
        $lat     = noo_get_post_meta( $post_id, $field_id . '_lat', '' );
        $lon     = noo_get_post_meta( $post_id, $field_id . '_lon', '' );

        $is_frontend_submit = ( isset( $_GET['action'] ) && $_GET['action'] == 'post_job' ) ? true : false;
        $is_edit_job = ( isset( $_GET['job_id'] ) && ! empty( $_GET['job_id'] ) ) ? true : false;
        $checkbox_label = __( 'The same as company address', 'noo' );
        $checkbox_id    = '_use_company_address';
        $company_id         = jm_get_employer_company();
        $full_address_company   = ! empty( $company_id ) ? get_post_meta( $company_id, '_full_address', true ) : '';
        $checkbox_value = empty( $post_id ) ? 1 : get_post_meta( $post_id, $checkbox_id, true );

        if ( ($is_frontend_submit && !empty($full_address_company) ) or (!empty($full_address_company) && $is_edit_job)):

            ?>
            <input name="<?php echo $checkbox_id; ?>" type="hidden" value="0"/>
            <div class="form-control-flat">
                <label class="checkbox">
                    <input id="use_company_address" name="<?php echo $checkbox_id; ?>" type="checkbox" <?php checked( $checkbox_value ); ?> value="1"/><i></i>
                    <?php echo esc_html( $checkbox_label ); ?>
                </label>
            </div>
            <script>
                jQuery(document).ready(function () {
                    jQuery("#use_company_address").change(function () {
                        if (jQuery(this).is(":checked")) {
                            jQuery(".noo-location-picker-field-wrap").addClass('hidden');
                        } else {
                            jQuery(".noo-location-picker-field-wrap").removeClass('hidden');
                        }
                    }).change();
                });
            </script>

        <?php endif; ?>
            <div class="noo-location-picker-field-wrap <?php echo $checkbox_value ? '' : ''; ?>">
                <input <?php echo $class; ?> type="text" name="<?php echo esc_attr( $input_id ); ?>" value="<?php echo esc_html($address); ?>" placeholder="<?php echo esc_html__('Enter an exact address.', 'noo') ?>" />
                <input type="hidden" class="noo-mb-lat" name="<?php echo esc_attr( $input_id ) ?>_lat" value="<?php echo $lat; ?>">
                <input type="hidden" class="noo-mb-lon" name="<?php echo esc_attr( $input_id ) ?>_lon" value="<?php echo $lon; ?>">
                <div class="noo-mb-job-location" id="<?php echo esc_attr( $input_id ); ?>" data-lat="<?php echo $lat; ?>"
                     data-lon="<?php echo $lon; ?>" style="height: 300px;">
                </div>
            </div>
        <?php
    }
endif;

1 Answer 1

-1

If I understood correctly, you are not able to save using update_post_meta. If this is you can try saving using jQuery using ajax, try this.

 //on change checkbox
  jQuery.ajax({
     type: 'POST',
     url: "/wp-admin/admin-ajax.php?action=your_custom_function",
     data:{
         meta_id: 'YOUR_META_ID', //get user or post meta and set here
         meta_value: 'YOUR_META_VALUE' /the value you want to save in the meta
     },
     dataType: 'JSON',
     success: function(res)
     {
        console.log(res);
     }
  });

  //in your functions.php
  function your_custom_function(){
      return (get_post_meta($_POST['meta_id'], 'you_meta_name')) ? update_post_meta($_POST['meta_id'], 'you_meta_name', $_POST['meta_value']) : ($_POST['meta_id'], 'you_meta_name', $_POST['meta_value'], true);
  }
  add_action('wp_ajax_your_custom_function','your_custom_function');

So each time the checkbox is changed, your goal will be updated, you can debug it using the console's XHR.

I hope this helps you.

1
  • 1
    You may want to sanitize the $_POST input. As shown, your code accepts everything and is highly insecure!
    – kaiser
    Commented Jul 13, 2018 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.