doujia1163 2012-07-03 06:21
浏览 115
已采纳

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

I have a Wordpress Front End Form to Publish/Draft Posts directly from my theme: -

<?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');

?>

and I have a simple php form which has the following function: -

<?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.";
}
?>

I wanted to include it in the form and I did it using the method below: -

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

but it will ignore the whole value of $description if the field of 'middle' is empty and I want it to ignore just the first sentence if the field of 'middle' is empty and display the second sentence which has the field of 'end' i.e.

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

How to make it work like this?

  • 写回答

2条回答 默认 最新

  • dongyanghan0556 2012-07-03 15:03
    关注

    Following a method like below will make the whole thing flow better and make more sense. Basically, setting the description variable to the first sentence and adding the second bit if it exists.

    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
    }
    

    Also, think about escaping the strings depending what you are using it for.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作