You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决WordPress中wp_insert_post函数未定义错误?已尝试wp-load.php无效

解决WordPress中wp_insert_post未定义错误的替代方案

尝试通过代码向WordPress发布内容时,出现Undefined function 'wp_insert_post'错误,已尝试引入wp-load.php但未解决,现提供其他可行解决办法:

原代码(修正变量名和符号问题)

<?php

date_default_timezone_set('Europe/Madrid');
$script_tz = date_default_timezone_get();
$currentDay = date('D');   //$d = date('D');
$formatter = new IntlDateFormatter('es_ES', IntlDateFormatter::FULL, IntlDateFormatter::NONE, 'Europe/Madrid', IntlDateFormatter::GREGORIAN);
$data1 = $formatter->format(time());

//to use built in wordpress function
require_once ('./wp-load.php');

//post variables
$postType = 'post';
$userID = 1;
$categoryID = '2';
$postStatus = 'publish';

$leadTitle = "Test post date:".$data1."";
$leadContent = '<h1>First Post</h1><p>This is my first post on wordpress.</p>';
$leadContent .= '<!--more--> <p>I am learning to post on wordpress.</p>';


$new_post = array(
    'post_title' => $leadTitle, // 原代码此处误用未定义的$loadTitle,已修正
    'post_content' => $leadContent,
    'post_status' => $postStatus,
    'post_author' => $userID,
    'post_type' => $postType,
    'post_category' => array($categoryID)
);

//Wordpress post function
$post_id = wp_insert_post($new_post);

$finaltext = '';

if($post_id){
    $finaltext = 'Yes, I made a new post.<br>';
}else{
    $finaltext .= 'Something went wrong.<br>';
}

echo $finaltext;
?>

其他解决办法

  • 确认wp-load.php路径正确性
    相对路径可能因脚本位置出错,改用绝对路径或先检查文件是否存在:

    // 示例:使用服务器根目录拼接绝对路径
    $wp_load_path = $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php';
    if (!file_exists($wp_load_path)) {
        die('wp-load.php 文件未找到,请检查路径');
    }
    require_once($wp_load_path);
    
  • 改用WordPress REST API发布内容
    无需加载核心文件,通过API接口实现发布,示例代码:

    <?php
    date_default_timezone_set('Europe/Madrid');
    $script_tz = date_default_timezone_get();
    $currentDay = date('D');
    $formatter = new IntlDateFormatter('es_ES', IntlDateFormatter::FULL, IntlDateFormatter::NONE, 'Europe/Madrid', IntlDateFormatter::GREGORIAN);
    $data1 = $formatter->format(time());
    
    $leadTitle = "Test post date:".$data1."";
    $leadContent = '<h1>First Post</h1><p>This is my first post on wordpress.</p>';
    $leadContent .= '<!--more--> <p>I am learning to post on wordpress.</p>';
    
    // API配置
    $site_url = 'https://你的网站域名'; // 替换为你的WordPress站点域名
    $username = '你的后台用户名';
    $app_password = '你的应用密码'; // 在WordPress后台「用户→个人资料」中生成应用密码
    
    $api_url = $site_url . '/wp-json/wp/v2/posts';
    $credentials = base64_encode("$username:$app_password");
    
    $post_data = array(
        'title' => $leadTitle,
        'content' => $leadContent,
        'status' => 'publish',
        'author' => 1,
        'categories' => array(2)
    );
    
    // 发起请求
    $ch = curl_init($api_url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        "Authorization: Basic $credentials"
    ));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $response = curl_exec($ch);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    $finaltext = '';
    if ($http_code == 201) {
        $finaltext = 'Yes, I made a new post.<br>';
    } else {
        $finaltext = 'Something went wrong.<br>';
        $error_data = json_decode($response, true);
        if (isset($error_data['message'])) {
            $finaltext .= '错误信息: ' . $error_data['message'];
        }
    }
    echo $finaltext;
    ?>
    
  • 检查PHP与WordPress版本兼容性
    确保使用的PHP版本符合当前WordPress的要求(如WordPress 6.4+需PHP 7.4及以上),版本不兼容可能导致核心函数无法正常加载。

  • 排查插件/主题冲突
    如果脚本在WordPress环境内运行(如主题functions.php或自定义插件),可暂时禁用所有插件、切换到默认主题后重新测试,排除冲突导致的函数无法调用问题。

内容的提问来源于stack exchange,提问作者segmentationfaultgonnakillme

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 23:30:56