Skip Navigation

[Resolved] Save a document field to another field changing extension

This thread is resolved. Here is a description of the problem and solution.

Problem: In my CRED form I allow the User to upload a file. I would like to copy the file name into another custom field, but modify the file name and extension.

Solution: Use the CRED API cred_save_data to copy and manipulate the field value, then store it into another field.

add_action('cred_save_data', 'save_pdf_image',10,2);
 function save_pdf_image($post_id, $form_data)
    {
        // if a specific form
        if ($form_data['id']==261)
        {
            if (isset($_POST['wpcf-pdf'])) 
            {
                $image_url = str_replace('.pdf', '-pdf.jpg', get_post_meta( $post_id, 'wpcf-pdf', true) );
                update_post_meta($post_id, 'wpcf-pdf_image_url', $image_url);
            }
        }
    }

Relevant Documentation:
https://codex.wordpress.org/Function_Reference/update_post_meta
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created 6 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by rafaelE-3 6 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#618737
Noticia_importante_en__pdf___1-_Showscreen_-_Tablón_digital.jpg

I need to upload a a pdf document to a child post in a creed form and when I clic "save button", save a copy of that field to another field in the same form:
Example:
Document field: [pdf] - Value= hidden link

Other field [pdf_image] - Value=hidden link

I was trying to do something following this tutorial but it doesn't works:
https://toolset.com/forums/topic/cred-save-data-on-existing-field/

This is the code I'm using at functions.php:

add_action('cred_save_data', 'save_pdf_image',10,2);
function save_pdf_imaget($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==261)
{
if (isset($_POST['pdf']))
{
$image_url = str_replace('.pdf', '-pdf.jpg', types_render_field( 'pdf'));
$dirname = str_replace('.jpg','',str_replace(home_url('/'),'',$image_url));
$images = glob($dirname."*.jpg");
update_post_meta($post_id, 'pdf_image_url', $images], true);
}
}
}

¡Thanks in advance!

#618841

I'll try to help.

add_action('cred_save_data', 'save_pdf_image',10,2);
function save_pdf_imaget($post_id, $form_data)

You have a typo: 'save_pdf_image' vs. 'save_pdf_imaget'.

if (isset($_POST['pdf']))

If the PDF field is a custom field created by Types, you must use the 'wpcf-' prefix to access it in the $_POST superglobal:

if (isset($_POST['wpcf-pdf']))
$image_url = str_replace('.pdf', '-pdf.jpg', types_render_field( 'pdf'));

The types_render_field function is used to generate a formatted HTML image tag, not to return the text value of a custom field. Use get_post_meta instead, and include the 'wpcf-' prefix again:

$image_url = str_replace('.pdf', '-pdf.jpg', get_post_meta( $post_id, 'wpcf-pdf', true) ); 
$dirname = str_replace('.jpg','',str_replace(home_url('/'),'',$image_url));
$images = glob($dirname."*.jpg");

I'm going to assume this part works as expected.

update_post_meta($post_id, 'pdf_image_url', $images], true);

You have a typo: Remove ']' after $images
Field slug 'pdf_image_url' should be 'wpcf-pdf_image_url' if this is a Types field.
Remove the last parameter ', true'. The last parameter should only be used to specify which value to update if there are multiple values. Check the documentation here:
https://codex.wordpress.org/Function_Reference/update_post_meta

I suspect this will not work on the first try. If that is the case, use error_log statements to determine what the problem is.

add_action('cred_save_data', 'save_pdf_image',10,2);
function save_pdf_image($post_id, $form_data)
{
error_log('save_pdf_image called');
// if a specific form
if ($form_data['id']==261)
{
error_log('correct form id');
if (isset($_POST['wpcf-pdf']))
{
error_log('post pdf is set');

.... and so on...

If you are not familiar with server logs, I can show you how to activate them. Go in your wp-config.php file and look for define(‘WP_DEBUG’, false);. Change it to:

define('WP_DEBUG', true);

Then add these lines, just before it says 'stop editing here':

ini_set('log_errors',TRUE);
ini_set('error_reporting', E_ALL);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');

Submit the CRED form with error logs added to your functions.php file. This will create an error_log.txt file in your site's root directory. Once that is done, you can revert the changes you made to wp-config.php.

#618981

First of all: ¡¡THANKYOU CHRISTIAN!! You are great, man! It works perfect!

Here I attach my final code:

add_action('cred_save_data', 'save_pdf_image',10,2);
 function save_pdf_image($post_id, $form_data)
 	{
		// if a specific form
		if ($form_data['id']==261)
		{
			if (isset($_POST['wpcf-pdf'])) 
			{
				$image_url = str_replace('.pdf', '-pdf.jpg', get_post_meta( $post_id, 'wpcf-pdf', true) );
				update_post_meta($post_id, 'wpcf-pdf_image_url', $image_url);
			}
		}
	}