WP_INSERT_POST不插入帖子

时间:2018-09-06 作者:Stef

当一个用户(又名我)点击按钮时,我试图插入一篇帖子(这个按钮位于管理仪表板上的元框内)。这没有发生,我也不知道为什么。当我将函数的内容直接放入php页面时,就会插入帖子。但在函数内部,代码似乎什么都没有做。

我的PHP

 add_action(\'wp_dashboard_setup\', \'schedule_synopsissen_setup\');

 function schedule_synopsissen_setup() {
     wp_add_dashboard_widget(\'schedule-synopsissen\', \'Synopsissen maken\', \'schedule_synopsissen_content\');
 }

 function schedule_synopsissen_content() {
     ?><form action="index.php" method="post">
         <input type="submit" name="synopis_schedule" value="Synopsis maken" />
     </form>
     <?php
     if($_SERVER[\'REQUEST_METHOD\'] == "POST" and isset($_POST[\'synopis_schedule\'])) { schedule_synopsissen(); echo \'<br>De synposis werd aangemaakt!\'; }
 }

 function schedule_synopsissen() {
global $wpdb;
$result = array(
    $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}synopsis WHERE `episodeShow` LIKE \'Familie\' AND `firstAired` LIKE \'" . date("Y-m-d", strtotime(date(\'Y\') . "-W" . (date(\'W\') + 1). "-1")) . "\'", \'ARRAY_A\' )[0],
    $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}synopsis WHERE `episodeShow` LIKE \'Familie\' AND `firstAired` LIKE \'" . date("Y-m-d", strtotime(date(\'Y\') . "-W" . (date(\'W\') + 1). "-2")) . "\'", \'ARRAY_A\' )[0],
    $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}synopsis WHERE `episodeShow` LIKE \'Familie\' AND `firstAired` LIKE \'" . date("Y-m-d", strtotime(date(\'Y\') . "-W" . (date(\'W\') + 1). "-3")) . "\'", \'ARRAY_A\' )[0],
    $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}synopsis WHERE `episodeShow` LIKE \'Familie\' AND `firstAired` LIKE \'" . date("Y-m-d", strtotime(date(\'Y\') . "-W" . (date(\'W\') + 1). "-4")) . "\'", \'ARRAY_A\' )[0],
    $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}synopsis WHERE `episodeShow` LIKE \'Familie\' AND `firstAired` LIKE \'" . date("Y-m-d", strtotime(date(\'Y\') . "-W" . (date(\'W\') + 1). "-5")) . "\'", \'ARRAY_A\' )[0],
);
$post_excerpt = $result[0][\'weekExcerpt\'];
$post_content = \'<p>\' . $post_excerpt . \'</p>\';
foreach ($result as $day) {
    $post_content .= \'
    <h2>\' . ucfirst(strftime(\'%A %e %B %Y\', strtotime($day[\'firstAired\']))) . \' (aflevering <div class="tooltip">\' . $day[\'airedEpisode\'] . \'<span class="tooltip-content">aflevering \' . $day[\'airedEpisode\'] . \'<br>van seizoen \' . $day[\'airedSeason\'] . \'</span></div> / <div class="tooltip">\' . $day[\'absoluteNumber\'] . \'<span class="tooltip-content">de \' . $day[\'absoluteNumber\'] . \'e aflevering<br> van Familie</span></div>)

    </h2>

    \' . $day[\'episodeSynopsis\'];
}
if ( strftime(\'%B\', strtotime($result[0][\'firstAired\'])) !== strftime(\'%B\', strtotime($result[0][\'firstAired\']))) {
    $post_title_month = \' \' . strftime(\'%e\', strtotime($result[0][\'firstAired\']));
}
$post_title = \'SYNOPSIS | \' . $result[0][\'episodeShow\'] . \': \' . strftime(\'%e\', strtotime($result[0][\'firstAired\'])) .  $post_title_month . \' - \' . strftime(\'%e\', strtotime($result[4][\'firstAired\'])) . \' \' . strftime(\'%B\', strtotime($result[4][\'firstAired\']));

wp_insert_post (array(
    \'post_author\' => 1,
    \'post_date\' => strtotime(date(\'Y\') . "-W" . (date(\'W\')). "-5") + 75600,
    \'post_content\' => $post_content,
    \'post_title\' => $post_title,
    \'post_excerpt\' => $post_excerpt,
    \'post_status\' => \'publish\',
    \'post_type\' => \'televisie\',
    \'tax_input\' => array(
        "programmas" => $result[0][\'episodeShow\'],
        "zenders" => switchShow( $result[0][\'episodeShow\'] ),
        ),
    \'meta_input\' => array(
        "layout" => \'synopsis\',
        "checkboxes-infobox" => \'on\',
        "checkboxes-time" => \'on\',
        "checkboxes-summary" => \'on\',
        "_thumbnail_id" => $result[0][\'weekId\'],
        ),
), true );
 }

1 个回复
SO网友:Tim

我建议将表单标记更改为:

这将确保表单提交到其所在的页面。

此外,还需要post\\u标题和post\\u内容,因此如果这些值中的任何一个为空或丢失,则插入将失败。(https://developer.wordpress.org/reference/functions/wp_insert_post/)

结束