最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - Display Author name PHP (Selfmade Plug-IN)

programmeradmin3浏览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. ( ) About the self-created plug-in „Contact Form 7 extender“, the post is generated in the background via a PHP code. ()

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…. () () 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:

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

Share Improve this question edited Feb 22, 2022 at 17:43 Faeb asked Feb 22, 2022 at 17:29 FaebFaeb 11 bronze badge 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
 |  Show 1 more comment

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论