Skip Navigation

[Resolved] View images is not showing correctly

This support ticket is created 5 years, 3 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 18 replies, has 2 voices.

Last updated by FelipeP5703 5 years, 3 months ago.

Assisted by: Shane.

Author
Posts
#1342319

I am trying to: This video explains - hidden link

#1342363

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Felipe,

Thank you for getting in touch and sharing the video.

I tried looking at this for you but it seems that none of the url's to the images that you've specified in the fields are valid.

Example the one for this post here
hidden link

Leads to a 404 when i tried to view the image.
hidden link

Also there is no preview of the image on the backend.

Could you set this up exactly as in the video so that I can look at it once more.

Thanks,
Shane

#1342365

Here - hidden link

#1342373

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Felipe,

This is definitely an issue local to your site.

Would it be possible for me to grab a copy to test locally.

Having the form to set the posts as draft does not cause the field image to get deleted.

Are there any hooks that are targeting this edit form ? This could be the issue as well.

Please let me know.

Thanks,
Shane

#1342431

Yes, do what you have to do please. So the draft button is taken to a form, which only has a submit button which changes the custom post to draft.

#1343427

Shane, I believe I have found the culprit of our problems! I have hired a php programmer to create a function that would permanently delete images when clicking in the trash can or X in the forms, and also when clicking the delete button (for deleting custom posts).

I temporarily removed the code in the function.php file name Custom functions to delete profile images (you might see it in your copy if you downloaded), and now the images are not removed.

I don't know if when moving servers, or updating the Forms plugin messed up the code. I really wanted this functionality because it would remove images and custom posts of clients who are not with us anymore.

Is there anything you guys can do?

#1344487

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Felipe,

Awesome. I was about to report to you that we were not able to replicate the problem at all. However i'm happy to see the issue was with some custom code.

You can add the code tot he toolset custom code section to preserve it by going to Toolset-> Settings -> Custom Code.

Add it there and then activate it.

Thanks,
Shane

#1345129

Shane

I did what you meantion, to put the custom code in the settings but it still messes up when I try to edit the forms, etc.

I did ask the programmer to give me instructions prior to closing the deal with him for this, which he sent. One of the instructions was to put some codes inside the JS files.

I looked for one specific credfile.js, which was suppose to be in wp-content/plugins/cred-frontend-editor/public/js/credfile.js but the file is not there anymore.

Has the new update to Forms, eliminated this file? If so, what file replaced it?

#1345131

Also, so you understand what this custom code is suppose to do. I hired a programmer to create this custom function in order to permanently delete the images from the database, folders, etc when:

1) The customer changes images, delete images using the trash can or X in the new form or edit form.
2) The customer decides to delete his custom post, then the custom post and the images attached to it would be deleted permanently.

This would save me the hustle of going inside and deleting every image that is in the media folder.

I have a Listing site that has 200+ clients and they want to keep changing pictures all the time, thus this function would really help me keep the database clean.

Toolset Forms should do that automatically but it does not, unfortunately.

#1345151

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Felipe,

Could you send me what the code looks like ?

Because if the code works in the functions.php file of your theme then it should work in the Custom Code sections for our plugins.

Also I'm not able to provide any information on the file wp-content/plugins/cred-frontend-editor/public/js/credfile.js

This is because it is not recommended to modify any of the core files of the plugin as you changes will be lost when the plugin updates.

Finally I cannot help you to fix or adopt this custom code but I can have a look at it and provide advise on it.

Thanks,
Shane

#1345183

Shane,

The programmer had to change some things on the plugin codes in order to make it work.

Here is the code for the functions.php

// Custom functions to delete profile images
add_action('cred_before_save_data', 'my_before_save_data_action',10,1);
function my_before_save_data_action($form_data)
{

  $post_meta_images = get_post_meta($form_data['container_id'],'wpcf-fotos-para-anuncio');
  $profile_image = get_post_meta($form_data['container_id'],'wpcf-foto-perfil');
  $_SESSION['images_before_remove'] = $post_meta_images;
  $_SESSION['profile_image'] = $profile_image[0];
}

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
$session_profile_image = $_SESSION['profile_image'];
$new_profile_image = $_POST['wpcf-foto-perfil'];
$compare_profile_image = strcmp($session_profile_image,$new_profile_image);
if($compare_profile_image != 0){
      $image_id = pn_get_attachment_id_from_url($session_profile_image);
      wp_delete_attachment($image_id); 
}
  if(!empty($_POST['wpcf-fotos-para-anuncio'])){
    $user_selected_imges = $_POST['wpcf-fotos-para-anuncio'];
    $result = array_diff($_SESSION['images_before_remove'], $user_selected_imges);
      foreach($result as $image){
        $image_id = pn_get_attachment_id_from_url($image);
        wp_delete_attachment($image_id);
      }
  } else {
        foreach($_SESSION['images_before_remove'] as $single_image){
        $image_id = pn_get_attachment_id_from_url($single_image);
        wp_delete_attachment($image_id); 
      }
    }
    unset($_SESSION['images_before_remove']);
    unset($_SESSION['profile_image']);
}

