问题描述
我有一个 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');
?>
我有一个简单的 php 表单,它具有以下功能:-
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.';
但是如果'middle'的字段为空,它将忽略$description的整个值,如果'middle'的字段为空,我希望它只忽略第一句并显示具有该字段的第二句结束"即
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.';
如何让它像这样工作?
推荐答案
遵循如下方法将使整个事情变得更好,更有意义.基本上,将描述变量设置为第一句,如果存在则添加第二位.
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.
这篇关于直接发布/草稿帖子的 Wordpress 前端表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!