Skip Navigation

[Resolved] How to include the wordpress post attributes for 0rder in a Cred form

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/Hong_Kong (GMT+08:00)

This topic contains 9 replies, has 3 voices.

Last updated by tony 1 year, 10 months ago.

Assisted by: Luo Yang.

Author
Posts
#2527817

Tell us what you are trying to do?

Please can you advise if it is possible to amend the WordPress post attributes field in a Cred form.

[wpv-post-menu-order]

I have tried various formats in expert mode but I keep getting an error message.
There is a problem with post-menu-order field. Please check CRED form.

<div class="form-group">
<label>Order</label>
[cred_field field='post-menu-order' output='bootstrap']
</div>

Thank you.
Tony

#2527905

I found this previous answer from way back:
https://toolset.com/forums/topic/cred-how-to-update-other-standard-wordpress-fields/

However a syntax error is being reported when I add the code to my functions.php file:
============
Your PHP code changes were rolled back due to an error on line 16 of file wp-content/themes/toolset-starter-child/functions.php. Please fix and try saving again.

syntax error, unexpected identifier "    if"
=============

Line 16 relates to the line 5 in the original code:
    if ($form_data['id']==123)

#2527909

Nigel
Supporter

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

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

Hi Tony

menu_order isn't a supported field in terms of the standard fields you can include in a Toolset Form.

You would need to include a fake menu_order field (a generic field which could have a menu_order slug), and you would then use the cred_save_data hook to take the value of that generic field and use it to update actual menu_order field of the newly published post.

See
https://toolset.com/course-lesson/adding-generic-fields-to-forms/
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://developer.wordpress.org/reference/functions/wp_update_post/

#2527929

Hi Nigel

I can display the field in the form now but when I come to update the functions.php file I get a syntax error, which is odd because it must have worked before on an old website that we no longer have.

Line 5
    if ($form_data['id']==123)

Any pointers appreciated.

Tony

add_action('cred_save_data', 'my_menu_order_action',10,2);
function my_menu_order_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==123)
    {
        if (isset($_POST['menu-oder']))
        {
            $my_post = array(
                        'ID' => $post_id,
                        'menu_order' => $_POST['menu-oder'],
                        );
 
            // Update the post into the database
            wp_update_post( $my_post );
        }
    }
}

#2528301

Nigel
Supporter

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

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

Hi Tony

That looks okay at face value.

What exactly is the error?

#2528309

Hello,

Please copy/paste the syntax error message here. thanks

#2528343

Your PHP code changes were rolled back due to an error on line 7 of file wp-content/themes/toolset-starter-child/functions.php. Please fix and try saving again.

syntax error, unexpected identifier "    if"

I am using the Toolset Starter Child theme and the Appearance Theme File Editor to edit the functions php file.
Line 7 is
    if ($form_data['id']==123)

Thanks
Luo

#2528367
functions-codes.jpg

I have tried the same PHP codes you provided above in a fresh WP installation + the latest version of Toolset plugins, it works fine, I can save it into theme file "functions.php", see my screenshot functions-codes.jpg

See below test site:
hidden link

See the result here:
hidden link

There might be other compatibility issue in your website, as a workaround, you can follow our document to add custom codes into your website:
https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

#2528387

Thanks Luo

I will do as you suggest and use the workaround.

Kind regards and all the best for 2023.
Tony

#2528389

My issue is resolved now. Thank you!