Php 在wordpress中获取用户ID

Php 在wordpress中获取用户ID,php,wordpress,Php,Wordpress,我已经创建了一个名为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

我已经创建了一个名为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','')";
$content1='';
$sql=“插入wp_njvt_帖子(帖子日期、帖子日期、帖子作者、帖子内容、帖子标题、帖子摘录、帖子密码、帖子名称、收件人ping、ping、帖子内容过滤、guid、帖子mime类型)
值(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 />';
$current_user=wp_get_current_user();
/**
*@安全使用示例:$current_user=wp_get_current_user();
*if(!($current\u user instanceof WP\u user))
*返回;
*/
回显“用户名:”$“当前用户->用户登录”
; 回显“用户电子邮件:”$当前用户->用户电子邮件。“
; 回显“用户名:”$当前用户->用户名
; 回显“用户名:”$当前用户->用户姓氏。“
; 回显“用户显示名称:”$当前\u用户->显示\u名称。'
; 回显“用户ID:”$当前用户->ID'

您知道如何将用户id放入数据库中插入的数据中吗?

您可以像这样使用get\u current\u user\u 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','')";
$content1='';
$sql=“插入wp_njvt_帖子(帖子日期、帖子日期、帖子作者、帖子内容、帖子标题、帖子摘录、帖子密码、帖子名称、收件人ping、ping、帖子内容过滤、guid、帖子mime类型)
值(NOW(),NOW(),''。获取当前用户id()。'','','','','','','$file','','','','',''http://www.sktcho.com/?p=$file',''”;
享受吧

  • 为什么要将文件放入/wp includes/?这是一个核心目录,不应修改

  • 我试过了,我发现:500个内部服务器错误