Skip Navigation

[Resolved] Hi. I want to migrate my functions php file code (only 1 code remains)

This support ticket is created 5 years, 11 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 1 reply, has 2 voices.

Last updated by Nigel 5 years, 11 months ago.

Assisted by: Nigel.

Author
Posts
#1177734

I am trying to: only 1 code - migrate code from functions.php to toolset inbuilt settings. Also the code does not seem to work anyways.

The code is this

<?php

/*
* Add your own functions here. You can also copy some of the theme functions into this file.
* WordPress will use those functions instead of the original functions then.
*/

/*
* NOT WORKING - ANY PERSON CAN UPLOAD IMAGE OF ANY DIMENTIONS
*/

add_filter('cred_form_ajax_upload_validate','my_validation',10,2);
function my_validation($error_fields, $form_data)
{
//field data are field values and errors
list($fields,$errors)=$error_fields;
//validate if specific form
if ($form_data['id']==434)
{
//check if featured image exists o
if (isset($fields['wpcf-imagez']))
{
list($width, $height, $type, $attr) = getimagesize($fields['wpcf-imagez']['field_data']['tmp_name']);

if ($width != 200 && $height != 280) {
//set error message for featured image
$errors['Image-size-error'] = 'Image size must be 280 (height) * 200 (width), your image dimensions are '.$height.' * '.$width;
}

}
}
//return result
return array($fields,$errors);
}

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==434)
{
if (!isset($_POST['wpcf-imagez']))//Change "my_custom_field" to your wpcf-field
{
// since $_POST['my_custom_field'] is not set (!isset())
update_post_meta($post_id, 'wpcf-imagez', 'hidden link');
}
}
}

/*
* I THINK THE FOLLOWING HAS ALREADY BEEN ADDED
*/

add_action( 'before_delete_post', 'count_total_children_delete' );
function count_total_children_delete($post_id)
{
if ( get_post_type( $post_id ) == 'lawyer-review' ) {
$parent_id = get_post_meta( $post_id, '_wpcf_belongs_lawyer_id', true );
if($parent_id != '')
{
$childargs = array(
'post_type' => 'lawyer-review',
'numberposts' => -1,
'meta_key' => '_wpcf_belongs_lawyer_id',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(array('key' => '_wpcf_belongs_lawyer_id', 'value' => $parent_id))
);
$totalChildCount = count(get_posts($childargs));

update_post_meta( $parent_id, 'wpcf-child-count', $totalChildCount);
}
}
}

add_action( 'save_post', 'count_total_children',20,2);
function count_total_children($post_id){

if ( get_post_type( $post_id ) == 'lawyer' ) {
$count=get_post_meta( $post_id, 'wpcf-child-count', true );
if($count == ''){
add_post_meta($post_id, 'wpcf-child-count',0);
}
}

if ( get_post_type( $post_id ) == 'lawyer-review' ) {
$parent_id = get_post_meta( $post_id, '_wpcf_belongs_lawyer_id', true );
if($parent_id != '')
{
$childargs = array(
'post_type' => 'lawyer-review',
'numberposts' => -1,
'meta_key' => '_wpcf_belongs_lawyer_id',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(array('key' => '_wpcf_belongs_lawyer_id', 'value' => $parent_id))
);
$totalChildCount = count(get_posts($childargs));

if( $totalChildCount != $oldChildCount ){
update_post_meta( $parent_id, 'wpcf-child-count', $totalChildCount );
}
}
}
}

#1179003

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

I think the code below "I THINK THE FOLLOWING HAS ALREADY BEEN ADDED" is redundant because of the code I provided to order the parent posts by the child post count.

The code above, it looks like you have some code to require images of specific dimensions when uploading via a form, is that right?

There haven't been any plugin changes that should affect this, if the code was working before it should still be working, unless you made changes to your site (in the other thread, for example, the code originally provided by Minesh was for a different post type than the one currently in use on your site).

If you want to move the code from your functions.php to Toolset > Settings > Custom Code, you should first delete the code from functions.php (obviously make sure you have a copy somewhere), and then create a new code snippet in the settings page and paste the required code.

If the image validation isn't working, double-check that the ID of the form and the field name haven't changed, and if you still have problems, let me know.