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 '<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 );
}