WordPress的前端形式直接发布/帖子草案(Wordpress Front End Form t

2019-09-19 01:10发布

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

但它会忽略$描述的整个价值如果“中间”字段为空,我想它忽略只是第一句话如果“中间”字段为空,并显示具有“端领域中的第二句“即

'a sentence ' . $_POST['end'] . ' with something in the END.';

如何使它像这样的工作吗?

Answer 1:

下面的方法如下面将会使整个事情流动性更好和更有意义。 基本上,设置描述变量到第一句子以及将所述第二位,如果它的存在。

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
}

此外,想想逃避根据您使用它什么字符串。



Answer 2:

更改:

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


文章来源: Wordpress Front End Form to Publish/Draft Posts directly