PHP: Einen Artikel in WordPress einfügen
Dienstag, 9. September 2008 - 23:05 Uhr
Um per PHP-Script einen Artikel in ein WordPress-Blog einzufügen – also nicht, wie üblich, über das Backend oder per Mail – reicht ein einfacher Aufruf der WordPress-Funktion wp_insert_post(). Vorher muss ein Array mit den Daten des Beitrags gefüllt werden, z.B. Autor, Titel, Inhalt, Kategorie und Tags, dazu Veröffentlichungszeitpunkt und -status. Die genaue Liste gibt’s weiter unten.
Automatisches Einfügen durch PHP-Scripte ist insbesondere nützlich, um zu spammen äääh… hm, tja. Gibt aber bestimmt auch sinnvolle Verwendung dafür.
Ein einfaches, funktionierendes Beispiel:
<?php
require("../wordpress/wp-blog-header.php"); // wp-blog-header.php im WordPress-Hauptverzeichnis
$post = array(
'post_title' => 'Mein Beitrag', // Beitragstitel
'post_content' => 'Das hier ist mein Beitrag. Mit Inhalt.',
'post_status' => 'publish', // veröffentlichen
'post_author' => 1, // Admin hat ID 1
'post_category' => array(1,4,5), // Array aus Kategorie-IDs
'tags_input' => 'wordpress, tests', // Tags kommagetrennt
);
// Beitrag in Datenbank einfügen
wp_insert_post( $post );
// fertig.
?>
Die verfügbaren Felder des Arrays sind aktuell (laut Funktionsreferenz):
$post = array( 'comment_status' => [ 'closed' | 'open' ] // 'closed' means no comments. 'ID' => [ <post id> ] //Are you updating an existing post? 'menu_order' => [ <order> ] //If new post is a page, sets the order should it appear in the tabs. 'page_template => [ <template file> ] //Sets the template for the page. 'ping_status' => [ ? ] //Ping status? 'pinged' => [ ? ] //? 'post_author' => [ <user ID> ] //The user ID number of the author. 'post_category => [ array(<category id>, <...>) ] //Add some categories. 'post_content' => [ <the text of the post> ] //The full text of the post. 'post_date' => [ Y-m-d H:i:s ] //The time post was made. 'post_date_gmt' => [ Y-m-d H:i:s ] //The time post was made, in GMT. 'post_excerpt' => [ <an excerpt> ] //For all your post excerpt needs. 'post_parent' => [ <post ID> ] //Sets the parent of the new post. 'post_password' => [ ? ] //password for post? 'post_status' => [ 'draft' | 'publish' | 'pending' ] //Set the status of the new post. 'post_title' => [ <the title> ] //The title of your post. 'post_type' => [ 'post' | 'page' ] //Sometimes you want to post a page. 'tags_input' => [ '<tag>, <tag>, <...>' ] //For tags. 'to_ping' => [ ? ] //? );
- Funktionsreferenz: wp_insert_post
- WordPress Foren: manually insert a post
Flattr ist ein Micro-Payment-Dienst für Web-Inhalte.
Mehr zu Flattr findest Du im Beitrag Was ist Flattr?
Ähnliche Beiträge
- comment-page-1 – WordPress 2.7 hat ein neues “Duplicate Content”-Problem 5. März 2009
- Google Chart API: Simple Encoding in PHP 19. Dezember 2009
- [PHP] array_flatten() 7. Dezember 2007
- WordPress hacken – so geht’s (nicht) 18. September 2008
- WordPress-Plugin: Abmahnung wegen “Subscribe to Comments” 20. Oktober 2008

Hey, hier bloggt Jan Papenbrock. Er studiert Wirtschaftsinformatik an der Uni Münster. Nebenbei erstellt er Webseiten und interessiert sich für SEO.
Am 10. September 2008 um 10:29 Uhr
Ja, in der Tat, eine solche Funktion ist sehr nützlich
Am 5. Mai 2009 um 18:41 Uhr
Thanks for the code. It didn’t work at once. I made a few changes to the original and it now works just fine! Great blog!