Last active
October 20, 2019 13:06
-
-
Save wpmudev-sls/20356cb55dbf02005fcb2049814393b8 to your computer and use it in GitHub Desktop.
[Forminator] - Fix Custom Fields From Post Data Field Not Working
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [Forminator] - Fix Custom Fields From Post Data Field Not Working | |
* Description: [Forminator] - Fix Custom Fields From Post Data Field Not Working - 1134362988714714 | |
* Author: Thobk @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
add_action( 'plugins_loaded', 'wpmudev_forminator_fix_custom_field_not_working_func', 100 ); | |
function wpmudev_forminator_fix_custom_field_not_working_func() { | |
if ( defined('FORMINATOR_PRO') && class_exists( 'Forminator' ) ) { | |
// fix custom field not display on front-end | |
add_filter('get_post_metadata', 'wpmudev_forminator_custom_meta_value_forminator_form_meta', 10, 3); | |
function wpmudev_forminator_custom_meta_value_forminator_form_meta( $check, $object_id, $meta_key ){ | |
if( 'forminator_form_meta' !== $meta_key ){ | |
return $check; | |
} | |
$meta_type = 'post'; | |
$meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' ); | |
if ( ! $meta_cache ) { | |
$meta_cache = update_meta_cache( $meta_type, array( $object_id ) ); | |
$meta_cache = $meta_cache[ $object_id ]; | |
} | |
if ( ! $meta_key ) { | |
return $meta_cache; | |
} | |
if ( isset( $meta_cache[ $meta_key ] ) ) { | |
$meta_value = maybe_unserialize( $meta_cache[ $meta_key ][0] ); | |
if( ! empty( $meta_value['fields'] ) ){ | |
foreach( $meta_value['fields'] as $k => $field ){ | |
if( isset( $field['type'] ) && 'postdata' === $field['type'] ){ | |
if( ! empty( $field['options'] ) && ! empty( $field['post_custom_fields'] ) ){ | |
$field['post_custom'] = $field['post_custom_fields']; | |
$field['custom_vars'] = $field['options']; | |
$meta_value['fields'][ $k ] = $field; | |
} | |
} | |
} | |
} | |
return [$meta_value]; | |
} | |
return ''; | |
} | |
// fix the custom form not update custom fields | |
add_action( 'forminator_post_data_field_post_saved', 'wpmudev_formiinator_save_custom_fields_data', 10, 3 ); | |
function wpmudev_formiinator_save_custom_fields_data( $post_id, $field, $data ){ | |
if( isset( $field['custom_vars'], $data['post-custom'] ) ){ | |
$values1 = wp_list_pluck( $field['custom_vars'], 'value' ); | |
$values2 = wp_list_pluck( $data['post-custom'], 'value' ); | |
if( $values1 && $values2 ){ | |
if( $values1 !== $values2 ){ | |
$values1 = array_map('trim', $values1); | |
} | |
if( $values1 === $values2 ){ | |
$field_id = Forminator_Field::get_property( 'element_id', $field ); | |
// support advanced custom field | |
$using_acf = function_exists('get_field_object'); | |
foreach( $data['post-custom'] as $key => $meta_field ){ | |
if( empty( $meta_field['value'] ) ){ | |
$meta_field['value'] = sanitize_title( $meta_field['key'] ); | |
} | |
$mod_field_id = $field_id .'-post_meta-'. $meta_field['value']; | |
if( isset( $_POST[ $mod_field_id ] ) ){ | |
if( $meta_field['key'] ){ | |
delete_post_meta( $post_id, $meta_field['key'], $data['post-custom'][ $key ]['value'] ); | |
} | |
// check advanced custom field first | |
if( $using_acf && $field_obj = get_field_object( $meta_field['value'], $post_id ) && $field_obj->ID ){ | |
update_field($meta_field['value'], $_POST[ $mod_field_id ], $post_id); | |
}else{ | |
update_post_meta( $post_id, $meta_field['value'], $_POST[ $mod_field_id ] ); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment