Sauter la navigation

[Résolu] Not displaying user field

This support ticket is created Il y a 5 années et 11 mois. 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
- 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)

Ce sujet contient 28 réponses, a 2 voix.

Dernière mise à jour par AndreG3332 Il y a 5 années et 9 mois.

Assisté par: Nigel.

Auteur
Publications
#1175880
view users.png
Custom code users list.png

I am referring to a support ticket ie
https://toolset.com/forums/topic/add-a-author-field-to-company-cred-form/. Unfortunately, the ticket was close before i could reply to keep it open.

If i can just carry forward from that ticket.

The last comment from Nigel was to create a view, add it as a json entry to the cred form to be able to link the multi user option to the add new client form.

I did follow the instruction how ever there seem to be two issues. The first is the "Function" call is giving out an error. Image "custom code user" attached. The second is the User drop down dont show up in the add new client cred form. I did create a view to "export" the users to json. Image "view users" attached.

The final bit of coding included is the "content"code of the dredform "add new client"

[credform]
	[cred_field field="form_messages" class="alert alert-warning"]
	<div class="form-group">
		<label>Company Name (Required)</label>
		[cred_field field="dtms-business-name" force_type="field" class="form-control" output="bootstrap"]
	</div>
	<div class="form-group">
		<label>Registration Nr (Required)</label>
		[cred_field field="dtms-registration-nr" force_type="field" class="form-control" output="bootstrap"]
	</div>
	<div class="form-group">
		<label>P.O. Box Number</label>
		[cred_field field="dtms-p-o-box-nr" force_type="field" class="form-control" output="bootstrap"]
	</div>
	<div class="form-group">
		<label>Post office Name</label>
		[cred_field field="dtms-post-office-name" force_type="field" class="form-control" output="bootstrap"]
	</div>
	<div class="form-group">
		<label>Postal Code</label>
		[cred_field field="dtms-postal-code" force_type="field" class="form-control" output="bootstrap"]
	</div>
	<div class="form-group">
		<label>City</label>
		[cred_field field="dtms-city" force_type="field" class="form-control" output="bootstrap"]
	</div>
	<div class="form-group">
		<label>Address 1 (Required)</label>
		[cred_field field="dtms-ph-address-1" force_type="field" class="form-control" output="bootstrap"]
	</div>
	<div class="form-group">
		<label>Address 2 (Required)</label>
		[cred_field field="dtms-ph-address-2" force_type="field" class="form-control" output="bootstrap"]
	</div>
	<div class="form-group">
		<label>Address 3</label>
		[cred_field field="dtms-ph-address-3" force_type="field" class="form-control" output="bootstrap"]
	</div>
	<div class="form-group">
		<label>Address 4</label>
		[cred_field field="dtms-ph-address-4" force_type="field" class="form-control" output="bootstrap"]
	</div>
	<div class="form-group">
		<label>Town (Required)</label>
		[cred_field field="dtms-ph-city" force_type="field" class="form-control" output="bootstrap"]
	</div>
	<div class="form-group">
		<label>Postal Code (Required)</label>
		[cred_field field='cust-ph-postal-code' force_type='field' class='form-control' output='bootstrap']
	</div>
	<div class="form-group">
		<label>Telephone Nr</label>
		[cred_field field="dtms-telephone-nr" force_type="field" class="form-control" output="bootstrap"]
	</div>
	<div class="form-group">
		<label>Customer Classifications </label>
		[cred_field field="@classification-customer.parent" class="form-control" output="bootstrap" select_text="--- not set ---"]
	</div>
	<div class="form-group">
		[cred_generic_field type='select' field='users']
		{
          "required":0,
          "options":[ [wpv-view name='Users'] ]
		}
  	</div>
[/cred_generic_field]

	[cred_field field="form_submit" output="bootstrap" value="Submit" class="btn btn-primary btn-lg"]
[/credform]

I need assistance wth this to finalise this matter

#1175930

Nigel
Supporter

Les langues: Anglais (English ) Espagnol (Español )

Fuseau horaire: Europe/London (GMT+00:00)

