Php Wordpress前端表单直接发布/起草帖子

Php Wordpress前端表单直接发布/起草帖子,php,forms,wordpress,frontend,Php,Forms,Wordpress,Frontend,我有一个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

我有一个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');

?>
但是如果'middle'字段为空,它将忽略$description的整个值,如果'middle'字段为空,我希望它只忽略第一句话,并显示第二句话,其中有'end'字段,即

'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.';
致:


遵循下面这样的方法将使整个流程更好,更有意义。基本上,将description变量设置为第一句话,并添加第二位(如果存在)

if(!empty($_POST['middle'])) {
   $description = "a sentence".$_POST['middle']." with something in the MIDDLE.";
}

if(!empty($_POST['end'])) {
   $description .= "a sentence".$_POST['end']." with something in the END.";
}

if(isset($description)) {
// do something with description
}

另外,根据您使用字符串的目的,考虑对字符串进行转义。

如果我有一个“开始”字段作为开始句子,并且它是必填字段,那么用户将不得不填充它,该怎么办?我应该在上面的代码之前用以下简单的方式添加它吗<代码>$description=“一句话“$\u POST['start']”。开头有东西。”谢谢你的回答,你能看看这个关于php和MySQL的问题吗。。。你能告诉我吗?
if(!empty($_POST['middle'])) {
$description = 'a sentence ' . $_POST['middle'] . ' with something in the MIDDLE. a sentence ' . $_POST['end'] . ' with something in the END.';
$description = (!empty($_POST['middle']))? 'a sentence ' . $_POST['middle'] . ' with something in the MIDDLE.': '' ;
$description .= (!empty($_POST['end']))? 'a sentence ' . $_POST['end'] . ' with something in the END.': '' ;
if(!empty($_POST['middle'])) {
   $description = "a sentence".$_POST['middle']." with something in the MIDDLE.";
}

if(!empty($_POST['end'])) {
   $description .= "a sentence".$_POST['end']." with something in the END.";
}

if(isset($description)) {
// do something with description
}