Skip Navigation

[Gelöst] Use Cred Field As Post Name

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem:
The issue here was that the customer wanted to use a custom field as the post name field.

Solution:
Contrary its not necessary to create a separate field for the post title since CRED generates a post title field for when you are creating a new post form. Generally having a separate post title field from the default one is considered redundant.

This support ticket is created vor 6 Jahren, 11 Monaten. 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.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

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)

Dieses Thema enthält 10 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von alanS3341 vor 6 Jahren, 10 Monaten.

Assistiert von: Shane.

Author
Artikel
#603013

I am trying to: Use a CRED Form Field As The Post Name

Link to a page where the issue can be seen: versteckter Link

I expected to see: A post name in the submitted forms field

Instead, I got: "CRED Auto Draft de389881d03428091ed9c48f0e8c2c34"

I looked for a solution and found this:
https://toolset.com/forums/topic/cred-saving-user-submitted-custom-field-as-post-title/

I am using this solution:

add_action('cred_save_data_[form_id_338]','item_title', 5, 1);
function item_title($post_id) {
$type = get_post_type($post_id);
if ($type == 'item') {
$title = get_post_meta($post_id, 'wpcf-item-name', true);
$slug = sanitize_title($title);
wp_update_post(array('ID' => $post_id, 'post_title' => $title, 'post_name' => $slug));
}
}

But it does not work. I have swapped out "item_title" with "the_event_name" cred form field but still does not work. The form id is 338.

When I try the next solution suggested below I get a syntax error in WP of :
Your PHP code changes were rolled back due to an error on line 187 of file wp-content/themes/toolset-starter-child/functions.php. Please fix and try saving again.
syntax error, unexpected '=', expecting ']'
--------------------------------------------------------------------------------------------------
184 add_action('cred_before_save_data_[form_id_here]','item_title');
185 function item_title() {
186 if (isset($_POST['post_title']) && isset($_POST['wpcf-item-name']))
187 $_POST['post_title'=$_POST['wpcf-item-name'];
188 }

My form id is 338 and the field I would like to use as the post name is "the_event_name_"
The code is placed in functions.php of the Toolset Starter Child Theme
I have changed the Auto Draft Names and changed the draft post to published so they will display on the site

#603030

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Alan,

Thank you for contacting our support forum.

Could you try adding the action with a different priority.

add_action('cred_save_data_[form_id_338]','item_title', 10, 2);

Please try this and let me know if it helps.

Thanks,
Shane

#603045

I put the code in fucntions.php and It came back with this as the post name:

CRED Auto Draft de389881d03428091ed9c48f0e8c2c34 — Pending

#603144

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Alan,

Please try the function like this

function item_title($post_id, $form_data){
// Change your CRED Form "ID" accordingly below
if ($form_data['id']==338){
$type = get_post_type($post_id);
if ($type == 'item') {
$title = get_post_meta($post_id, 'wpcf-item-name', true);
$slug = sanitize_title($title);
wp_update_post(array('ID' => $post_id, 'post_title' => $title, 'post_name' => $slug));
}
}
add_action('cred_save_data','item_title', 10, 2);

Please let me know if this helps.
Thanks,
Shane

#603225
CRED_Post_Name.PNG

Sorry! But I am getting a syntax error:

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

syntax error, unexpected '&', expecting ']'

#603450

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Alan,

The issue is that when it was copied from the forum its being copied and the quotes are being replaced with the HTML ascii equivalent. This means that the quotes i.e "" is being replaced with "&#039" so I would go through and change it back to the ""

Each &#039 is "

Please let me know if this is clear.

Thanks,
Shane

#603480
EventName.PNG

I feel like I have tried every which way on this and still only seem to be getting "Auto-Draft Posts" after reading the original post again I felt I should clarify what I am trying to achieve because what the original post is saying might be something different (or not).

So, I have a CERD form id 338. Within that form I have a field "Event Name" which in the CRED Form code looks like this
<div class="form-group">
<label>Event Name</label>
[cred_field field='the-event-name' post='event' value='' urlparam='' class='form-control' output='bootstrap']
</div>

I am trying to use 'the-event-name' as the post name/title so that I don't have to change it every time someone fills in the form, from the Auto-Draft name to that of the "Event Name" field ('the-event-name')

So when a form (338) is filled out and submitted, the post name becomes that of the Event Name field ('the-event-name')

Thanks for all the help so far 🙂
Al

#603526

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Alan,

Would you mind providing me with admin access to the website so that I can have a look a this for you ?

The private fields will be enabled for your next response.

Thanks,
Shane

#603923

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Alan,

Is there any particular reason why you would be storing the post title in a custom field when there is a dedicated post title field ?

Please let me know .

Thanks,
Shane

#603925

I think I must have had it auto create the form, so I will look at that and get back to you...

#603933

Boy, I feel really silly not considering that! Sorry for all the time it's taken up. It works by using the post name field...

Regards

Al