【问题标题】:Getting 500 (Internal Server Error) on wp_insert_post在 wp_insert_post 上获取 500(内部服务器错误)
【发布时间】:2015-05-02 18:56:30
【问题描述】:

我需要一些帮助,我正在尝试使用 htmlform(php 和 wordpress)来创建 wp_insert_post。我一直收到 500 服务器错误,我不明白为什么,你能帮帮我吗? (打印和回声是为了测试一些东西)。

<?php
    print_r($_POST);
    
    if( 'POST' == $_SERVER['REQUEST_METHOD'] ){
        print((isset ($_POST['title1'])));
        // Do some minor form validation to make sure there is content
        if (isset ($_POST['title1'])) {
            $title =  $_POST['title1'];
        } else {
            echo 'fyll i';
        }
        if (isset ($_POST['description'])) {
            $description = $_POST['description'];
        } else {
            echo 'fyll i';
        }
         if (isset ($_POST['message'])) {
            $message = $_POST['message'];
        } else {
            echo 'fyll i';
        }
        $tags = $_POST['post_tags'];
        echo('ny entry');
    
        print_r($new_post);
    
        $new_post = array(
           'post_title'    => $title,
           'post_content' => $message,
           'post_name'  => $description,
           'post_status'   => 'publish',
           'post_type' => 'omdmen'
            
        );
    
        print_r($new_post);
        $new_post = wp_insert_post($new_post); 
    
    }    
    
    echo('finished');

?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <div id="postbox">
    <form id="new_post" name="new_post" method="post" action="votepost.php">
        <p><label for="title">Ide</label><br />
            <input type="text" id="title" value="" tabindex="1" size="20" name="title1" />
        </p>
        <p><label for="description">Hantverk</label><br />
            <input type="text" id="description" value="" tabindex="1" size="20" name="description" />
        </p>
        <p><label for="message">Budskap</label><br />
            <input type="text" id="message" value="" tabindex="1" size="20" name="message" />
        </p>
        <input type="submit" value="OK" tabindex="6" id="submit" name="submit" />
    </form>
</div>
    </body>
</html>
$标题, 'post_content' => $消息, 'post_name' => $描述, 'post_status' => '发布', 'post_type' => 'omdmen' ); print_r($new_post); $new_post = wp_insert_post($new_post); } 回声(“完成”); ?>

【问题讨论】:

  • 在 wordpress 上下文之外使用 wordpress 代码时,您需要先加载 wordpress 引擎。你必须做require_once('path/to/wp/root/wp-load.php');

标签: php wordpress


【解决方案1】:

是否在代码的其他地方启动了 $title、$description?当代码到达 '$new_post = array' 行时,变量可能不存在。

如果您像这样在代码顶部设置变量会发生什么:

$title = 'This is my title';
$message = 'This is my message';
$description = 'This is my description';

它会被保存吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-13
    • 1970-01-01
    • 2021-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多