在WordPress中获取用户ID

3
我已经创建了一个名为testsave.php的文件,它将数据写入WordPress数据库,代码如下:
$content1 = '<img class="alignnone  wp-image-11" src="http://web.qp2qp.com/qp2qp2/'.$file.'.png" alt="" />';
$sql = "INSERT INTO wp_njvt_posts (post_date,post_date_gmt,post_author, post_content, post_title, post_excerpt, post_password, post_name, to_ping, pinged, post_content_filtered, guid, post_mime_type)
VALUES (NOW(),NOW(),'1','', '', '', '','$file','','','','http://www.sktcho.com/?p=$file','')";

testsave.php现已放置在/wp-includes中,现在我需要将用户ID插入到数据库中的数据中。

我尝试将此代码放入文件中,并使用$current_user->ID而不是'1'来获取用户ID,但这并没有起作用。

$current_user = wp_get_current_user();
/**
 * @example Safe usage: $current_user = wp_get_current_user();
 * if ( !($current_user instanceof WP_User) )
 *     return;
 */
echo 'Username: ' . $current_user->user_login . '<br />';
echo 'User email: ' . $current_user->user_email . '<br />';
echo 'User first name: ' . $current_user->user_firstname . '<br />';
echo 'User last name: ' . $current_user->user_lastname . '<br />';
echo 'User display name: ' . $current_user->display_name . '<br />';
echo 'User ID: ' . $current_user->ID . '<br />';

有什么办法可以将用户 ID 放置在插入到数据库中的数据中吗?

2个回答

1
  1. 获取当前用户ID()

  2. 为什么要将文件放在 /wp-includes/ 目录下?那是一个核心目录,不应该被修改。


0
你可以像这样使用 get_current_user_id() 函数...
$content1 = '<img class="alignnone  wp-image-11" src="http://web.qp2qp.com/qp2qp2/'.$file.'.png" alt="" />';
$sql = "INSERT INTO wp_njvt_posts (post_date,post_date_gmt,post_author, post_content, post_title, post_excerpt, post_password, post_name, to_ping, pinged, post_content_filtered, guid, post_mime_type)
VALUES (NOW(),NOW(),'".get_current_user_id()."','', '', '', '','$file','','','','http://www.sktcho.com/?p=$file','')";

享受!


我试过了,结果是这样的:<title>500 内部服务器错误</title> - طارق بك محمود

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接