Saltar navegación

[Resuelto] Affect a specific author to a post created by Forms

This support ticket is created hace 6 años, 3 meses. 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)

Este tema contiene 2 respuestas, tiene 2 mensajes.

Última actualización por Pat hace 6 años, 3 meses.

Asistido por: Shane.

Autor
Mensajes
#1112975

Pat

Hello,

I have 2 postypes :

- "Object" that is used to define objects presented ont he site
- "Message" that is used to request info of a specific object
A Forms that's related to "Message" has been created placed in the Object page to allow visitors to send a message to the Object's owner and have additional info about it.
Now, I need to make that the author of the Message is the author of the Object where Message is displayed.

How can I do this?
Regards
Pat

#1113450

To manipulate the Author of a Post with a Toolset form, if a guest uses that form, you need to apply a Custom Code snippet to that form, using the cred_save_data() hook

You will (within above-shown hook) add a snippet that is using the WordPress API function wp_update_post() to update the created Post with the Author.

To get the proper user (the existing Author of the related Object), you need to:
- make sure there is some form of relation between the Message and Object.
- since the form if placed on the object it's supposed to relate to, it's not crucial to know the exact relationship, you can get the "related Object" author from the current post (where the form is displayed on).
This Is accessible in Toolset Form's API linked above as $form_data['container_id'].

Hence, we can conclude that you will need to apply a Custom Code snippet to your Form, which follows the above logic and uses the outlined API.
We can be of limited assistance only of what comes after (The coding itself):
https://toolset.com/toolset-support-policy/

Here is an example snippet that updates the Post Author with the user of the page where the Form is inserted to, for reference:

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']==19)
    {
        //Get author of container post
        $container_author = get_post_field( 'post_author',  $form_data['container_id'] );
        
         // Update message created
        $my_post = array(
            'ID'           => $post_id,
            'post_author' => $container_author
        );

        // Update the post into the database
        wp_update_post( $my_post );
    }
}

Please adapt the code to your site, it is just a reference snippet that updates the post submitted by the Form with the author of the Page where the Form is inserted to.

The API's above used are documented here:
https://toolset.com/documentation/programmer-reference/cred-api/
https://developer.wordpress.org/reference/functions/add_action/
https://codex.wordpress.org/Function_Reference/get_post_field
https://codex.wordpress.org/Function_Reference/wp_update_post

#1117914

Pat

Hi Beda,

Thanks for your answer. I think this is the good way to move forward on this topic.
I will work on this during next week to adapt it to my specific case and hope everything will be fine.

Regards
Pat