Skip Navigation

[Resolved] Don't run custom code on a page with the toolset form

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

Problem:
The customer is struggling with formating numbers of the price field.

Solution:
The customer specified the page he wanted to apply the formating rules so it wouldn't impact on other pages as well.

	
add_filter('wpcf_fields_slug_arancel_value_display', 'my_price_format');
function my_price_format($value) {
    if (is_singular( 'CPT' ) ) {
        $value = "ARS " . number_format($value, 0, ',', '.');
    }
    return $value;
}

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 13:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Sao_Paulo (GMT-03:00)

This topic contains 4 replies, has 2 voices.

Last updated by Miguel 11 months, 3 weeks ago.

Assisted by: Mateus Getulio.

Author
Posts
#2669063

I have a page that shows the price of a course. To format the price number, I use the following code:

<?php
/**
* New custom code snippet (replace this with snippet description).
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.

add_filter('wpcf_fields_slug_arancel_value_display', 'my_price_format');
function my_price_format($value) {
$value = "ARS ".number_format($value, 0, ',', '.');
return $value;
}

This code works perfect on any course page.

The problem is that I have a Toolset form to subscribe to any course, and I hid a special number field called "e-arancel" that copy the content of the field "arancel".

So, my issue is that the hidden "e-arancel" got me an error because it's not a number anymore because of the code above.
For example, a course price is "72000", and the code transform it to "ARS 72.000".

I think that I could avoid the code with some kind of "if the page doesn't contain the form id=xx, run the code" something like that.

But I'm not sure how to do that.

Is there any documentation that you are following?
yes
https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

Is there a similar example that we can see?
No

What is the link to your site?
hidden link

#2669431

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello there,

It is possible to check if the form is present in that page with a code similar to this:

add_filter('wpcf_fields_slug_arancel_value_display', 'my_price_format');
function my_price_format($value) {
    // Check if the specific form is NOT present on the page
    if (!is_form_present()) {
        $value = "ARS " . number_format($value, 0, ',', '.');
    }
    return $value;
}

function is_form_present() {
    global $post;
    // Replace 'your_form_shortcode_or_id' with the actual shortcode or ID of your form
    if (strpos($post->post_content, 'your_form_shortcode_or_id') !== false) {
        return true;
    }
    return false;
}

The code above will look for occurrences of the form in the current post, only in case it doesn't find it it that it will go ahead with the number formatting.

Please give it a try and tell us how that goes,

Best,

#2669809

My Toolset form is: Preinscripción (ID: 3475)

I customized your code like this (please, tell me how to format the code as you did with your code in your reply!)

<?php
/**
* New custom code snippet (replace this with snippet description).
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.
add_filter('wpcf_fields_slug_arancel_value_display', 'my_price_format');
function my_price_format($value) {
// Check if the specific form is NOT present on the page
if (!is_form_present()) {
$value = "ARS " . number_format($value, 0, ',', '.');
}
return $value;
}

function is_form_present() {
global $post;
// Replace 'your_form_shortcode_or_id' with the actual shortcode or ID of your form
if (strpos($post->post_content, '3475') !== false) {
return true;
}
return false;
}

But I have the same problem, when the user fills the form, there is an error. When I inspect the html code, I see the field arancel with ARS 72.000

#2670069

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hey there,

You can try to use < code> </code > without the spaces to print code in that format.

We can try a different, approach, you mentioned that your form is present only on a single page, is that correct?

Then what if we check if the current page ID is the one where the form is added and only apply the formatting otherwise? Does that approach suffice for you?

If so, you can use the following code, only replacing the ID with the page id where the form is present, the one in which people can subscribe to courses:

add_filter('wpcf_fields_slug_arancel_value_display', 'my_price_format');
function my_price_format($value) {
    if (!is_on_form_page()) {
        $value = "ARS " . number_format($value, 0, ',', '.');
    }
    return $value;
}

function is_on_form_page() {
    // Replace 'PAGE_ID' with the ID of the page where the form is located
    if (is_page('PAGE_ID')) {
        return true;
    }
    return false;
}

If this approach doesn't work or it is not enough, I would like to request temporary access (wp-admin and FTP) to your site to take better look at the issue. You will find the needed fields for this below the comment area when you log in to leave your next reply. The information you will enter is private which means only you and I can see and have access to it.

Our Debugging Procedures

I will be checking various settings in the backend to see if the issue can be resolved. Although I won't be making changes that affect the live site, it is still good practice to backup the site before providing us access. In the event that we do need to debug the site further, I will duplicate the site and work in a separate, local development environment to avoid affecting the live site.

Privacy and Security Policy

We have strict policies regarding privacy and access to your information. Please see:
https://toolset.com/purchase/support-policy/privacy-and-security-when-providing-debug-information-for-support/

**IMPORTANT**

- Please make a backup of site files and database before providing us access.
- If you do not see the wp-admin/FTP fields this means your post & website login details will be made PUBLIC. DO NOT post your website details unless you see the required wp-admin/FTP fields. If you do not, please ask me to enable the private box. The private box looks like this: hidden link

Please, let me know if you need any additional details. Have a nice day.

#2670875

Well I was analyzing what I made with toolset and I realize that the the page where I need to format the course price is custom post type "Cursos"

So I think that I have to set the condition to that post type.

Something like this:

if ( is_singular( 'curso' ) ) {
    // conditional content/code
}

Using your code, I will try this:

add_filter('wpcf_fields_slug_arancel_value_display', 'my_price_format');
function my_price_format($value) {
    if (is_singular( 'curso' ) ) {
        $value = "ARS " . number_format($value, 0, ',', '.');
    }
    return $value;
}

Am I right?

#2671035

I was right!

Thank you for your patience!