function pn_get_attachment_id_from_url( $attachment_url = '' ) {
 
  global $wpdb;
  $attachment_id = false;
 
  // If there is no url, return.
  if ( '' == $attachment_url )
    return;
 
  // Get the upload directory paths
  $upload_dir_paths = wp_upload_dir();
 
  // Make sure the upload path base directory exists in the attachment URL, to verify that we're working with a media library image
  if ( false !== strpos( $attachment_url, $upload_dir_paths['baseurl'] ) ) {
 
    // If this is the URL of an auto-generated thumbnail, get the URL of the original image
    $attachment_url = preg_replace( '/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '', $attachment_url );
 
    // Remove the upload path base directory from the attachment URL
    $attachment_url = str_replace( $upload_dir_paths['baseurl'] . '/', '', $attachment_url );
 
    // Finally, run a custom database query to get the attachment ID from the modified attachment URL
    $attachment_id = $wpdb->get_var( $wpdb->prepare( "SELECT wposts.ID FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = '_wp_attached_file' AND wpostmeta.meta_value = '%s' AND wposts.post_type = 'attachment'", $attachment_url ) );
 
  }
 
  return $attachment_id;
}
//Update Post Title
function update_post_slug( $post_id, $form_data ) {
    $forms = array( 49, 1355 );
     
    if (in_array($form_data['id'], $forms )) {
        $custom_title = get_the_title( $post_id );
        $updated_data = array(
            'ID' => $post_id,
            'post_title' => $custom_title,
            'post_name' => sanitize_title($custom_title),
        );
        wp_update_post( $updated_data );
    }
}
add_action( 'cred_save_data', 'update_post_slug', 10, 2 );

And here was his instructions for the other part of the code that would go inside the plugin:

"Most of the functions are in functions.php file of child theme.

then under wp-content/plugins/cred-frontend-editor/vendor/toolset/toolset-common/toolset-forms/js/repetitive.js

In this file,

paste the below code:

var getUrl = window.location;
var baseUrl = getUrl .protocol + "//" + getUrl.host + "/" + getUrl.pathname.split('/')[1];
var imgae_path = $(this).closest('.wpt-repctl').find('img').attr("src");
$.ajax({
type:'POST',
url:baseUrl+'/wp-content/themes/astra-child/ajax/remove_reptive_images.php',
data:{imgae_path:imgae_path},
success:function(response){
}
});

place above code just after this line:

$('.wpt-repctl', $parent).length > 1

then open this file:

wp-content/plugins/cred-frontend-editor/public/js/credfile.js

and place the below code at top of the page but after this line

$('.js-wpt-credfile-delete, .js-wpt-credfile-undo').on('click', function (e) {

Paste below code:

e.preventDefault();
var imgae_path = $(this).parent().find('img').attr('src');
var getUrl = window.location;
var baseUrl = getUrl .protocol + "//" + getUrl.host + "/" + getUrl.pathname.split('/')[1];
if(imgae_path !== 'undefined'){
$.ajax({
type:'POST',
url:baseUrl+'/wp-content/themes/astra-child/ajax/remove_reptive_images.php',
data:{imgae_path:imgae_path},
success:function(response){
}
});
}

Then open the below file:

wp-content/plugins/types/vendor/toolset/toolset-common/toolset-forms/js/repetitive.js

and paste the below code just after this line:
if ($('.wpt-repctl', $parent).length > 1) {

Placed below code after the above line:

var getUrl = window.location;
var baseUrl = getUrl .protocol + "//" + getUrl.host + "/" + getUrl.pathname.split('/')[1];
var imgae_path = $(this).closest('.wpt-repctl').find('img').attr("src");
$.ajax({
type:'POST',
url:baseUrl+'/wp-content/themes/astra-child/ajax/remove_reptive_images.php',
data:{imgae_path:imgae_path},
success:function(response){
}
});

------------------
and you are good to go. Thanks"

I understand I'm not suppose to mess with the code of the plugin, but is there anyway to make this work without it?

#1345185

But if you guys can adopt this code and make it work in the next update of the plugin, it would be awesome. This feature is very important to keep the site less cluttered due to clients changing images all the time.

#1345203

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Felipe,

Thank you for the code.

However this is out of the scope of what I can provide as support. I would recommend that you try getting in touch with the developer again to adopt this code without having to modify the core files of our plugin to make this work.

Also you are more than welcomed to open a feature request for this as I think your Idea is a good option to add on our forms.

You can suggest the feature request here.
https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

"I understand I'm not suppose to mess with the code of the plugin, but is there anyway to make this work without it?"

If you modify the files of the plugin you will need to add this each time the plugin updates. Finally these files might even get changed or removed in the future so the code isn't exactly future proof.

I would also suggest that the developer converts this code to a custom plugin .

Thanks,
Shane

#1345205

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Also since it is interfering with your form.

The developer needs to add conditionals to the code so it only targets specific forms rather than all your forms

#1345223

I'll see what the programmer can do for me. Thanks Shane