Skip Navigation

[Resolved] I see post titles added by CRED having a Title like

This thread is resolved. Here is a description of the problem and solution.

Problem

I noticed that whenever a CRED Form is submitted to create a post, an exact or maybe different copy of the same post (duplicate) is created that has the title like "AutoDraft..."

Why?

Solution

There are 2 Bugs provoking this issue.

One with Relevannsi, and one with SEO by Yoast and Elementor.
The last one has no patch yet, the first has one.

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

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 14 replies, has 2 voices.

Last updated by JanP6468 6 years, 9 months ago.

Assisted by: Beda.

Author
Posts
#619153
screnshot 11.jpg
screnshot 10.jpg

I am trying to:
Make view of a custom post look sleek.

I expected to see:
No post name on the view of a custom post (check screenshot 10).

Instead, I got:
An automatically generated post name appear above the post view (check screenshot 11). Every time user submits a new post, a name auto-generates and appears above the post, which is ugly. I then have to delete the name.

The best thing that could happen would be that the post field "Bike's Name" (bike-name) copies into the name of the post, but it doesn't appear in the post view.

Link to a page where the issue can be seen:
hidden link
vs fixed one:
hidden link

#619344

CRED-Auto-Draft Titles are generated by CRED when you create Posts with CRED but omit the Post Title, or destroy it during the submit process with some code.

Now, there are also 2 Bugs provoking this issue, one with Relevannsi:
https://toolset.com/errata/cred-auto-draft-posts-created-when-relevannsi-cred-and-featured-images-are-used/
and one with SEO by Yoast and Elementor:
https://toolset.com/forums/topic/duplicate-post/

Or, as said, you apply the custom code that alters the Post Title during submit, or omit the post title completely.

To avoid the auto draft, you could for example, as you say, display the post field "Bike's Name" as the title.
To do that you need to update the post title when you submit the form, with the value as from the post field "Bike's Name".

For this you use cred_save_data():
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

An exact example is visible here:
https://pastebin.com/2BTbVbcs

Please let me know which of the issues matches the problem you experience, and if you need more help with the Custom code.

#619652

Hi Beda,

I'm not sure we understood each other well with the problem. There is no post doubling, the post that is created by a user is single and generally works/looks as it should.

The issue is that the post that a user creates has a weird name, which then appears above the post itself in the post view.

I think you addressed this part of the problem in your second part of the advice, but I do not really understand what code to put where to fix this. Just to align on the goal - best output would be that the post name is saved as the "Bike's name" field (which the users put in), but it is then not visible in the post view.

Thank you for the help!

Jan

#619985

No, the CRED plugin never produces such titles unless there is somethign wrong.

So, you either already have broken Custom. Code in place, or you experience the Bugs I mentioned.

Since you say nothing is duplicated, there must be either a custom code or another plugin breaking the title during submit, or, you remove the post title field from CRED.

Please do this test:

1. Create a new "Create new post" CRED form, for the post type you use here
2. Use the "Autogenerate Form" button only, please
3. Insert this CRED Form into a page and submit a post.

Is the title fine now?

If yes, please check your existing CRED Form for missing fields.
If not, please disable all Plugins and use another, newly downloaded theme.

Then, the issue must be solved, as CRED itself cannot produce such titles if nothing else interferes.

Please, can you outline the steps you take to replicate this issue and the results of above tests?

I am quite sure you use custom code already, given your last reply.

Please let me know.

#620592

Hi Beda,

Did as you advised, and I think I found the cause of the issue, although it is still a bit curious.

So, the pure form generated by the wizard worked ok. Then I tried removing the field for filling the post title from the form (see code below), so I tried that, and voila, the same thing happened (CRED generated the insane post name).

	<div class="form-group">
		<label>Bike Name</label>
		[cred_field field='post_title' post='bike' value='' urlparam='' class='form-control' output='bootstrap']
	</div>

Thing is, I do not want the user to input the post title, I need it to stay empty (or even better, take over the user input from "Bike's name" field and then not appear anywhere). The "post_content" field is also absent from all forms, and it stays empty as I would have expected.

Is there any way to solve this?

Thank you again,

Jan

#620606

As I elaborated, this is the most likely source of that issue:
https://toolset.com/forums/topic/automatically-generated-name-of-post/#post-619344

Now, this field is either required, or you need to update the field with Custom Code with some value.
Otherwise, auto drafts are created, since no one knows how to name your post at this point, if you do not pass a value there (omit the filed).

take over the user input from "Bike's name" field and then not appear anywhere

Yes, this is exactly what this script here does:
https://toolset.com/forums/topic/automatically-generated-name-of-post/#post-619344

It takes a custom field value and updates the title with it.

#620647

I checked it out, understand some bits and think it might solve part of the issue (naming the user post), it won't resolve the double appearance of the post name on the post view though.

This is probably the script you are referring to?

add_action('cred_save_data','func_custom_post_title',10,2);
function func_custom_post_title($post_id,$form_data) {
    if ($form_data['id']==9999) {
        $name = get_post_meta($post_id, 'wpcf-name', true);
        $email = get_post_meta($post_id, 'wpcf-email', true);
        $title= $name. '-' . $email;
        $args = array('ID' => $post_id, 'post_title' => $title);
        wp_update_post($args);
    }
}

