テーマから直接投稿を公開/ドラフトするためのWordpressフロントエンドフォームがあります:-
<?php
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == "new_post") {
// Do some minor form validation to make sure there is content
$title = $_POST["title"];
if(!empty($_POST['middle'])) {
$description = 'a sentence ' . $_POST['middle'] . ' with something in the MIDDLE. a sentence ' . $_POST['end'] . ' with something in the END.';
}
$tags = $_POST["tags"];
$post_cat = $_POST['cat'];
// ADD THE FORM INPUT TO $new_post ARRAY
$new_post = array(
'post_title' => $title,
'post_content' => $description,
'post_category' => $post_cat, // Usable for custom taxonomies too
'tags_input' => $tags,
'post_status' => 'draft', // Choose: publish, preview, future, draft, etc.
'post_type' => 'post', //'post',page' or use a custom post type if you want to
);
//SAVE THE POST
$pid = wp_insert_post($new_post);
//REDIRECT TO THE NEW POST ON SAVE
$link = get_permalink( $pid );
wp_redirect( '/post-submitted-draft' );
} // END THE IF STATEMENT THAT STARTED THE WHOLE FORM
//POST THE POST YO
do_action('wp_insert_post', 'wp_insert_post');
?>
そして、私は次の機能を持つ単純なphpフォームを持っています: -
<?php
if(!empty($_POST['middle'])) {
echo "a sentence".$_POST['middle']." with something in the MIDDLE.";
}
if(!empty($_POST['end'])) {
echo "a sentence".$_POST['end']." with something in the END.";
}
?>
私はそれをフォームに含めたかったので、以下の方法を使用してそれを行いました: -
if(!empty($_POST['middle'])) {
$description = 'a sentence ' . $_POST['middle'] . ' with something in the MIDDLE. a sentence ' . $_POST['end'] . ' with something in the END.';
しかし、「middle」のフィールドが空の場合は $description の値全体を無視し、「middle」のフィールドが空の場合は最初の文だけを無視し、「end」のフィールドを持つ 2 番目の文を表示するようにしたい' すなわち
'a sentence ' . $_POST['end'] . ' with something in the END.';
このように機能させるにはどうすればよいですか?