SOLVIUM

Webentwicklung

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' => [ ? ] //?
);
GD Star Rating
loading...

, , | Kommentar schreiben | Trackback einrichten

5 Reaktionen zu “PHP: Einen Artikel in WordPress einfügen”

» Einen Kommentar schreiben

  1. Markus

    Ja, in der Tat, eine solche Funktion ist sehr nützlich ;)

  2. Greg @ HOW TO SOLVE NTDLL.DLL ERRORS

    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!

  3. Max

    Wirklich nützlich, vor allem um News aus einem eigenem Admin Bereich gleich noch auf WP zu veröffentlichen ;)

    Aber eine Frage: ist es auch irgendwie möglich, anstatt der Kategorie-IDs auch direkt neue Kategorien anzulegen, indem man einfach einen neuen Begriff sendet?

  4. Jan

    Max, das geht, wenn du vorher

    $categoryId = wp_create_category($category);

    ausführst.

  5. Sandra

    Schönes Script. Ist zwar schon ein wenig älter, aber ich habe es direkt mal eingebaut. Danke dafür!!!

    Gruß Sandra

Einen Kommentar schreiben

Blog Blogs Code DJK Dyckburg Einkaufen EM 2008 Essen Film Firefox Fotos Fussball Games Geburtstag Gewinnspiel Google Gutenachtgeschichten Handy ich Katzen Kreta Lebensweisheiten Musik Münster Münster Poker Politik Programmierung real Rechner Schokolade SEO Shop Studium The Killers Trash TV Twitter unblogbar Uni Video Web Weihnachten Werbung Wirtschaft Wordpress

© 2006-2012 Solvium.de - von und mit Jan Papenbrock - Impressum - Kontakt aufnehmen

» Blog powered by Wordpress. Silk icons von FamFamFam.