0

I have a problem for which I need help, because I just can not find a solution myself.

I have taken over a site where a selfmade plugin was created by someone before.. Using the default plug-in „Contact Form 7“, people can create posts on the page. ( https://gyazo.com/c8b20adecacd90fb9bfe72ad2138a980 ) About the self-created plug-in „Contact Form 7 extender“, the post is generated in the background via a PHP code. (https://gyazo.com/115a6c7c9afafd2970b66fd421ca76a3)

This is how it should work: If a user is not logged in, the author becomes „Hyperspace_Bot“. If a user is logged in, the Author name of the user is displayed.

Now the problem: When a user who is logged in creates a post via the contact form, no Author is displayed…. (https://gyazo.com/b51653a1c5fc08b875a50b926c7565d3) (https://gyazo.com/54e4067ecf1227831898a79e1577affa) As you can see on the screenshots again, there is no author entered. Currently I always have to enter them manually afterwards.

Here is the important part of the code of the plug-in:

add_action('wpcf7_before_send_mail', 'createPostData' );

function createPostData() {
     //get user id
     //$user = get_userdatabylogin('myusername');
     //$user = get_user_by('login', $user_login);
     //$udi = $user->ID;

     get_currentuserinfo();

     if ($_POST['aut'] == '') {
             // Create post object
             $my_post = array(
                     'post_title' => $_POST['title'],
                     'post_content' => $_POST['Description'],
                     'post_status' => 'pending',
                     'post_author' => 3,
                     'post_category' => array($_POST['postcategory'])
             );
     } else {
             $my_post = array(
                     'post_title' => $_POST['title'],
                     'post_content' => $_POST['Description'],
                     'post_status' => 'publish',
                     //'post_author' => $user,
                     'post_author' => $user_ID,
                     'post_category' => array($_POST['postcategory'])
             );
     }

The problem is with ‚post_author‘ => $User_ID in the second part I think…. I’ve already tried different ways with get_current_user_id() or $author_id = get_the_author_meta( ‚ID‘ ), but can’t find a solution.

If someone has an idea, I would be very grateful!

Thanks a lot in advance.

Best regards Fabian

Whole plugin: https://pastebin.com/8RFRPRLw

6
  • Your Code is a security nightmare... Right now its super easy to hijack the website. I strongly advise to use a Plugin like de.wordpress.org/plugins/post-my-contact-form-7
    – Michi91
    Commented Feb 22, 2022 at 17:45
  • You probably need to change the if ($_POST['aut'] == '') condition too. What's that trying to check?
    – Rup
    Commented Feb 22, 2022 at 17:48
  • i think this checks if the user is logged in, so has a rank like subscriber, editor, author or if he is not logged in and has no rank. If he is not logged in the author name becomes "Hyperspace_bot".
    – Faeb
    Commented Feb 22, 2022 at 18:26
  • I think this bit specifically checks if the posted form has a non empty field ‘aut’. If you want to check if the user is logged in you can test the result of get_current_user_id() or is_logged_in().
    – Rup
    Commented Feb 22, 2022 at 21:20
  • I’m curious: how? There’s probably ways to make posts but I’m not sure you can do worse than that.
    – Rup
    Commented Feb 22, 2022 at 21:23

1 Answer 1

0

Your $my_post array contains 'post_author' => $user_ID, but $user_ID isn't defined.

According to WP codex, get_currentuserinfo is deprecated, and should be replaced with wp_get_current_user. But in this case you want the ID, so a better option would be get_current_user_id

So if you replace this line:

get_currentuserinfo();

With:

$user_ID = get_current_user_id();

You should be in business.

3
  • Hi Alex, thank you for the answer! I tried it (I think before also so similar). But it still does not work :(, it just does not take the author name from the logged in user
    – Faeb
    Commented Feb 23, 2022 at 13:15
  • I don't see anywhere that needs the author name?
    – Alex
    Commented Feb 24, 2022 at 18:06
  • Hi, the author's name is not entered manually. People who are registered on the site get the status "Author" and have their name and all their data stored as a profile / user. This data from the logged in user, more precisely only the name, I want to have automatically in the post as can be seen in the screenshots. But currently the code does not seem to find the name of the person who sends the post.
    – Faeb
    Commented Feb 25, 2022 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.