Editable Custom fields
-
Hi There WP lovers,
I am very new to WordPress, dabbled in PHP on and off for a couple of years… so please bare with me. This is also my first post on wp.org ever.
So here it goes…
I have recently taken over a website, that is built in WordPress. It is quite a complicated site. Apart from adding to the website, I’m also trying to fix it (Not that its broken) by illuminating most of the plugins and re-writing the functionality into the theme.
I have managed to do most of it myself. But one of the plugins that is used, is the Post Type Re-order plugin. it does exactly what I need it to do, but for a ‘business directory’ type website, to instruct users to go to a separate page just to rearrange there posts, which are ‘Paintings'(A custom post type) in this case.
I have managed to create the custom meta, display, edit(via post.php) and orderby the custom meta_value on the frontend.
What I would like to do is have a input field within the custom column.
Here is a screenshot of what I currently have:
HEREThen when I have sorted out the inline-editing/saving, I would like to have a function that checks against the other “re-order” values and adjusts them based on the row that was changed, to avoid duplicate values.
Here is the code I currently have, you will see my attempt at using ajax-saving (Which also doesn’t work):
<?php<br /> //EDIT meta box added to post edit screen<br /> add_action("admin_init", "admin_init");<br /> function admin_init(){<br /> $screens = array( 'painting', 'page' );<br /> foreach ($screens as $screen) {<br /> add_meta_box(<br /> 'painting_ro_sectionid',<br /> __( 'Number', 'painting_ro_textdomain' ),<br /> 'painting_ro_inner_custom_box',<br /> $screen<br /> );<br /> }<br /> }</p> <p>//ADD and replace columns in edit.php for custom post types<br /> add_filter('manage_edit-painting_columns', 'add_new_painting_columns');<br /> function add_new_painting_columns($painting_columns) {<br /> $new_columns['cb'] = '<input type="checkbox" />';<br /> $new_columns['title'] = _x('Painting Name', 'column name');<br /> $new_columns['number'] = __('Number', 'column name');<br /> $new_columns['images'] = __('Images');<br /> $new_columns['author'] = __('Author');<br /> $new_columns['date'] = _x('Date', 'column name');<br /> return $new_columns;<br /> }</p> <p>add_action('manage_painting_posts_custom_column', 'manage_painting_columns', 10, 2);<br /> function manage_painting_columns($column_name, $id) {<br /> global $wpdb;<br /> switch ($column_name) {<br /> case "number":<br /> $custom = get_post_custom($post->ID);<br /> $painting_ro = $custom['_painting_ro_my_meta_value_key'][0];<br /> ?><br /> <script type="text/javascript"><br /> var j = jQuery.noConflict();<br /> j('submit').click(function() {<br /> var formValues = j("#update_ro").serialize();<br /> var data = { type: 'save', action: 'painting_ro_save_postdata_ajax', data: formValues }<br /> j.post( ajaxurl, data, function(message){<br /> alert('Saved');<br /> });<br /> return false;<br /> });<br /> </script><br /> <form id="update_ro"><br /> <label>Change Order:</label><br /> <input type="text" name="painting_ro_new_field" value="<?php echo $painting_ro; ?>"><br /> <?php submit_button(); ?><br /> </form><br /> <?php<br /> break;<br /> case 'images':<br /> $num_images = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->posts WHERE post_parent = {$id};"));<br /> echo $num_images;<br /> break;<br /> default:<br /> break;<br /> }<br /> }</p> <p>add_action( 'save_post', 'painting_ro_save_postdata' );<br /> add_action('wp_ajax_save_product', 'painting_ro_save_postdata_ajax');</p> <p>function painting_ro_inner_custom_box( $post ) {<br /> wp_nonce_field( plugin_basename( __FILE__ ), 'painting_ro_noncename' );<br /> $value = get_post_meta( $post->ID, $key = '_painting_ro_my_meta_value_key', $single = true );<br /> echo '<label for="painting_ro_new_field">';<br /> _e("Add a reorder number", 'painting_ro_textdomain' );<br /> echo '</label> ';<br /> echo '<input type="text" id="painting_ro_new_field" name="painting_ro_new_field" value="'.($value).'" size="25" />';<br /> }</p> <p>function painting_ro_save_postdata_ajax( $post_id ) {<br /> global $wpdb;</p> <p> $post_ID = $_POST['post_ID'];<br /> $mydata = sanitize_text_field( $_POST['painting_ro_new_field'] );</p> <p> add_post_meta($post_ID, '_painting_ro_my_meta_value_key', $mydata, true) or<br /> update_post_meta($post_ID, '_painting_ro_my_meta_value_key', $mydata);</p> <p> update_post_meta($post->ID, "_painting_ro_my_meta_value_key", $_POST["_painting_ro_my_meta_value_key"]);<br /> echo 'Meta Updated';<br /> die();<br /> }</p> <p>function painting_ro_save_postdata( $post_id ) {<br /> global $wpdb;<br /> $post_ID = $_POST['post_ID'];<br /> $mydata = sanitize_text_field( $_POST['painting_ro_new_field'] );</p> <p> add_post_meta($post_ID, '_painting_ro_my_meta_value_key', $mydata, true) or<br /> update_post_meta($post_ID, '_painting_ro_my_meta_value_key', $mydata);</p> <p> update_post_meta($post->ID, "_painting_ro_my_meta_value_key", $_POST["_painting_ro_my_meta_value_key"]);<br /> }</p> <p>?>
I am such a noob to WP, that I’m not even sure if what i am asking is possible.
If anyone can point me in the right direction to achieve what i’m asking, that would be great ??
Thanks in advance
- The topic ‘Editable Custom fields’ is closed to new replies.