Skip Navigation

[Resolved] Add a author field to company cred form.

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.

Our next available supporter will start replying to tickets in about 1.83 hours from now. Thank you for your understanding.

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 9 replies, has 2 voices.

Last updated by Nigel 5 years, 11 months ago.

Assisted by: Nigel.

Author
Posts
#1158552
company Image1.png

The setup
Consultant user
Company user
Company Cpt

Scenario,

The Consultant creates a company User with a specified User form. Then the Consultant creates an entry on the Company Cpt. At this stage we want to associate the Company user to Company Ctp entry as the author, (co-author with Consultant).

We used the Co-Authors Plus to achieve this. The native add new company backend form allows you to add the second author to the Company. (company image 1) Right at the top. need to to be able to bring this in on my cred form also. How would I go about doing this?

#1158624

Nigel
Supporter

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

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

I installed the co-authors plus plugin and took a look at how it works.

Having added multiple authors to a post I expected to find author details stored as hidden post meta, but it seems that they use a hidden custom taxonomy "authors" to manage this.

It's not documented as far as I can see, so it will require some further investigation (or support request on the plugin page) to determine how to programmatically set multiple authors.

Once you have established that then you will be able to use the cred_save_data hook to set the required taxonomy terms, but without knowing the specifics of what the plugin requires I can't say much more than that.

https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

#1158722

HI Nigel,

Thx for your reply.

I have put a support request out to the author of a similar plugin we will be using, to assist. As soon as he has replied I will revert back to you with his answer if I am not able to figure it out myself. I will keep this ticket open for a little longer.

#1158782

Nigel
Supporter

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

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

Sure. I'll be away for the rest of this week, but if you reply someone will cover me.

#1162802

Hi NIgel,

I got a responce from the pluing author. I trust this will assist you?

Hi Herman,

Sure thing, I would be glad helping. But you mention "the part in red" but I can't see what part is that.

I can confirm the multiple authors are set as terms of the taxonomy "author" for the post.
Authors are taxonomies, and can be mapped to a user or not. That is defined in the termmeta table by the meta_key: user_id, and user_id_XX, which contains the user id. The user's data are added as meta data as well (please, check the attached image).

I could give you more information, just need to understand better what is the part you need help.

Looking forward to helping,

Anderson

#1162865

Nigel
Supporter

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

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

That's a little opaque.

I would ask them, concretely if you have

$post_id // post ID
$user1_id // user ID of first author
$user2_id // user ID of second author

How would they programmatically set users 1 and 2 as authors of the post specified by $post_id?

They presumably have an internal function for this, which may or may not also be part of a public API.

#1165340

HI Nigel,

Quick update, we opted for a premium product of multiuser plugin created by PublishPress. I have been communicating with them about this. They have now sent me a bit of sample code to explain your last comment.
I trust this will assist

<?php

use PublishPress\Addon\Multiple_authors\Classes\Objects\Author;
use PublishPress\Addon\Multiple_authors\Classes\Utils;

$post_id  = 1;
$user1_id = 2;
$user2_id = 3;

$authors_list = [];
$users_list   = [$user1_id, $user2_id];

foreach ($users_list as $user_id) {
    $author = Author::get_by_user_id($user_id);

    if (is_object($author)) {
        $authors_list[] = $author;
    }
}

if ( ! empty($authors_list)) {
    Utils::set_post_authors($post_id, $authors_list);
}


#1165506

Nigel
Supporter

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

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

OK, that should be enough to get this to work.

The only thing I'm missing is the company user.

When the consultant publishes the company CPT how do they specify the associated company user?

(You can't select the post author in the form, it will automatically be the current user who submits the form.)

#1168434

HI Nigel,
The intention is to manually a second and third Author using publish press. I did explain the process in the first post on this channel

"The Consultant creates a company User with a specified User form. Then the Consultant creates an entry on the Company Cpt. At this stage we want to associate the Company user to Company Ctp entry as the author, (co-author with Consultant).

We used the Publish press plugin to achieve this. The native add new company backend form allows you to add the second author to the Company. (company image 1) Right at the top. need to to be able to bring this in on my cred form also. How would I go about doing this?"

I hope this explains it.

#1168632

Nigel
Supporter

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

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

I understand that when the consultant publishes the company CPT they want to run the code after the form is submitted to add the matching user as a second author of the company post, but the question is, how does the consultant specify which user that should be?

I guess you could add a generic select field where the value is the user id and the display value the username, so the consultant can select that user in the form, and then when the form is submitted the user ID (required for the custom code to assign the second author) would be available.

To do that, create a View which lists users. For the output section you will need to output the results in a JSON format that can supply the options to a form generic select field, like so:

		<wpv-loop>
          [wpv-item index=1]
          {"value":"[wpv-user field='ID']","label":"[wpv-user field='display_name']"}
          [wpv-item index=other]
          ,{"value":"[wpv-user field='ID']","label":"[wpv-user field='display_name']"}
		</wpv-loop>

You will need to check the option "Disable the wrapping DIV around the View" to get clean output without wrapping divs.

The edit your form which publishes the company CPTs and insert a generic select field, specifying the shortcode to insert this View as the source of the options. That will produce something like this:

[cred_generic_field type='select' field='users']
{
"required":0,
"options":[ [wpv-view name='Users'] ]
}
[/cred_generic_field]

Then you will need the custom code to assign the selected user as an additional author.

I don't have the plugin and so this is untested, and is just an illustration of how you might adapt their code to work with the Forms API, if it doesn't work you'll need to study the code and do some debugging to identify why.

/**
 * Custom form action
 */
add_action('cred_save_data', 'tssupp_form_submit', 10, 3);
function tssupp_form_submit($post_id, $form_data) {

	use PublishPress\Addon\Multiple_authors\Classes\Objects\Author;
	use PublishPress\Addon\Multiple_authors\Classes\Utils;

	if (in_array($form_data['id'], array(53))) { // Edit form ID

		$user1_id = get_current_user_id();

		if ( isset($_POST['users'] ) ){
			$user2_id = $_POST['users'];

			$authors_list = [];
			$users_list   = [$user1_id, $user2_id];

			foreach ($users_list as $user_id) {
			    $author = Author::get_by_user_id($user_id);

			    if (is_object($author)) {
			        $authors_list[] = $author;
			    }
			}

			if ( ! empty($authors_list)) {
			    Utils::set_post_authors($post_id, $authors_list);
			}
		}

	}
}

Let me know if it works in case others are looking for a similar solution.