Anyway, I am not sure I understand it entirely. It seems like PHP to me if I am not mistaken, so I'll implement it with the Snippets plugin. I assume I need to adjust some of the "arguments" of the script - Form ID (the "9999" in the code above for the actual post ID, that sounds understandable). Is there anything else I need to adjust in the script?

#620917

This is the problem with Custom Code.

I feel bad if I do not provide it, but if I do, and you do not understand it fully, then you have no control over it and depend on us.

That is why usually we encourage to either craft custom code on your own or seek assistance with a Contractor:
https://toolset.com/contractors/

Now, related to the code above, let me explain this:

1. You do want to omit the Title Field when a User submits a post with this CRED Form
2. Hence, CRED will automatically create auto draft titles, since there is nothing CRED could use to update the title with, but a title is mandatory in WordPress
3. Hence, I suggest to use a Custom code, that takes your favourite field and puts it in the title

The code is custom code, needs to be understood fully or crafted by someone who can help you with this.

This is the code that does it:

add_action('cred_save_data','func_custom_post_title',10,2);

function func_custom_post_title($post_id,$form_data) {
    if ($form_data['id']==9999) {

        //We get the value of a post field "wpcf-name"
        $name = get_post_meta($post_id, 'wpcf-name', true);

        //We update the title with the above value
        $title= $name;
        $args = array('ID' => $post_id, 'post_title' => $title);
        wp_update_post($args);
    }
}

This code would need to be adjusted (Form ID, and Field slug) to your system.
Then, you would put it in your Theme's Functions PHP file.

After, your CRED forms will now save a title for each new post created with this form, which will be equivalent to the value you pass in the custom field.

The double display of a title in a View, or elsewhere, is probably due to a double call of a shortcode or function in your theme that calls the title.
It would be subject to a new ticket, which we can create once the issue here is solved.

#622151

Alright, well, we went "the Toolset way" because we are kind of bootstrapping the whole web, me learning on the go, and as for now need to rely on our own skills only.

But it doesn't seem too complicated, I got some basics, and I'm sure we'll be able to put this together with your advice (despite all the risk that's involved - I got your disclaimer and am aware we do this on our own risk..)

There are two fields to fill in if I am not mistaken:
1) The id of the post-creating form (in place of the "id" on line 7 of your excerpt:

if ($form_data['id']==9999) {

and
2) the slug of the custom field instead of the "wpcf-name" on line 11:

$name = get_post_meta($post_id, 'wpcf-name', true);

...is that right? Or did I miss anything?

Are there any other crucial things to consider?

#622522

No, you need to adjust:

if ($form_data['id']==9999) {

(line 4 of my code).
Here you change 9999 to the ID of the CRED form you use.

The second is correct, there you edit the slug of the custom field.
My code sample is using a simple line text field.

Do not use a checkbox or similar with this code.

#622628

Oh, my dumb mistake, I actually knew that I replace the "9999" before.

Anyway, tested with the correct setup, and it does work only partially - now there is NO name of the custom post (not the one created by the user in the custom field, neither the long CRED-656546542116.... autogenerated one).

#622635

Then you most likely get an empty result from

$name = get_post_meta($post_id, 'wpcf-name', true);

Please add a var_dump($name); die(); at the end of your CRED API hook.

Then submit the form.
The code will then dump the value that is in the variable $name to your screen.
The form stops, since you die();

Hence, your new code looks like this:

add_action('cred_save_data','func_custom_post_title',10,2);
 
function func_custom_post_title($post_id,$form_data) {
    if ($form_data['id']==9999) {
 
        //We get the value of a post field "wpcf-name"
        $name = get_post_meta($post_id, 'wpcf-name', true);
 
        //We update the title with the above value
        $title= $name;
        $args = array('ID' => $post_id, 'post_title' => $title);
        //wp_update_post($args);

//We debug
      //get the value with the vardump function
       var_dump($name);
      //Stop the form
       die();
//We stop debugging

    }
}

This will show you what the value of $name is.
Most likely, nothing.

This means the field does not exist or is empty on submit.

#622656

Indeed, this is the output:
string(0) ""
...probably meaning the input is considered empty (even though it is not).

I think I found the problem though - I think it shouldn't be the slug that I fill in

$name = get_post_meta($post_id, 'wpcf-name', true);

...but the meta-key of the desired field.

I am not really sure here though and don't want to seriously screw the website - what do you thinks? Shall I try with the meta-key? (It is the same as slug, but with "wpcf-" preposition.)

#622908

string(0) "" means there is nothing.
It's an empty string.

You need to adapt the code as mentioned.
You need to change the "wpcf-name" to "wpcf-your-custom-field-slug-here" where "your-custom-field-slug-here" is the slug if the Field as you set it in Types > Custom Fields > your_fields_group > edit > your_custom_field > edit.
It is important that the "wcpf-" prefix is added because the entire metakey as generated by types is alsways "wpcf-custom-field-slug-here".

$name = get_post_meta($post_id, 'wpcf-name', true);

This already has the wpcf-prefix.
What you change is the "name" part.

#622914

Ok, nevermind, tried it with the wpcf- preposition and it worked. Problem solved.

Thank you for your help!