I inserted the following code, see below, to have a post submitted from the front-end. When you submit it, this warning appears:
Warning: array_filter() expects parameter 1 to be an array, string given in [...]/wp-includes/post.php on line 3148
And I can't figure out where the warning come from, which array is a string?
function ty_front_end_form() {
?>
<form id="custom-post-type" name="custom-post-type" method="post" action="">
<p><label for="title">Post Title</label><br />
<input type="text" id="title" value="" tabindex="1" size="20" name="title" />
</p>
<p><label for="description">Post Description</label><br />
<textarea id="description" tabindex="3" name="description" cols="50" rows="6"></textarea>
</p>
<p><?php wp_dropdown_categories( 'show_option_none=Category&tab_index=4&taxonomy=category' ); ?></p>
<p><label for="post_tags">Post Tags</label>
<input type="text" value="" tabindex="5" size="16" name="post-tags" id="post-tags" /></p>
<p align="right"><input type="submit" value="Publish" tabindex="6" id="submit" name="submit" /></p>
<input type="hidden" name="post-type" id="post-type" value="custom_posts" />
<input type="hidden" name="action" value="custom_posts" />
<?php wp_nonce_field( 'name_of_my_action','name_of_nonce_field' ); ?>
</form>
<?php
if($_POST){
ty_save_post_data();
}
}
add_shortcode('custom-post','ty_front_end_form');
function ty_save_post_data() {
if ( empty($_POST) || !wp_verify_nonce($_POST['name_of_nonce_field'],'name_of_my_action') )
{
print 'Sorry, your nonce did not verify.';
exit;
}
else{
// Do some minor form validation to make sure there is content
if (isset ($_POST['title'])) {
$title = $_POST['title'];
} else {
echo 'Please enter a title';
exit;
}
if (isset ($_POST['description'])) {
$description = $_POST['description'];
} else {
echo 'Please enter the content';
exit;
}
if(isset($_POST['post_tags'])){
$tags = $_POST['post_tags'];
}else{
$tags = "";
}
// Add the content of the form to $post as an array
$post = array(
'post_title' => wp_strip_all_tags( $title ),
'post_content' => $description,
'post_category' => $_POST['cat'], // Usable for custom taxonomies too
'tags_input' => $tags,
'post_status' => 'draft', // Choose: publish, preview, future, etc.
'post_type' => 'listings' // Use a custom post type if you want to
);
wp_insert_post($post); //
$location = home_url(); // redirect location, should be login page
echo "<meta http-equiv='refresh' content='0;url=$location' />"; exit;
} // end IF
}
I inserted the following code, see below, to have a post submitted from the front-end. When you submit it, this warning appears:
Warning: array_filter() expects parameter 1 to be an array, string given in [...]/wp-includes/post.php on line 3148
And I can't figure out where the warning come from, which array is a string?
function ty_front_end_form() {
?>
<form id="custom-post-type" name="custom-post-type" method="post" action="">
<p><label for="title">Post Title</label><br />
<input type="text" id="title" value="" tabindex="1" size="20" name="title" />
</p>
<p><label for="description">Post Description</label><br />
<textarea id="description" tabindex="3" name="description" cols="50" rows="6"></textarea>
</p>
<p><?php wp_dropdown_categories( 'show_option_none=Category&tab_index=4&taxonomy=category' ); ?></p>
<p><label for="post_tags">Post Tags</label>
<input type="text" value="" tabindex="5" size="16" name="post-tags" id="post-tags" /></p>
<p align="right"><input type="submit" value="Publish" tabindex="6" id="submit" name="submit" /></p>
<input type="hidden" name="post-type" id="post-type" value="custom_posts" />
<input type="hidden" name="action" value="custom_posts" />
<?php wp_nonce_field( 'name_of_my_action','name_of_nonce_field' ); ?>
</form>
<?php
if($_POST){
ty_save_post_data();
}
}
add_shortcode('custom-post','ty_front_end_form');
function ty_save_post_data() {
if ( empty($_POST) || !wp_verify_nonce($_POST['name_of_nonce_field'],'name_of_my_action') )
{
print 'Sorry, your nonce did not verify.';
exit;
}
else{
// Do some minor form validation to make sure there is content
if (isset ($_POST['title'])) {
$title = $_POST['title'];
} else {
echo 'Please enter a title';
exit;
}
if (isset ($_POST['description'])) {
$description = $_POST['description'];
} else {
echo 'Please enter the content';
exit;
}
if(isset($_POST['post_tags'])){
$tags = $_POST['post_tags'];
}else{
$tags = "";
}
// Add the content of the form to $post as an array
$post = array(
'post_title' => wp_strip_all_tags( $title ),
'post_content' => $description,
'post_category' => $_POST['cat'], // Usable for custom taxonomies too
'tags_input' => $tags,
'post_status' => 'draft', // Choose: publish, preview, future, etc.
'post_type' => 'listings' // Use a custom post type if you want to
);
wp_insert_post($post); // http://codex.wordpress/Function_Reference/wp_insert_post
$location = home_url(); // redirect location, should be login page
echo "<meta http-equiv='refresh' content='0;url=$location' />"; exit;
} // end IF
}
Share
Improve this question
edited Dec 13, 2017 at 13:18
bueltge
17.1k7 gold badges62 silver badges97 bronze badges
asked Dec 13, 2017 at 13:02
EnzoEnzo
518 bronze badges
1
- You should always add the code, if is necessary for the question. If users read it later and the external link is broken is the q/a not helpful. – bueltge Commented Dec 13, 2017 at 13:19
1 Answer
Reset to default 3If you check the line from the error notice you should find in WP 4.9.1 the usage of the function array_filter
, that need a array as parameter (see the code).
Your value inside your array for an post need an array for 'post_category'
, is currently a string.
From the source of the WP core:
* @type array $post_category Array of category names, slugs, or IDs.
* Defaults to value of the 'default_category' option.
In your source example you should change this and it should work, 'post_category' => array( $_POST['cat'] ),
.
// Add the content of the form to $post as an array
$post = array(
'post_title' => wp_strip_all_tags( $title ),
'post_content' => $description,
'post_category' => array( $_POST['cat'] ), // Usable for custom taxonomies too
'tags_input' => $tags, // Need an array
'post_status' => 'draft', // Choose: publish, preview, future, etc.
'post_type' => 'listings', // Use a custom post type if you want to
);
wp_insert_post( $post );
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745065877a4609251.html
评论列表(0条)