admin管理员组

文章数量:1122832

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

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 );
 }
Share Improve this question asked Sep 6, 2018 at 12:54 StefStef 762 silver badges10 bronze badges 3
  • 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 Commented Sep 6, 2018 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 Commented Sep 6, 2018 at 16:30
  • 1 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 Commented Sep 6, 2018 at 18:03
Add a comment  | 

1 Answer 1

Reset to default 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/)

本文标签: wpdbwpinsertpost not inserting post