custom field not saving

7.94K viewsIssues
1

I have register custom field for single-question.php template, “embed_video” and am trying to pass custom field from AnsPress ask form to this field.

function embed_video_field( $form ) {function embed_video_field( $form ) {  // Record Button field. $form['fields']['record'] = array(  'html' => '<div class=""><h3>A simple header</h3></div>',);
// Video field.  $form['fields']['video'] = array( 'type'          => 'input', 'subtype'       => 'text', 'label'         => __( 'Embed Video' ), 'attr'          => array( // custom HTML tag attributes. 'id'       => 'videourl', 'class'    => 'embed_video' ), 'sanitize'      => '', // Comma separated list of sanitization filters 'validate'      => '', // Comma separated list of validation methods);  return $form;}add_filter( 'ap_question_form_fields', 'embed_video_field' );

function hooks_save_meta_value($post_id,$post)function hooks_save_meta_value($post_id,$post){ $form = anspress()->get_form( 'form_question' ); // Get all sanitized values. $values = $form->get_values(); { if ( ! add_post_meta( $post_id, 'embed_video', $values['embed_video']['value'], true ) ) {   update_post_meta( $post_id, 'embed_video', $values['embed_video']['value'] ); } }add_action( 'ap_after_new_question', 'hooks_save_meta_value' );

 

Can anyone help me with this?

Changed status to publish

you can see this in working example here – user is able to record video and upload, we retrieve video URL after submission and populate custom “embed_video” field – but I still cannot save this value in the post meta.

http://fresh.richmondcounseling.org/community/questions/ask-a-question/

1

I’ve gotten the custom field to save and update custom meta field. I update custom field with URL using call back from video recorder (clipchamp). Here is my final code:
function embed_video_field( $form ) {
// Record Button field.
$form['fields']['record'] = array(
'html' => '<div id="clipchamp-button"></div><p class="ap-field-desc" id="description_record">Not required. Feel free to record a video for your question. Limit one video per question.</p>',
'order' => 8,
);

// Record Button Message.
$form['fields']['message'] = array(
'html' => '<p class="ap-field-desc" id="hit_record"><a href="#" onclick="window.location.reload(true);">Reload this page</a> if you want to re-record.</p>',
'order' => 9,
);

// Record Success Message.
$form['fields']['success'] = array(
'html' => '<p class="ap-field-desc" id="record_success">Your video was successfully uploaded. It will be available to watch once you submit your question.</p>',
'order' => 10,
);

// Video field.
$form['fields']['video'] = array(
'type' => 'input',
'subtype' => 'text',
'label' => __( 'Embed Video' ),
'order' => 99,
'sanitize' => '', // Comma separated list of sanitization filters
'validate' => '', // Comma separated list of validation methods
);
return $form;
}
add_filter( 'ap_question_form_fields', 'embed_video_field' );

function hooks_save_meta_value($post_id,$post)
{
$form = anspress()->get_form( 'form_question' );
// Get all sanitized values.
$values = $form->get_values();
if ( ! add_post_meta( $post_id, 'embed_video', $values['video']['value'], true ) ) {
update_post_meta( $post_id, 'embed_video', $values['video']['value'] );
}
}
add_action( 'ap_after_new_question', 'hooks_save_meta_value' );

Posted new comment

Hello Matt,
You need to save the video before insert to post meta. Check my below code

Hey Rahul,
Thanks for updating this…I actually save the video file to youtube and then use callback to grab youtube URL – so all I needed was to update custom field with URL and then display using oEmbed – you can see working example now here – http://www.thriveworks.com/community/questions/ask-a-question/

Oh interesting. Looks cool. BTW currently I am working on improving save functionality of our form class. It will be lot more easier to save data after this get completed.

1

Hello Matt,

Here is an example I am doing to save a image field in a custom form. Also make sure to add valid mimes in field args. By default AnsPress allows only image mimes.

Loading Gist...