wp_insert_post not inserting post

I'm trying to insert a post when a user (aka me) clicks on the button (this button is inside a meta box on the admin dashboard). This doesn't happen and I don't know why. When I put the content of my function directly in the php page, the post is inserted. But inside the function, the code doesn't seem to do anything.

My 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 'brDe 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'] . 'brvan seizoen ' . $day['airedSeason'] . '/span/div / div class="tooltip"' . $day['absoluteNumber'] . 'span class="tooltip-content"de ' . $day['absoluteNumber'] . 'e afleveringbr 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 );
 }

Topic wp-insert-post wpdb posts Wordpress

Category Web


I recommend changing the form tag to:

This will ensure that the form submits to the same page it is on.

Also post_title and post_content are required, so the insert will fail if either of those values are blank or missing. (https://developer.wordpress.org/reference/functions/wp_insert_post/)

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.