I have created a form using wordpress which returns the title and description. I need to return a few more custom fields. I have searched the internet and found many answers which did not explain it very well and they did not end up working for me. The form would return a post with title, description, the custom fields already filled in. Thanks!
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == "new_post") {
// Do some minor form validation to make sure there is content
if (isset ($_POST['title'])) {
$title = $_POST['title'];
} else {
echo 'Please enter the wine name';
}
if (isset ($_POST['description'])) {
$description = $_POST['description'];
} else {
echo 'Please enter some notes';
}
// ADD THE FORM INPUT TO $new_post ARRAY
$new_post = array(
'post_title' => $title,
'post_content' => $description,
'post_Blog_URL' => $URL,
'post_status' => 'publish', // Choose: publish, preview, future, draft, etc.
'post_type' => 'post' //'post',page' or use a custom post type if you want to
);
add_post_meta($post_id, $meta_key, 'URL' , $unique);
//SAVE THE POST
$pid = wp_insert_post($new_post);
//REDIRECT TO THE NEW POST ON SAVE
$link = get_permalink( $pid );
wp_redirect( $link );
} // END THE IF STATEMENT THAT STARTED THE WHOLE FORM
do_action('wp_insert_post', 'wp_insert_post');
Submit your own!
<div id="postbox">
<form id="new_post" name="new_post" method="post" action="" class="" enctype="multipart/form-data">
<!-- post name -->
<fieldset name="name">
<label>Name of the Article</label>
<input type="text" id="title" value="" tabindex="1" size="20" name="title" />
</fieldset>
<!-- post Content -->
<fieldset class="content">
<label for="description">Description</label>
<textarea id="description" tabindex="15" name="description"></textarea>
</fieldset>
<button type="submit">Submit</button>
<input type="hidden" name="action" value="new_post" />
<?php wp_nonce_field( 'new-post' ); ?>
</form>
If I understand you correctly, you want to be able to show Post Meta Fields in your form.
There are a few functions to set, update and retrieve these which will be helpful:
get_post_meta or get_post_custom
add_post_meta
update_post_meta
With those functions, you can create any kind of meta information for posts and display them either in the admin panel or to the end user, depending on the scenario.
Related
I am trying to create a custom plugin in which form is submitting on first option page added.
I want to fetch posts from database on the basis of submitted data and show on other page in wp-admin to further mark these posts to add some more data with these posts.
Problem: Upon submitting a form to action added, this shows only the values of $_POST on a broken page instead of proper admin page.
Question: how to show fetched data on proper admin page?
function listposts_register_lists_page() {
//brings all posts at first time
add_options_page('Set Message in Selected Posts', 'Alert Messages', 'manage_options', 'listposts_plugin', 'listposts_lists_page'); //listposts_option_page
//Triggers upon Add New Button
add_options_page( 'Set Message in Selected Posts', '', 'manage_options', 'add-new-message', 'add_new_message_page');
}
add_action('admin_menu', 'listposts_register_lists_page');
Form
function add_new_message_page()
{
?>
<div class="wrap">
<h1>Set Alert Message</h1>
<form method='POST' action='<?php echo admin_url( 'admin-post.php' ); ?>'>
<input type='hidden' name='action' value='fetch_posts'/>
<?php wp_nonce_field( 'submitform', 'submitform_nonce' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Message</th>
<td><textarea id="listposts_option_name" name="listposts_option_name" value="<?php echo get_option('listposts_option_name'); ?>" ></textarea></td>
</tr>
<?php
// Excluded CPTs. You can expand the list.
$exclude_cpts = array(
'attachment'
);
// Builtin types needed.
$builtin = array(
'post',
'page',
// 'attachment'
);
// All CPTs.
$cpts = get_post_types( array(
'public' => true,
'_builtin' => false
) );
// remove Excluded CPTs from All CPTs.
foreach($exclude_cpts as $exclude_cpt)
unset($cpts[$exclude_cpt]);
// Merge Builtin types and 'important' CPTs to resulting array to use as argument.
$post_types = array_merge($builtin, $cpts);
//$post_types = get_post_types( $args, $output, $operator );
foreach ( $post_types as $post_type ) {
echo ' <tr valign="top">
<td><input type="checkbox" class="selectposttype" name="posttypes" value="'.$post_type.'" '.checked( $post_type, 1 ).' /></td>
<td scope="row"><label for="'.$post_type.'" >'.$post_type.'</label></td>
</tr>';
}
?>
</table>
<input type="submit" value="Send"/>
</form>
</div>
<?php
}
Function to handle form submit
function show_posts(){
//This should be displayed on proper Option page instead of white page
var_dump($_POST);
}
Here how to render this out put on proper wp-admin page in WP_LIST_TABLE ?
I am creating form for wordpress website. Currently i am getting value on same page successfully with blank action.
Now i need to get form value on data.php page.
Form
<form id="post-container" action="<?php echo home_url( '/' ); ?>data.php" method="POST">
<input name="aa" value="post_id"> ........ </input>
<input name="ab" value="post_id"> ........ </input>
<input type="submit" name="submit" value="submit">
</form>
data.php
<?php
if (isset ( $_POST['aa'] )){
$ID = $_POST['aa'] ;
echo get_post_meta( $ID, 'brand', true );
}
if (isset ( $_POST['ab'] )){
$ID = $_POST['ab'] ;
echo get_post_meta( $ID, 'brand', true );
}
?>
After submit
url = http://localhost/mobile/data.php which is correct.
but it display index.php which is wrong and also not print any value.
How to submit form to data.php and print value?
Wordpress have protection from POST, GET requests..
I have some links about it
wordpress-jquery-contact-form-without-a-plugin
create-a-submit-form-in-wordpress
add_query_arg
how-to-submit-a-form-in-wordpress
I am using the below code to allow a logged in user to set delete their own posts from the front end. Is there a way to do the same thing but setting the post to 'draft' rather than deleting it completely?
<?php if ($post->post_author == $current_user->ID) { ?>
<p><a onclick="return confirm('Are you SURE you want to delete this?')" href="<?php echo get_delete_post_link( $post->ID ) ?>">Delete post</a></p>
<?php } ?>
use this function wp_update_post(), you can test with this example:
First create a form were you want to users select if the post are going to be a draft
<form action="" method="POST" >
<input type="checkbox" value="ok" name="draft">
<input type="submit" value="Ok">
</form>
Then create a function to save the new state put this in function.php:
function toDraft($pid){
$toDraft = $_POST['draft'];
if($toDraft == 'ok'){
echo "string";
wp_update_post(array('ID' => $pid, 'post_status' => 'draft'));
}
}
Then add this function below the form you create.
toDraft($post->ID);
And test. Read this to know more about update post status
I have a website with a blog and a custom post_type for Videos (named video). Along with various taxonomies attached to it (Video Categories, Video Tags, etc.)
I am trying to set up a search function to search just the video taxononmy and another to search just the blog taxonomy. There will be a search box in each of these pages to slim down the results.
Here is what I have done so far.
<aside id="sub-search" class="widget widget_search">
<form class="search-form" action="http://www.studiobenna.com/jf/" method="get" role="search">
<label>
<span class="screen-reader-text">Search for:</span>
<input class="search-field" type="search" name="s" value="long" placeholder="Search Videos">
</label>
<input type="hidden" name="post_type" value="video" />
<input class="search-submit" type="submit" value="Search">
</form>
</aside>
Which results in the url ending in: http://example.com/?s=video&post_type=video
But this doesn't filter only the video taxonomy. I have one that references post_type=post for the regular blog search.
What is the correct way to query the Wordpress search function in the URL to only return one post type? I am using the WP Extended Search plugin to allow a search box at the top right of the screem to search the entire site.
I also want these searches to be limited to post type but also pickup any categories and tags attached to them (I don't know if this is any extra step).
An example of what I am doing is here http://www.studiobenna.com/jf/?page_id=8 in the search box next to browse. If you type in Blog here there should be only one result title "Great Western Loop" but others come back.
I have tried adding this to my functions.php:
function mySearchFilter($query) {
$post_type = $_GET['post_type'];
if (!$post_type) {
$post_type = 'any';
}
if ($query->is_search) {
$query->set('post_type', $post_type);
};
return $query;
};
add_filter('pre_get_posts','mySearchFilter');
But it doesn't work.
I also tried adding this to the search.php page right before the if (have_posts) loop:
<?php
if(isset($_GET['post_type'])) {
$type = $_GET['post_type'];
$args = array( 'post_type' => $type );
$args = array_merge( $args, $wp_query->query );
query_posts( $args );
}
?>
Still nothing.
Everything is correct except the post_type set of the query.
This will work correctly for your case:
function mySearchFilter( $query ) {
$post_type = $_GET['post_type'];
if (!$post_type) {
$post_type = 'any';
}
if ( $query->is_search ) {
$query->set( 'post_type', array( esc_attr( $post_type ) ) );
}
return $query;
}
add_filter( 'pre_get_posts', 'mySearchFilter' );
I'm currently creating a simple CMS and I would really, really like to use the editor and mainly, image uploader from WordPress on my site. Is this possible?
I am aware that WordPress uses TinyMCE and that they do offer an image manager as a commercial add-on, which I will probably use if this is not possible.
You can use the wp function the_editor:
http://codex.wordpress.org/Function_Reference/the_editor
If you look up that function on google you'll find a bunch of pages describing how to insert the wordpress editor into plug-ins. They provide lots of different ways of doing what you're hopefully looking for.
I used something like:
<form id="new_post" name="new_post" method="post" action="" enctype="multipart/form-data">
<div><h2>Title</h2>
<input type="text" id="title" value="" tabindex="1" name="title" AUTOCOMPLETE=OFF/>
<div>
<h2>Description</h2>
<?php the_editor('', 'description', 'title', true); ?>
</div>
<input type="hidden" name="action" value="post" />
<p align="right"><input type="submit" value="Publish" tabindex="6" id="submit" name="submit" /></p>
<?php wp_nonce_field( 'new-post' ); ?>
</form>
Which you then have to save to the wordpress db using something like:
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] )) {
// Do some minor form validation to make sure there is content
if (isset ($_POST['title'])) {
$title = $_POST['title'];
} else {
echo 'Please enter a title';
}
if (isset ($_POST['description'])) {
$description = $_POST['description'];
} else {
echo 'Please enter the content';
}
// Add the content of the form to $post as an array
$post = array(
'post_title' => $title,
'post_content' => $description,
/* 'post_category' => array('cat' => '3'), */ // Usable for custom taxonomies too
'post_status' => 'pending', // Choose: publish, pending, draft, auto-draft, future, etc.
'post_type' => 'post' // Use a custom post type if you want to
);
$newID = wp_insert_post($post); // Pass the value of $post to WordPress the insert function
// http://codex.wordpress.org/Function_Reference/wp_insert_post
// wp_redirect( home_url() );
} // end IF
// Do the wp_insert_post action to insert it
do_action('wp_insert_post', 'wp_insert_post');
That's the basics of it... Good luck!
Without knowing the details of your project, I can offer this as a starting point:
With respect to the image uploader, WordPress utilizes some version of the free upload handler Plupload in the current version of WP (3.3), which will handle image uploads.
http://www.plupload.com
If you are looking for features such as crop, resize, thumbnail, etc., then you are right - they do have a paid file and image managers (MCImageManager)
http://www.tinymce.com/enterprise/mcimagemanager.php