/ / Beitrag im Frontend aktualisieren: Beitrags-ID an functions.php senden - WordPress

Post im Frontend aktualisieren: Sende die Post ID an functions.php - wordpress

Dies ist ein sehr einfaches Formular, mit dem ich Beiträge im Frontend aktualisieren möchte:

<?php $current_post = $post->ID; ?>
<form action="" method="post">
<input type="text" name="title" value="<?php echo $title; ?>">
<textarea name="text"><?php echo $content; ?></textarea>
<input type="submit" value="edit post">
<?php wp_nonce_field( "edit_post", "edit_post_nonce" ); ?>
<input type="hidden" name="edit_post" value="true" />
</form>

In functions.php habe ich folgendes Skript (Ich entferne die Überprüfungszeilen):

if( "POST" == $_SERVER["REQUEST_METHOD"] && !empty( $_POST["edit_post"] )) {
global $current_post;

$post = array(
"ID" => $current_post,
"post_type"     => "post",
"post_title"    => wp_strip_all_tags($_POST["title"]),
"post_content"  => wp_strip_all_tags($_POST["text"]),
"post_status"   => "publish"
);

$post_id = wp_update_post( $post );

if ( $post_id ) {
wp_redirect( home_url("list-posts/?status=edit") );
exit;
}
}

Bearbeiten Sie stattdessen den Beitrag. WP erstellt einen neuen. Das ist weil $current_post Wert werden nicht an functions.php gesendet. Wie kann ich das tun, ohne die Post-ID zu speichern?

AKTUALISIEREN

Problem gelöst:

function edit_post_foo($query){
$id = get_the_id();

$post = array(
"ID" => $id,
"post_type"     => "post",
"post_title"    => wp_strip_all_tags($_POST["title"]),
"post_content"  => wp_strip_all_tags($_POST["text"]),
"post_status"   => "publish"
);

$post_id = wp_update_post( $post );

if ( $post_id ) {
wp_redirect( $_SERVER["HTTP_REFERER"] );
exit;
}
}

if( "POST" == $_SERVER["REQUEST_METHOD"] && !empty( $_POST["edit_post"] )) {

if ( ! isset( $_POST["edit_post_nonce"] ) ) {
return;
}

if ( ! wp_verify_nonce( $_POST["edit_post_nonce"], "edit_post" ) ) {
return;
}

if ( isset( $_POST["post_type"] ) && "post" == $_POST["post_type"] ) {

if ( ! current_user_can( "edit_posts" ) ) {
return;
}
}

if(empty(trim($_POST["title"]))){
$erro = "inform a title.";
return;
}

if(empty(trim($_POST["text"]))){
$erro = "sen a text.";
return;
}

add_action("wp", "edit_post_foo");
}

Antworten:

1 für die Antwort № 1

Ich finde vielleicht eine Lösung.

Fügen Sie in Ihrem Formular zunächst einen Eingabetyp mit name = "action" und value = "your_custom_function" hinzu.

Dann

Erstellen Sie Ihre Funktion in Ihrer functions.php

function your_function($query)
{
$id = get_the_id();

// do something

wp_redirect( $_SERVER["HTTP_REFERER"] );
exit;
}
if( isset($_POST["action"]) && $_POST["action"] == "your_custom_value" )
add_action("wp", "your_function");

In diesem Moment können Sie die aktuelle ID des aktuellen Beitrags abrufen. Dann aktualisiere deinen Beitrag. Die ID bleibt für den Benutzer verborgen.

Es funktioniert nur, wenn Sie das Formular auf der Seite des Beitrags senden.


0 für die Antwort № 2

Ich kann Ihnen ein Tutorial vorschlagen, das erklärt, wie Sie einen neuen Beitrag vom Frontend erstellen.

@Hier

Fügen Sie einfach eine ausgeblendete Aktion in Ihr Formular ein und erstellen Sie dann eine Funktion mit dem Wert der ausgeblendeten Eingabe.

Vergessen Sie nicht, die post_id im post-Array zu übergeben.


0 für die Antwort № 3

Ändern Sie den Code wie folgt und es sollte funktionieren:

<form action="" method="post">
<input type="text" name="title" value="<?php echo $title; ?>">
<textarea name="text"><?php echo $content; ?></textarea>
<input type="hidden" name="postID" value="<?php echo get_the_ID(); ?>">
<input type="submit" value="edit post">
<?php wp_nonce_field( "edit_post", "edit_post_nonce" ); ?>
<input type="hidden" name="edit_post" value="true" />
</form>

<?php
if( "POST" == $_SERVER["REQUEST_METHOD"] && !empty( $_POST["edit_post"] )) {
$current_post = $_POST["postID"];

$post = array(
"ID" => $current_post,
"post_type"     => "post",
"post_title"    => wp_strip_all_tags($_POST["title"]),
"post_content"  => wp_strip_all_tags($_POST["text"]),
"post_status"   => "publish"
);

$post_id = wp_update_post( $post );

if ( $post_id ) {
wp_redirect( home_url("list-posts/?status=edit") );
exit;
}
}
?>

Wenn dies für einen Front-End-Editor ist, sollten Sie die Verwendung in Betracht ziehen WP-Editor () anstelle einer Textfläche.