Skip Navigation

[Resolved] Cred form javascript missing when called via PHP cred_form function

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

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 9 replies, has 2 voices.

Last updated by Waqar 3 years, 3 months ago.

Assisted by: Waqar.

Author
Posts
#1280517

I created a cred form to accept user data to create a new CPT post. When I add it to a page via the cred form shortcode ( [cred_form form="content-add"] ) it works as expected. However, I need to push this cred form to the page via my own shortcode so I can run some conditionals and do some other stuff to decide exactly what to display. When I insert the cred form via the PHP cred_form function ( cred_form($form_id,$post_id); ) the form will display but when you submit it won't save the date and it reloads the page rather than submitting via ajax.

I loaded my page both ways and captured the full page source. Running it through winmerge shows me that when the cred_form shortcode is used the below javascript is included on the page. However, when I include the cred form via my PHP code it does not push to the page. The missing javascript seems to be the reason the cred_form via PHP does not work correctly.

<script type='text/javascript' src='<em><u>hidden link</u></em>;
<script type='text/javascript' src='<em><u>hidden link</u></em>;
<script type='text/javascript'>
/* <![CDATA[ */
var cred_frontend_i18n = {"ajaxurl":"https:\/\/exampledomain.gdn\/wp-admin\/admin-ajax.php","submit":{"action":"cred_submit_form","nonce":"4d567039c8"}};
/* ]]> */
</script>
<script type='text/javascript' src='<em><u>hidden link</u></em>;
<script type='text/javascript' src='<em><u>hidden link</u></em>;
<script type='text/javascript'>
/* <![CDATA[ */
var toolset_select2_i18n = {"errorLoading":"The results could not be loaded","inputTooLongSingular":"Please delete %NUM% character","inputTooLongPlural":"Please delete %NUM% characters","inputTooShort":"Please enter %NUM% or more characters","loadingMore":"Loading more results...","maximumSelectedSingular":"You can only select %NUM% item","maximumSelectedPlural":"You can only select %NUM% items","noResults":"No results found","searching":"Searching..."};
var toolset_select2_i18n = {"errorLoading":"The results could not be loaded","inputTooLongSingular":"Please delete %NUM% character","inputTooLongPlural":"Please delete %NUM% characters","inputTooShort":"Please enter %NUM% or more characters","loadingMore":"Loading more results...","maximumSelectedSingular":"You can only select %NUM% item","maximumSelectedPlural":"You can only select %NUM% items","noResults":"No results found","searching":"Searching..."};
/* ]]> */
</script>
<script type='text/javascript' src='<em><u>hidden link</u></em>;
<script type='text/javascript'>
/* <![CDATA[ */
var cred_select2_frontend_settings = {"ajaxurl":"https:\/\/exampledomain.gdn\/wp-admin\/admin-ajax.php","select2_fields_list":[],"cred_lang":""};
var cred_select2_frontend_settings = {"ajaxurl":"https:\/\/exampledomain.gdn\/wp-admin\/admin-ajax.php","select2_fields_list":[],"cred_lang":""};
/* ]]> */
</script>
<script type='text/javascript' src='<em><u>hidden link</u></em>;

My shortcode PHP code looks something like the following:

add_shortcode('show-some-content', 'do_something_func');
function do_something_func (){
	$child_posts = types_child_posts('my-cpt');
	$user_id = get_current_user_id();
	$post_id = get_the_ID();
	
	// Do some stuff
	
	ob_start();
	cred_form( '2610', $post_id);
	$content = ob_get_clean();
	return $content;
}

How can I get the javascript to enqueue to support the cred form when called via the PHP cred_form function? This seem to probably be the cause of the problem (at least that's my best guess right now).

Thanks!

#1280659

Hi Lane,

Thank you for contacting us and I'd be happy to assist.

During testing, I was able to reproduce this behavior on my test website as well. When the "cred_form" function is used in the PHP code to load a form, some of the necessary scripts are not loaded, which also affects the AJAX form submission.

Appreciate you brought this forward. I'm going to share these findings with the concerned team for further review and will keep you updated with the progress through this ticket.

For now, you can replace "cred_form" function with the "cred_form" shortcode, called through the "do_shortcode" function:
( ref: https://developer.wordpress.org/reference/functions/do_shortcode/ )

Current:


cred_form( '2610', $post_id);

Updated:


echo do_shortcode('[cred_form form="2610" post="'.$post_id.'"]');

I hope this helps and please let me know how it goes.

regards,
Waqar

#1281119

Hi Waqar,

Your suggestion did the trick! Please let me know when the issue is resolved so I can switch back to the native PHP function.

I’m seeing something similar with render_view_template. I’m not on my desktop today so doing a diff/winmerge is not possible but when I use the render_view_template below I do not get the data from the content template, but if I use your same method from above and use do_shortcode it comes through as expected.

//render_view_template('2609', $post_id); //login-or-register or 2609
echo do_shortcode('[wpv-post-body view_template="login-or-register"]');

Is this the same bug?

Thanks!

#1281127

Also, with render_view, I have to do_shortcode to get it to output via PHP:

ob_start();
//render_view( array(
//	'name' => 'show-something',
//	'score' => $res,
//	'count' => $num
//) );
echo do_shortcode('[wpv-view name="show-something" score="' . $res . '" count="' . $num . '"]');
$content = ob_get_clean();
return $content;
#1281825

Hi Lane,

Thanks for writing back and I'll keep you updated with the progress on the "cred_form" function.

As for the "render_view_template" and "render_view" functions, I couldn't reproduce the same issue, in my tests.

Can you please test them again and make sure to use "echo" before them?
https://toolset.com/documentation/programmer-reference/views-api/#render_view_template
( screenshot: hidden link )

regards,
Waqar

#1281923

Hi Waqar,

Thank you for reviewing. I can’t believe I blindly missed the echo.... My apologies for wasting your time.

I’ll keep on the lookout for updates for the original cred_form() function issue.

Thanks,
Lane

#1282709

Hi Lane,

Thanks for the update and glad that this is sorted out.

I'll keep you updated on the cred_form() function issue.

regards,
Waqar

#1319015

Hi Waqar,

Just checking in to see if the cred_form() function issue was resolved?

Thanks,
Lane

#1319867

Hi Lane,

Thanks for writing back.

The fix for this issue hasn't been scheduled yet, but I'll update you through this ticket, once it is resolved.

Note: This has been included in our known issues section at:
https://toolset.com/errata/forms-not-working-correctly-when-rendering-them-using-the-cred_form-php-function/

regards,
Waqar

#2133087

Hi,

The fix for this issue has been covered in the latest release of Toolset Forms (2.6.10).

You're welcome to update and let us know, in case the issue still persists.

regards,
Waqar