Regarding the syntax error, before you can use the other plugin classes, you must include the files where these classes are declared. (They should have pointed it out when providing you with the code samples, and I didn't spot it.)

I don't know the location for sure because I don't have the plugin, but find the plugin directory in /wp-content/plugins/ and see if you find the files where the classes are declared in something like publishpress/addon/multiple_authors/classes/

Check with the plugin authors if you are not sure.

Once you have identified the plugin files where the Author and Utils classes are defined you will need to include them, so the top of your custom code would look something like:

include_once( basename(__DIR__) . '/wp-content/plugins/xxx/author-filename.php' );
include_once( basename(__DIR__) . '/wp-content/plugins/xxx/utils-filename.php' );
use PublishPress\Addon\Multiple_authors\Classes\Objects\Author;
use PublishPress\Addon\Multiple_authors\Classes\Utils;

/**
 * Custom form action
 */
add_action('cred_save_data', 'tssupp_form_submit', 10, 3);
function tssupp_form_submit($post_id, $form_data) {
  
    if (in_array($form_data['id'], array(53))) { // Edit form ID

...etc...

(The 'use' statements must appear at the top of the file before other code, once the required files have been included.)

Regarding the problem with populating the CRED select input with the output of a View, can you test the output of the View by inserting it on a static page?

Does it produce the options in the same format as if you had inserted a generic select field and manually added two or three options?

#1179074

I am busy working with publishpress on your query will revert back asap

#1179825

Hi Niigel,
Sorry for the delay in responding. Here was the answer form Publish press

// Add these lines:
if (!defined('PP_MULTIPLE_AUTHORS_LOADED')) {
    require_once plugin_dir_path('publishpress-multiple-authors/publishpress-multiple-authors.php') . '/includes.php';
}

I have added it to my code.

With regards to the View issue, i have added it to a static page and it is still not showing at all although all the other fields are showing correctly. I can supply you with a url however you have to be logged in to view it, so i will have to send you the credentials in a private post if you can make it available to me.

#1179831

HI NIgel I have added the code but i am still getting an error.

A problem occurred when executing snippet "add_user_to_usercpt". The result of include_once is: ""

I am pasting the full code here if you can see if i have messed up something

<?php
/**
 * New custom code snippet.
 */

toolset_snippet_security_check() or die( 'Direct access is not allowed' );
/**
 * Custom form action
 */
add_action('cred_save_data', 'tssupp_form_submit', 10, 3);
function tssupp_form_submit($post_id, $form_data) {
     if (!defined('PP_MULTIPLE_AUTHORS_LOADED')) {
        require_once plugin_dir_path('publishpress-multiple-authors/publishpress-multiple-		authors.php') . '/includes.php';
    }

    use PublishPress\Addon\Multiple_authors\Classes\Objects\Author;
    use PublishPress\Addon\Multiple_authors\Classes\Utils;
 
    if (in_array($form_data['id'], array(40))) { // 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);
            }
        }
 
    }
}

THX

#1179833

Nigel
Supporter

Les langues: Anglais (English ) Espagnol (Español )

Fuseau horaire: Europe/London (GMT+00:00)

Let me set up that private reply so I can look at the View issue.

With your code, the obvious thing is that there are spaces in the middle of the require_once line you inserted, i.e.

require_once plugin_dir_path('publishpress-multiple-authors/publishpress-multiple-      authors.php') . '/includes.php';

Remove the spaces between multiple- and authors.php

#1179981
#1180055

Nigel
Supporter

Les langues: Anglais (English ) Espagnol (Español )

Fuseau horaire: Europe/London (GMT+00:00)

Hi Andre

I updated your custom code to get past the error: the use commands near to come at the top of the snippet. (I haven't tested the code to know if it works, but it is no longer showing errors.)

I checked that your View generates the JSON correctly (you can see that on the page View: Users, which you'll want to delete once you've confirmed that for yourself), but I noticed that the select dropdown wasn't being added to the form.

The reason was simple enough: the closing div tag for the containing form group was inside the cred_generic_field shortcode, and not after, which I've fixed, and the select box now appears on the form, and is populated by the View.

#1180663

Hi Nigel,

That seemed to have done the trick. I just want to keep the ticket open for a day or two just to do testing to make sure the results are consistent. I will confirm once it have tested and the results are consistent! Once again thank you for you help in this matter!

#1180700

Nigel
Supporter

Les langues: Anglais (English ) Espagnol (Español )

Fuseau horaire: Europe/London (GMT+00:00)

Glad that it seems to be working. Let me mark this as awaiting your feedback, you'll have a week or so before the robot wants to close the thread.

#1185723

HI NIgel thx for keeping it opne for a week. WOuld it be possible to keep it open for one more week please. Things have been hectic over here!

#1185750

Nigel
Supporter

Les langues: Anglais (English ) Espagnol (Español )

Fuseau horaire: Europe/London (GMT+00:00)

Sure, let me set the status back to awaiting your feedback.

#1187633

Nigel, I have finally tested and it seems to work. However, when I run the template for the add new customer it only shows the author name. Now I am not sure if my code in the template view is incorrect or if it did not create the second user name as it was supposed to do

Here is my code for the template for the add new customer view


<h2>[wpv-post-title]<br/></h2>
[types field='dtms-registration-nr'][/types]<br/>
[types field='dtms-ph-city'][/types]<br/>
[types field='dtms-ph-address-1'][/types]<br/>
[types field='dtms-ph-address-2'][/types]<br/>
[types field='dtms-ph-city'][/types]<br/>
[cred_child_link_form form='45' parent_id='-1' text='Add New Contact' target='_blank']<br/>
[wpv-user field="display_name"]
#1188035

Nigel
Supporter

Les langues: Anglais (English ) Espagnol (Español )

Fuseau horaire: Europe/London (GMT+00:00)

Because the Multiple Authors add-on handles post authors in a non-standard way you'll need to follow their directions on displaying the authors.

According to this page they let you output the authors using a shortcode: lien caché, so you should be able to add their shortcode to your Toolset template to output the multiple authors.

Or, check yourself or clarify with them how the data for multiple authors is stored in the database. Depending on the that, it may be possible to use some combination of Toolset shortcodes to output the additional author name(s).

#1188785

Ok i did get the shortcode and added it to the view. [author_box] I did create an author block however it is still only showing one author. I seem as if the second author didn't load into the database when selected in the input form.