0

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 );
 }
  • It's probably because your form action is index.php and it needs to point to the URL of the code that processes the postdata instead. – WebElaine Sep 6 '18 at 14:50
  • I don't think it has to do with that, since the function kind of stands on it own and doesn't rely on the form. – Stef Sep 6 '18 at 16:30
  • Unless you have JavaScript that intercepts, the form action is critical because it tells PHP what URL to send the data to. If it's sending data to the wrong URL nothing will happen. – WebElaine Sep 6 '18 at 18:03
0

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/)

  • What do you mean post_title and post_content are missing? They're there... And what to I have to change the form tag to? – Stef Sep 6 '18 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.