How to Autofill the Title?
Support › MB Frontend Submission › How to Autofill the Title?Resolved
- This topic has 7 replies, 3 voices, and was last updated 4 years, 5 months ago by
Teia Local Studio.
-
AuthorPosts
-
November 7, 2020 at 12:29 AM #22734
EddyPiV
ParticipantHi,
I have a custom post type "Check-in", where the logged-in username and check-in time are stored as author and publication date/time.
There are no other fields in this cpt than the title. The title is not shown in the frontend.How do I automatically set the title to the display name of the author?
November 7, 2020 at 9:41 AM #22737Long Nguyen
ModeratorHi Eddy,
Could you please clarify the question?
You've posted the question under the MB Frontend Submission sub-forum, that means you want to store to logged-in username as the post title when submitting a new post (check-in)?
Or want to show the post title as the username when the user logged-in? It will change the post title for other users logged-in.
November 7, 2020 at 7:43 PM #22739EddyPiV
ParticipantHi, the first scenario. It is a sort of attendance registration.
The user logs in, and then does the checkin. He will be the author of the post, but I want his displayname as title of the post.Hope that makes it clear.
November 8, 2020 at 5:16 AM #22742Teia Local Studio
ParticipantI have experienced a similar situation in the past.
I had a website where a logged in editor/user created a post, but I needed to force the post title to have the current date (formatted to my needs), the current user name and a custom text.
So, it was required to hide from the current user the post title field (to prevent interference) and set up some custom PHP code on my functions to perform the task, right on post creation.
I don't think you will achieve this right out-of-the-box with MetaBox suite. You will need to manually add this function to you theme, since it works on a different scenario.
Here is a simplified version of my code. Just paste it in the end of your
functions.php
and you will be good to go. Also, you will need to edit it to suit your needs. Hope this helps...function force_modify_the_post_title( $data , $postarr ) { // We only care if it is a post_type "post" // You can change to any other custom post you have registered, like "booking", "project", etc if( $data[ 'post_type' ] === 'post' ) { // Grab all info about the loggedin user global $current_user; wp_get_current_user(); // To see all user details: // print_r($current_user); // My new title $forced_title = 'This post was created by '.$current_user->display_name.' at '.date('d/m/Y'); // Insert the new post title $data[ 'post_title' ] = $forced_title; // Sanitize the new post title to create its slug $data[ 'post_name' ] = sanitize_title($forced_title); } return $data; } add_filter( 'wp_insert_post_data' , 'force_modify_the_post_title' , '99', 2 );
Just tested here on a clean install of wordpress and it worked flawlessly. To test, paste the code and simple go to your control panel and create a new blog post, a simple common one. Pay attention to the title and the slug.
Regards!
Giovani.November 8, 2020 at 5:20 PM #22744EddyPiV
ParticipantHi, Giovani, works perfect, thanks so much!
Just 1 question: I have added the time, tried varies variations, but it keeps coming with UTC time iso the local time.
Checked my settings, set to local (Amsterdam) timezone.
Any idea how to get that correct as well?November 8, 2020 at 6:02 PM #22745Teia Local Studio
Participantwell,
You are saying that you need the date/time to be a different one?
Like for example, the time of a person in another timezone?I imagine you could show a select/combo option on the registration page where the user could pick hist location/timezone, and then you could use this info in you new (forced) post name.
G.
November 9, 2020 at 12:32 AM #22753EddyPiV
ParticipantNo, what I need is the time in my own timezone (Amsterdam timezone). In the settings of the website it is configured like that, but when I use .date('d-m-Y H:i:s'), it adds the time according to UTC, which is 1 hour different from my timezone. I understood that the date function is meant for the local timezone. So I wonder why it adds UTC time, and how to add the local time instead.
Thanks for your help.
November 9, 2020 at 1:12 AM #22754Teia Local Studio
ParticipantI am in a hurry now, but take a look on the "the_date()" function. I think it will help.
-
AuthorPosts
- You must be logged in to reply to this topic.