WordPress Tipps #3 – Post-Listen Teil 1

Diese und übernächste Woche zeigen wir dir ein paar Tricks wie du die Post-Listen im WordPress-Backend optimieren und erweitern kannst.

Inhalt

Alle der folgenden Code-Snippets können im functions.php des gewünschten Themes hinterlegt werden.

Spalten umbenennen

Vorhandene Spalten können bei Bedarf einfach umbenannt werden. Das folgende Beispiel ändert die Bezeichnung der Titel-Spalte für normale Posts, Pages oder einem eigenen Post-Type:

<span class="token keyword">function</span> <span class="token function">post_columns_rename</span><span class="token punctuation">(</span><span class="token variable">$columns</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token variable">$columns</span><span class="token punctuation">[</span><span class="token string">'title'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'Beitragsname'</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> <span class="token variable">$columns</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token function">add_filter</span><span class="token punctuation">(</span><span class="token string">'manage_posts_columns'</span><span class="token punctuation">,</span> <span class="token string">'post_columns_rename'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Beiträge</span>
<span class="token comment">//add_filter('manage_pages_columns', 'post_columns_rename'); // Seiten</span>
<span class="token comment">//add_filter('manage_{$post-type}_posts_columns', 'post_columns_rename'); // Eigener Post-Type</span>

Neben title gibt es noch folgende Standard-Spalten: cb (Checkbox), author (Autor), categories (Kategorien), tags (Schlagworte), comments (Kommentare) und date (Datum).

Spalten hinzufügen

Sobald man eigene Plugins schreibt oder eigene Post-Types verwendet, wird man früher oder später eigene Spalten in den Post-Listen benötigen. Dazu muss man mit zwei verschiedenen Hooks zuerst die Spalte selber und dann deren Inhalt definieren.

Spalten definieren

Mit dem folgenden Hook werden zwei neue Spalten hinzugefügt:

<span class="token keyword">function</span> <span class="token function">post_columns_append</span><span class="token punctuation">(</span><span class="token variable">$columns</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token variable">$columns</span><span class="token punctuation">[</span><span class="token string">'subtitle'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'Untertitel'</span><span class="token punctuation">;</span>
    <span class="token variable">$columns</span><span class="token punctuation">[</span><span class="token string">'time'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'Zeit'</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> <span class="token variable">$columns</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token function">add_filter</span><span class="token punctuation">(</span><span class="token string">'manage_posts_columns'</span><span class="token punctuation">,</span> <span class="token string">'post_columns_append'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Beiträge</span>
<span class="token comment">//add_filter('manage_pages_columns', 'post_columns_append'); // Seiten</span>
<span class="token comment">//add_filter('manage_{$post-type}_posts_columns', 'post_columns_append'); // Eigener Post-Type</span>

Der Nachteil an dem obigen Beispiel ist, dass die Spalten auf der rechten Seite angehängt werden. Möchte man die Spalten jedoch an einer bestimmten Stelle einfügen, muss man dazu den Columns-Array neu zusammenbauen. Im folgenden Beispiel wird die Spalte Untertitel nach Titel und Zeit nach Datum eingefügt:

<span class="token keyword">function</span> <span class="token function">post_columns_add</span><span class="token punctuation">(</span><span class="token variable">$columns</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token variable">$columns_new</span> <span class="token operator">=</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">foreach</span><span class="token punctuation">(</span><span class="token variable">$columns</span> <span class="token keyword">as</span> <span class="token variable">$column_key</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token variable">$column_value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token variable">$columns_new</span><span class="token punctuation">[</span><span class="token variable">$column_key</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token variable">$column_value</span><span class="token punctuation">;</span>

        <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token variable">$column_key</span> <span class="token operator">==</span> <span class="token string">'title'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token variable">$columns_new</span><span class="token punctuation">[</span><span class="token string">'subtitle'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'Untertitel'</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token variable">$column_key</span> <span class="token operator">==</span> <span class="token string">'date'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token variable">$columns_new</span><span class="token punctuation">[</span><span class="token string">'time'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'Zeit'</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> <span class="token variable">$columns_new</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token function">add_filter</span><span class="token punctuation">(</span><span class="token string">'manage_posts_columns'</span><span class="token punctuation">,</span> <span class="token string">'post_columns_add'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Beiträge</span>
<span class="token comment">//add_filter('manage_pages_columns', 'post_columns_add'); // Seiten</span>
<span class="token comment">//add_filter('manage_{$post-type}_posts_columns', 'post_columns_add'); // Eigener Post-Type</span>

Spalteninhalte anzeigen

Bis jetzt sind nur die Spalten und deren Überschriften definiert, jedoch noch kein Inhalt. Mit dem folgenden Hook werden nun für die oben definierten Spalten Inhalte angezeigt:

<span class="token keyword">function</span> <span class="token function">post_columns_add_content</span><span class="token punctuation">(</span><span class="token variable">$column_name</span><span class="token punctuation">,</span> <span class="token variable">$post_id</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">switch</span><span class="token punctuation">(</span><span class="token variable">$column_name</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">case</span> <span class="token string">'subtitle'</span><span class="token punctuation">:</span>
            <span class="token keyword">echo</span> <span class="token function">get_post_meta</span><span class="token punctuation">(</span><span class="token variable">$post_id</span><span class="token punctuation">,</span> <span class="token string">'subtitle'</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>

        <span class="token keyword">case</span> <span class="token string">'time'</span><span class="token punctuation">:</span>
            <span class="token keyword">echo</span> <span class="token function">get_the_time</span><span class="token punctuation">(</span><span class="token string">'H:i'</span><span class="token punctuation">,</span> <span class="token variable">$post_id</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token function">add_action</span><span class="token punctuation">(</span><span class="token string">'manage_posts_custom_column'</span><span class="token punctuation">,</span> <span class="token string">'post_columns_add_content'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Beitrag und eigene nicht-hierarchische Post-Types</span>
<span class="token comment">//add_action('manage_pages_custom_column', 'post_columns_add_content', 10, 2); // Seiten und eigene hierarchische Post-Types</span>

Dieser Hook wird für jede oben definierte Spalte aufgerufen, so könnten auch Standard-Spalten-Inhalte überschrieben werden. Der $column_name muss mit dem Array-Key der Spalten-Definition von oben übereinstimmen. Wichtig: Die Hooks manage_posts_custom_column und manage_pages_custom_column werden jeweils für alle nicht-hierarchische (posts) und hierarchische (pages) Post-Types verwendet. Wenn man die Spalten-Inhalte also nur für einen bestimmten Post-Type definieren möchte, muss man eine Abfrage dafür einbauen:

<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">get_post_type</span><span class="token punctuation">(</span><span class="token variable">$post_id</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token string">'post'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// Code für Post-Type 'post'</span>
<span class="token punctuation">}</span>

Spalten entfernen

Wenn man nicht alle Spalten in einer Post-Liste benötigt, kann man diese mit wenig Aufwand entfernen. Der folgende Hook entfernt die Spalten für Kategorien und Tags:

<span class="token keyword">function</span> <span class="token function">post_columns_remove</span><span class="token punctuation">(</span><span class="token variable">$columns</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token function">unset</span><span class="token punctuation">(</span><span class="token variable">$columns</span><span class="token punctuation">[</span><span class="token string">'categories'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token variable">$columns</span><span class="token punctuation">[</span><span class="token string">'tags'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> <span class="token variable">$columns</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token function">add_filter</span><span class="token punctuation">(</span><span class="token string">'manage_posts_columns'</span><span class="token punctuation">,</span> <span class="token string">'post_columns_remove'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Beiträge</span>
<span class="token comment">//add_filter('manage_pages_columns', 'post_columns_remove'); // Seiten</span>
<span class="token comment">//add_filter('manage_{$post-type}_posts_columns', 'post_columns_remove'); // Eigener Post-Type</span>

QuickEdit-Aktion entfernen

Manchmal kann es sein, dass man die Quick-Edit-Funktion für gewisse Post-Types deaktivieren möchte, um zu verhindern, dass der Post ausserhalb der normalen Edit-Seite bearbeitet werden kann. Mit dem folgenden Snippet wird auf dem Post-Type page (also für normale Seiten) die Aktion aus der Liste entfernt:

<span class="token keyword">function</span> <span class="token function">post_row_actions_remove_quickedit</span><span class="token punctuation">(</span><span class="token variable">$actions</span><span class="token punctuation">,</span> <span class="token variable">$post</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token variable">$post</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">post_type</span> <span class="token operator">==</span> <span class="token string">'page'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">unset</span><span class="token punctuation">(</span><span class="token variable">$actions</span><span class="token punctuation">[</span><span class="token string">'inline hide-if-no-js'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> <span class="token variable">$actions</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token function">add_filter</span><span class="token punctuation">(</span><span class="token string">'post_row_actions'</span><span class="token punctuation">,</span> <span class="token string">'post_row_actions_remove_quickedit'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

Wenn QuickEdit für alle Post-Types entfernt werden soll, kann die if-Abfrage einfach entfernt werden.

Post-Aktion hinzufügen

Neben den Standard-Aktionen (Bearbeiten, QuickEdit, Papierkorb und Vorschau) können auch eigene Links oder Aktionen hinzugefügt werden, die dann zur Verfügung stehen, wenn an mit der Maus über eine Post-Zeile in der Liste fährt.

Einfacher Link hinzufügen

Wenn man lediglich einen Link zu den Aktionen hinzufügen möchte, kann man dies mit einem einfachen Hook erledigen. Innerhalb des Hooks muss abgefragt werden, für welchen Post-Type die Aktion definiert werden soll ($post->post_type == 'post'), da sie ansonsten für alle Post-Types hinzugefügt würde. Das folgende Snippet fügt einen einfachen externen Link für normale Beiträge hinzu:

<span class="token keyword">function</span> <span class="token function">post_row_actions_add_link</span><span class="token punctuation">(</span><span class="token variable">$actions</span><span class="token punctuation">,</span> <span class="token variable">$post</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token variable">$post</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">post_type</span> <span class="token operator">==</span> <span class="token string">'post'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token variable">$actions</span><span class="token punctuation">[</span><span class="token string">'google'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'<a href="http://www.google.ch" target="_blank">Google</a>'</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> <span class="token variable">$actions</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token function">add_filter</span><span class="token punctuation">(</span><span class="token string">'post_row_actions'</span><span class="token punctuation">,</span> <span class="token string">'post_row_actions_add_link'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Beitrag und eigene, nicht-hierarchische Post-Types</span>
<span class="token comment">//add_filter('page_row_actions', 'post_row_actions_add_link', 10, 2); // Seiten und eigene, hierarchische Post-Types</span>

Genau wie schon bei den Spalten, hat dieser Hook der Nachteil, dass diese Aktionen nach allen anderen am Ende der Liste definiert werden. Wenn die Aktion an einer bestimmten Stelle eingefügt werden soll, kann dies ähnlich wie bei den Spalten gemacht werden. Das folgende Beispiel fügt die Aktion Google direkt nach der Standard-Aktion Bearbeiten ein:

<span class="token keyword">function</span> <span class="token function">post_row_actions_add_link</span><span class="token punctuation">(</span><span class="token variable">$actions</span><span class="token punctuation">,</span> <span class="token variable">$post</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token variable">$post</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">post_type</span> <span class="token operator">==</span> <span class="token string">'post'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token variable">$actions_new</span> <span class="token operator">=</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">foreach</span><span class="token punctuation">(</span><span class="token variable">$actions</span> <span class="token keyword">as</span> <span class="token variable">$action_key</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token variable">$action_link</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token variable">$actions_new</span><span class="token punctuation">[</span><span class="token variable">$action_key</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token variable">$action_link</span><span class="token punctuation">;</span>

            <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token variable">$action_key</span> <span class="token operator">==</span> <span class="token string">'edit'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token variable">$actions_new</span><span class="token punctuation">[</span><span class="token string">'google'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'<a href="http://www.google.ch" target="_blank">Google</a>'</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token variable">$actions_new</span> <span class="token operator">=</span> <span class="token variable">$actions</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> <span class="token variable">$actions_new</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token function">add_filter</span><span class="token punctuation">(</span><span class="token string">'post_row_actions'</span><span class="token punctuation">,</span> <span class="token string">'post_row_actions_add_link'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Beitrag und eigene nicht-hierarchische Post-Types</span>
<span class="token comment">//add_filter('page_row_actions', 'post_row_actions_add_link', 10, 2); // Seiten und eigene hierarchische Post-Types</span>

Auch in dem obigen Beispiel wird die Aktion nur für normale Beiträge definiert, für alle anderen Post-Types werden die Aktionen unverändert ausgegeben (else-Abfrage).

Aktion hinzufügen

Neben Links können auch komplexere Aktionen definiert werden. Auch diese Aktionen werden genau so wie die einfachen Links definiert, darum zeigen wir im folgenden Snippet nur den einfachen Hook der die Aktion an die bereits vorhandenen Links anhängt. Als Beispiel erstellen wir eine Aktion, welche einen Post publiziert (also den Status auf ‚publish‘ setzt) und nur angezeigt wird, wenn der Post nicht bereits publiziert wurde:

<span class="token keyword">function</span> <span class="token function">post_row_actions_add_action</span><span class="token punctuation">(</span><span class="token variable">$actions</span><span class="token punctuation">,</span> <span class="token variable">$post</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token variable">$post</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">post_type</span> <span class="token operator">==</span> <span class="token string">'post'</span> <span class="token operator">&&</span> <span class="token variable">$post</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">post_status</span> <span class="token operator">!=</span> <span class="token string">'publish'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token variable">$publish_link</span> <span class="token operator">=</span> <span class="token function">wp_nonce_url</span><span class="token punctuation">(</span><span class="token function">admin_url</span><span class="token punctuation">(</span><span class="token string">'edit.php?post_type='</span> <span class="token punctuation">.</span> <span class="token variable">$post</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">post_type</span> <span class="token punctuation">.</span> <span class="token string">'&publish_post='</span> <span class="token punctuation">.</span> <span class="token variable">$post</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">ID</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'publish_post'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token variable">$actions</span><span class="token punctuation">[</span><span class="token string">'publish'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'<a href="'</span> <span class="token punctuation">.</span> <span class="token variable">$publish_link</span> <span class="token punctuation">.</span> <span class="token string">'">Publizieren</a>'</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> <span class="token variable">$actions</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token function">add_filter</span><span class="token punctuation">(</span><span class="token string">'post_row_actions'</span><span class="token punctuation">,</span> <span class="token string">'post_row_actions_add_action'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Beitrag und eigene nicht-hierarchische Post-Types</span>
<span class="token comment">//add_filter('page_row_actions', 'post_row_actions_add_action', 10, 2); // Seiten und eigene hierarchische Post-Types</span>

Um die Sicherheit dieser Aktion zu erhöhen, generieren wir für den Aktions-Link mit wp_nonce_url einen sogenannten nonce. Das zweite Argument dieser Funktion ist die sogenannte nonce-action (in diesem Beispiel „publish_post“), welche in der Aktion abgefragt werden kann.

Aktion ausführen

Jetzt haben wir den Link hinzugefügt und müssen noch die Aktion selber definieren. Dies machen wir über den allgemeinen Hook current_screen, welcher bei jedem Backend-Seiten-Aufruf ausgeführt wird. Aus diesem Grund müssen wir innerhalb des Hooks die Seite $current_screen->base = 'edit', den Post-Type $current_screen->post_type und den URL-Parameter isset($_GET['publish_post']) abfragen. Neben dieser Abfrage müssen wir auch den zuvor definierten nonce überprüfen (die nonce-action muss übereinstimmen). Nach der eigentlichen Aktion (in diesem Fall wp_update_post, um den Post-Status auf ‚publish‘ zu setzen) leiten wir mit wp_redirect zurück zur Liste:

<span class="token keyword">function</span> <span class="token function">post_action_publish_post</span><span class="token punctuation">(</span><span class="token variable">$current_screen</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// Aktion überprüfen</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token variable">$current_screen</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">base</span> <span class="token operator">=</span> <span class="token string">'edit'</span> <span class="token operator">&&</span> <span class="token variable">$current_screen</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">post_type</span> <span class="token operator">==</span> <span class="token string">'post'</span> <span class="token operator">&&</span> <span class="token function">isset</span><span class="token punctuation">(</span><span class="token variable">$_GET</span><span class="token punctuation">[</span><span class="token string">'publish_post'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// nonce überprüfen</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">isset</span><span class="token punctuation">(</span><span class="token variable">$_GET</span><span class="token punctuation">[</span><span class="token string">'_wpnonce'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">&&</span> <span class="token function">wp_verify_nonce</span><span class="token punctuation">(</span><span class="token variable">$_GET</span><span class="token punctuation">[</span><span class="token string">'_wpnonce'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string">'publish_post'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token comment">// Post status auf 'publish' setzen</span>
            <span class="token function">wp_update_post</span><span class="token punctuation">(</span>
                <span class="token keyword">array</span><span class="token punctuation">(</span>
                    <span class="token string">'ID'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token variable">$_GET</span><span class="token punctuation">[</span><span class="token string">'publish_post'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                    <span class="token string">'post_status'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'publish'</span>
                <span class="token punctuation">)</span>
            <span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token comment">// Zurück zur Liste weiterleiten</span>
        <span class="token function">wp_redirect</span><span class="token punctuation">(</span><span class="token variable">$_SERVER</span><span class="token punctuation">[</span><span class="token string">'HTTP_REFERER'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">exit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token function">add_action</span><span class="token punctuation">(</span><span class="token string">'current_screen'</span><span class="token punctuation">,</span> <span class="token string">'post_action_publish_post'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

Der zweite Teil …

… folgt in zwei Wochen. Es gibt noch mehr interessante Tipps zu den Post-Listen die wir dir nicht vorenthalten möchten. Schau doch wieder vorbei!

Andere WordPress Tipps

Schlagwörter:

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.