black friday sale

Custom Fields
Re-Envisioned
Support MB Frontend Submission How to Autofill the Title?

  • Creator
    Topic
  • #22734
    Resolved EddyPiV
    Participant

    Hi,

    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?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Author
    Replies
  • #22737
    Long Nguyen
    Moderator

    Hi 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.

    #22739
    EddyPiV
    Participant

    Hi, 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.

    #22742
    Teia Local Studio
    Participant

    I 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.

    #22744
    EddyPiV
    Participant

    Hi, 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?

    #22745
    Teia Local Studio
    Participant

    well,

    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.

    #22753
    EddyPiV
    Participant

    No, 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.

    #22754
    Teia Local Studio
    Participant

    I am in a hurry now, but take a look on the “the_date()” function. I think it will help.

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.