• 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:
    HERE

    Then 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

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter 10PL8

    (@10pl8)

    No one… How am i supposed to as a novice, build my experience in WordPress – if no one is willing to help or at minimum say “Its not possible”

    Josh

    (@josh401)

    Hello,

    Frankly… that’s just WAY too much information. I doubt many people are going to take the time to read through all that. I don’t mean to be “negative Nancy”… but, really.

    Please try to narrow down your question. Ask a concise question.. provide only the absolute necessary info.. and I’m sure you will get an answer.

    Thread Starter 10PL8

    (@10pl8)

    Hi,

    Thanks for that Josh. Constructive guidelines. Will summarize and be more concise when submitting questions.

    “Keep it simple stupid”

    Josh

    (@josh401)

    Lol… exactly ??

    If you wanna ask here… I’m listening… just keep it as concise as possible ??

    Thread Starter 10PL8

    (@10pl8)

    Ok, here it goes. I am trying to order my custom post types by a custom post meta.

    Basically this works (via post.php) – Works

    and this doesn’t (via edit.php) – Not working

    And I am able to order the post by the custom meta value on frontend.

    My issue is updating/saving via edit.php, this is my code instantiated via require_once ‘plugins/extra-user-fields/sortable-columns.php’; in functions.php

    Code:
    `<?php
    //EDIT meta box added to post edit screen
    add_action(“admin_init”, “admin_init”);
    function admin_init(){
    $screens = array( ‘painting’, ‘page’ );
    foreach ($screens as $screen) {
    add_meta_box(
    ‘painting_ro_sectionid’,
    __( ‘Number’, ‘painting_ro_textdomain’ ),
    ‘painting_ro_inner_custom_box’,
    $screen
    );
    }
    }

    //ADD and replace columns in edit.php for custom post types
    add_filter(‘manage_edit-painting_columns’, ‘add_new_painting_columns’);
    function add_new_painting_columns($painting_columns) {
    $new_columns[‘cb’] = ‘<input type=”checkbox” />’;
    $new_columns[‘title’] = _x(‘Painting Name’, ‘column name’);
    $new_columns[‘number’] = __(‘Number’, ‘column name’);
    $new_columns[‘images’] = __(‘Images’);
    $new_columns[‘author’] = __(‘Author’);
    $new_columns[‘date’] = _x(‘Date’, ‘column name’);
    return $new_columns;
    }

    add_action(‘manage_painting_posts_custom_column’, ‘manage_painting_columns’, 10, 2);
    function manage_painting_columns($column_name, $id) {
    global $wpdb;
    switch ($column_name) {
    case “number”:
    $custom = get_post_custom($post->ID);
    $painting_ro = $custom[‘_painting_ro_my_meta_value_key’][0];
    ?>
    <script type=”text/javascript”>
    var j = jQuery.noConflict();
    j(‘submit’).click(function() {
    var formValues = j(“#update_ro”).serialize();
    var data = { type: ‘save’, action: ‘painting_ro_save_postdata_ajax’, data: formValues }
    j.post( ajaxurl, data, function(message){
    alert(‘Saved’);
    });
    return false;
    });
    </script>
    <form id=”update_ro”>
    <label>Change Order:</label>
    <input type=”text” name=”painting_ro_new_field” value=”<?php echo $painting_ro; ?>”>
    <?php submit_button(); ?>
    </form>
    <?php
    break;
    case ‘images’:
    $num_images = $wpdb->get_var($wpdb->prepare(“SELECT COUNT(*) FROM $wpdb->posts WHERE post_parent = {$id};”));
    echo $num_images;
    break;
    default:
    break;
    }
    }

    add_action( ‘save_post’, ‘painting_ro_save_postdata’ );
    add_action(‘wp_ajax_save_product’, ‘painting_ro_save_postdata_ajax’);

    function painting_ro_inner_custom_box( $post ) {
    wp_nonce_field( plugin_basename( __FILE__ ), ‘painting_ro_noncename’ );
    $value = get_post_meta( $post->ID, $key = ‘_painting_ro_my_meta_value_key’, $single = true );
    echo ‘<label for=”painting_ro_new_field”>’;
    _e(“Add a reorder number”, ‘painting_ro_textdomain’ );
    echo ‘</label> ‘;
    echo ‘<input type=”text” id=”painting_ro_new_field” name=”painting_ro_new_field” value=”‘.($value).'” size=”25″ />’;
    }

    function painting_ro_save_postdata_ajax( $post_id ) {
    global $wpdb;

    $post_ID = $_POST[‘post_ID’];
    $mydata = sanitize_text_field( $_POST[‘painting_ro_new_field’] );

    add_post_meta($post_ID, ‘_painting_ro_my_meta_value_key’, $mydata, true) or
    update_post_meta($post_ID, ‘_painting_ro_my_meta_value_key’, $mydata);

    update_post_meta($post->ID, “_painting_ro_my_meta_value_key”, $_POST[“_painting_ro_my_meta_value_key”]);
    echo ‘Meta Updated’;
    die();
    }

    function painting_ro_save_postdata( $post_id ) {
    global $wpdb;
    $post_ID = $_POST[‘post_ID’];
    $mydata = sanitize_text_field( $_POST[‘painting_ro_new_field’] );

    add_post_meta($post_ID, ‘_painting_ro_my_meta_value_key’, $mydata, true) or
    update_post_meta($post_ID, ‘_painting_ro_my_meta_value_key’, $mydata);

    update_post_meta($post->ID, “_painting_ro_my_meta_value_key”, $_POST[“_painting_ro_my_meta_value_key”]);
    }

    ?>

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Editable Custom fields’ is closed to new replies.