I've set up a quick edit to handle the custom fields I've created with Types.
I can populate the quick edit fields with the current content but have no ability to update it via quick edit.
I've ensured the fields I'm trying to update have the wpcf- but still have no success...any suggestions on how to get this to work?
Here is the code I am currently using
dd_action( 'save_post', 'manage_wp_posts_be_qe_save_post', 10, 2 );
function manage_wp_posts_be_qe_save_post( $post_id, $post ) {
// pointless if $_POST is empty (this happens on bulk edit)
if ( empty( $_POST ) )
return $post_id;
// verify quick edit nonce
if ( isset( $_POST[ '_inline_edit' ] ) && ! wp_verify_nonce( $_POST[ '_inline_edit' ], 'inlineeditnonce' ) )
return $post_id;
// don't save for autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// dont save for revisions
if ( isset( $post->post_type ) && $post->post_type == 'revision' )
return $post_id;
switch( $post->post_type ) {
case 'jackpots':
/**
* Because this action is run in several places, checking for the array key
* keeps WordPress from editing data that wasn't in the form, i.e. if you had
* this post meta on your "Quick Edit" but didn't have it on the "Edit Post" screen.
*/
$custom_fields = array( 'wpcf-prize-value', 'wpcf-jackpot-numbers' );
foreach( $custom_fields as $field ) {
if ( array_key_exists( $field, $_POST ) )
update_post_meta( $post_id, $field, $_POST[ $field ] );
}
break;
}
}
Dear Malcolm,
Thanks for the details and for posting the code. I'm reviewing and checking this one now. I will update shortly. 🙂
Cheers,
Emerson
Dear Malcolm,
You have not provided information as to how you are setting up a quick edit to handle/display the custom fields created with Types. So I cannot replicate your PHP code (to save the fields) exactly in my test server.
Anyway I have tested your case and it seems I have found some methods that will work. You will need to use http://wordpress.org/plugins/custom-bulkquick-edit/ plugin along with Types if you want to edit custom fields on Quick Edit. The free version of custom-bulkquick-edit works with posts and pages (for custom post types you need the premium version).
Anyway I have tested their free version by updating custom fields on a posts on quick edit and it works. This is how I do it (which I suggest you should try):
Step1.) Start with a clean WordPress install.
Step2.) Activate your preferred theme.
Step3.) Activate Types plugin (version 1.6.2)
Step4.) Activate Custom Bulk/Quick Edit by Aihrus plugin (version 1.5.2)
Step5.) Create a custom field group "Quick edit test fields" and a custom field named as: "Test line field".
Please see attached screenshot on how these fields are created.
Step6.) In your active theme functions.php, add the following code:
[php]
add_filter( 'manage_post_posts_columns', 'my_manage_customfield_posts_columns' );
function my_manage_customfield_posts_columns( $columns ) {
$columns['wpcf-test-line-field'] = esc_html__( 'Test line field');
return $columns;
}
[/php]
The above code is based on this documentation: hidden link
Step7.) Go to Settings --> Custom Bulk/Quick.
Step8.) Under "Posts" tab, scroll down and make sure "Enable "Test line field"?" is set to "As input field" (see attached screenshot).
Step9.) Go to "Posts" --> "All Posts", you will see the default "Hello World" post.
Step10.) Click "Quick Edit" and then there , fill up a value of "Test line field". See related screenshot attached.
Step11.) After clicking "Update" it should be able to save this field. (see also attached screenshot).
The only custom code I added is in Step6. I didn't add any custom code to save the fields when using the Custom Bulk Quick Edit plugin. This should work also for you. Please let me know how it goes. Thanks 🙂
Cheers,
Emerson
Thanks Emerson...but...I can't use that plugin (tried to install it, need a newer version of PHP). So instead I found a good tutorial on setting up the quick edit by Rachel Cardon.
Here is all of the code I am using to make it work...and for the most part, it does! As I said, it just doesn't seem to be able to save any quick edit changes and I can't figure out why?
/***********QUICK EDIT FOR JACKPOTS**************/
add_filter( 'manage_posts_columns', 'jackpot_columns', 10, 2 );
function jackpot_columns( $columns, $post_type ) {
if ( $post_type == 'jackpots' )
$columns[ 'prize-value' ] = 'Prize Value';
$columns[ 'jackpot-numbers' ] = 'in #\'s';
return $columns;
}
add_action( 'manage_posts_custom_column', 'jackpot_column', 10, 2 );
function jackpot_column( $column_name, $post_id ) {
switch( $column_name ) {
case 'prize-value':
echo '<div id="prize-value-' . $post_id . '">$' . get_post_meta( $post_id, 'wpcf-prize-value', true ) . '</div>';
break;
case 'jackpot-numbers':
echo '<div id="jackpot-numbers-' . $post_id . '">' . get_post_meta( $post_id, 'wpcf-jackpot-numbers', true ) . '</div>';
break;
}
}
add_action( 'bulk_edit_custom_box', 'jackpot_quick_edit_custom_box', 10, 2 );
add_action( 'quick_edit_custom_box', 'jackpot_quick_edit_custom_box', 10, 2 );
function jackpot_quick_edit_custom_box( $column_name, $post_type ) {
switch ( $post_type ) {
case 'jackpots':
switch( $column_name ) {
case 'prize-value':
?><fieldset class="inline-edit-col-left">
<div class="inline-edit-col">
<label>
<span class="title">Prize Value</span>
<span class="input-text-wrap">
<input type="text" value="" name="prize-value">
</span>
</label>
</div>
</fieldset><?php
break;
case 'jackpot-numbers':
?><fieldset class="inline-edit-col-left">
<div class="inline-edit-col">
<label>
<span class="title">in #'s</span>
<span class="input-text-wrap">
<input type="text" value="" name="jackpot-numbers">
</span>
</label>
</div>
</fieldset><?php
break;
}
break;
}
}
add_action( 'admin_print_scripts-edit.php', 'jackpot_enqueue_admin_scripts' );
function jackpot_enqueue_admin_scripts() {
wp_enqueue_script( 'manage-wp-posts-using-bulk-quick-edit', trailingslashit( get_bloginfo( 'stylesheet_directory' ) ) . 'library/js/bulk_quick_edit.js', array( 'jquery', 'inline-edit-post' ), '', true );
}
add_action( 'save_post', 'manage_wp_posts_be_qe_save_post', 10, 2 );
function manage_wp_posts_be_qe_save_post( $post_id, $post ) {
// pointless if $_POST is empty (this happens on bulk edit)
if ( empty( $_POST ) )
return $post_id;
// verify quick edit nonce
if ( isset( $_POST[ '_inline_edit' ] ) && ! wp_verify_nonce( $_POST[ '_inline_edit' ], 'inlineeditnonce' ) )
return $post_id;
// don't save for autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// dont save for revisions
if ( isset( $post->post_type ) && $post->post_type == 'revision' )
return $post_id;
switch( $post->post_type ) {
case 'jackpots':
/**
* Because this action is run in several places, checking for the array key
* keeps WordPress from editing data that wasn't in the form, i.e. if you had
* this post meta on your "Quick Edit" but didn't have it on the "Edit Post" screen.
*/
$custom_fields = array( 'wpcf-prize-value', 'wpcf-jackpot-numbers' );
foreach( $custom_fields as $field ) {
if ( array_key_exists( $field, $_POST ) )
update_post_meta( $post_id, $field, $_POST[ $field ] );
}
break;
}
}
add_action( 'wp_ajax_manage_wp_posts_using_bulk_quick_save_bulk_edit', 'manage_wp_posts_using_bulk_quick_save_bulk_edit' );
function manage_wp_posts_using_bulk_quick_save_bulk_edit() {
// we need the post IDs
$post_ids = ( isset( $_POST[ 'post_ids' ] ) && !empty( $_POST[ 'post_ids' ] ) ) ? $_POST[ 'post_ids' ] : NULL;
// if we have post IDs
if ( ! empty( $post_ids ) && is_array( $post_ids ) ) {
// get the custom fields
$custom_fields = array( 'wpcf-prize-value', 'wpcf-jackpot-numbers' );
foreach( $custom_fields as $field ) {
// if it has a value, doesn't update if empty on bulk
if ( isset( $_POST[ $field ] ) && !empty( $_POST[ $field ] ) ) {
// update for each post ID
foreach( $post_ids as $post_id ) {
update_post_meta( $post_id, $field, $_POST[ $field ] );
}
}
}
}
}
Please advise...
Dear Malcolm,
Thanks for posting your code. I'll check this one to see if there are any errors on it. I'll update later. Cheers.
Emerson
Dear Malcolm,
I found the bug in your code. You are checking for posting field names as "wpcf-prize-value"/"wpcf-jackpot-numbers" if they exist on $_POST. But actually you are posting the fields names from the quick edit as "prize-value" and "jackpot-numbers" . As a result, your code cannot find these fields and therefore does not get the posted value. This is the section in your code that is having this problem:
switch( $post->post_type ) {
case 'jackpots':
/**
* Because this action is run in several places, checking for the array key
* keeps WordPress from editing data that wasn't in the form, i.e. if you had
* this post meta on your "Quick Edit" but didn't have it on the "Edit Post" screen.
*/
$custom_fields = array( 'wpcf-prize-value', 'wpcf-jackpot-numbers' );
foreach( $custom_fields as $field ) {
if ( array_key_exists( $field, $_POST ) )
update_post_meta( $post_id, $field, $_POST[ $field ] );
}
break;
}
This is the fix that works for me in my local test:
switch( $post->post_type ) {
case 'jackpots':
/**
* Because this action is run in several places, checking for the array key
* keeps WordPress from editing data that wasn't in the form, i.e. if you had
* this post meta on your "Quick Edit" but didn't have it on the "Edit Post" screen.
*/
//BUG: Rename this to prize-value and jackpot-numbers to fix.
$custom_fields = array( 'prize-value', 'jackpot-numbers' );
foreach( $custom_fields as $field ) {
if ( array_key_exists( $field, $_POST ) )
//Append wpcf- prefix for Types compatibility
$field_types='wpcf-'.$field;
update_post_meta( $post_id, $field_types, $_POST[ $field ] );
}
break;
}
I changed the $custom_fields key values to the correct field names posted. But I created a new variable $field_types that will store the actual key field compatible with Types. So the code can locate this one on the dB and update it. That should fix the issue on updating these fields. Please let me know how it goes. Thanks 🙂
Cheers,
Emerson
Awesome catch Emerson! Works great. Thanks for the help!