WordPress.org

Ready to get started?Download WordPress

Forums

custom field by default (16 posts)

  1. ioustinos
    Member
    Posted 6 years ago #

    Hi there,
    I want to add two parameters for each post. I read here and there that the best way to do that is through custom fields. However i want a different region for each one. Not all under the custom fields region. Moreover i want the user to just click on checkbox for them and not add the key and value each time. So far i am using a hidden element for the key and a checkbox for the value but when i edit the post i get the key and value textboxes again.
    Anyone know a more robust way to do what i want?

  2. Kafkaesqui

    Posted 6 years ago #

  3. mangroovy
    Member
    Posted 6 years ago #

    Custom Field GUI don't work in wordpress 2.3.3; if anyone has a solution about this, would be great!

  4. Roger Theriault
    Member
    Posted 6 years ago #

    Try this in your plugin or theme:

    add_action('edit_page_form', 'custom_add_edit', 90)
    function custom_add_edit() {
       include ('plugin-edit-form.php'); // chunk of HTML form with checkboxes
    }
    
    add_action('save_post', 'custom_add_save');
    function custom_add_save($postID);
       // called after a post or page is saved
       if ($_POST['my_custom_item']) {
          update_custom_meta($postID, $_POST['my_custom_item']);  // your checkbox field ie name=my_custom_item
       }
    }
    
    function update_custom_meta($postID, $newvalue) {
       // ... insert logic ...
       // TO CREATE NEW META
       add_post_meta($postID, "my_custom_key", $newvalue);
       // -- OR-- TO UPDATE EXISTING META
       update_post_meta($postID, "my_custom_key", $newvalue);
    }
    
    // generic retrieve wrapper
    function get_my_custom_meta($postID = '') {
    	global $post;
    	if ($postID == '') $postID = $post->ID;
    	return get_post_meta($postID, "my_custom_key", true);
    }
    
    -- inside plugin-edit-form.php: (use the WP classes)
    -- this example is for a dropdown list
    <div id="pagepost-my-custom" class="postbox ">
    <h3>CUSTOM TITLE</h3>
    <div class="inside">
    <fieldset id="mycustom-div">
    <legend>Section Name</legend>
    <div>
    <p><label for="my_custom_item" class="selectit">
    <select
    name="my_custom_item" class="something-input"
    id="something-input" tabindex="3"><option value="">Select an Item</option>
    <?php my_custom_dropdown(); ?></select>
    Custom Item</label></p>
    ... etc ... and close the divs

    Also checkout the WP-ratings plugin although he doesn't modify the post edit form he does use the META for storing ratings.

    Note that the custom fields will still be there on the post or page edit form, but this creates a more user-friendly way to enter the info.

    If only certain posts get the custom form just put the logic to filter them into your custom_add_edit function, otherwise this will be available for EVERY post and page.

  5. viitoria
    Member
    Posted 6 years ago #

  6. Ownersbox
    Member
    Posted 6 years ago #

    @RogerTheriault That method works great for something I'm work on. Is there a way to use the Custom Field as a counter? If I use your method to setup a counter variable per post that I set to 0, can the user on the front end click a link that can then allow me to increment that value?

    Is there a function call that I get set that value? I currently only see gets in the docs.

    Thank you

  7. Dgold
    Member
    Posted 6 years ago #

    Is the "More Fields" plugin just the same, and as-good-as Custom Fields GUI plugin?

    I still use Custom Fields GUI on one of my sites. Before I upgrade that site to a newer WP it would be nice to know if this crucial plugin will still work, and if not -- can I simply replace it with More Fields?

    Always like to hear from other users who use these plugins

  8. Roger Theriault
    Member
    Posted 6 years ago #

    @Ownersbox - Just create your own increment_counter function that gets the current value, converts to int, adds, and then calls update_post_meta (see above).

    And I can't take credit for the idea, Lester 'GaMerZ' Chan uses it in his WP-PostRatings plugin as a counter. It's great for any data that is associated with a particular post that you usually have just one of, like number of views, ratings, or (for my real estate plugin) a listing's price, number of bedrooms, etc.

    See http://RogerTheriault.com/listings/ - I use a custom form to present and edit all the data that is displayed there including the map lat and long (and all the pointers to which gallery, panorama, video, etc to show on the detail page).

    My plugin isn't public and isn't even fully tested yet. But here are a couple of utility functions I created:

    function xtend_updatemeta($postID,$metakey,$single_or_list) {
    // permits storing or clearing out of meta from form input,
    // will work with multi-select inputs
    	if (is_array($single_or_list)) {
    		$commalist = implode(',',$single_or_list); // multiple
    	} else {
    		$commalist = $single_or_list; // one or blank
    	}
    	if (xtend_getmeta($metakey,$postID)) {
    		update_post_meta($postID, $metakey, $commalist);
    	} else {
    		if ($commalist) // dont add if blank
    			add_post_meta($postID, $metakey, $commalist);
    	}
    }
    function xtend_updatemetafrompost($postID,$metakey) {
    // call this when you may have form data to stash away
    	$postvalue = $_POST[$metakey];
    	xtend_updatemeta($postID,$metakey,$postvalue);
    }
    function xtend_getmeta($metakey,$postID = '') {
    // call this to grab the data
    	global $post;
    	if ($postID == '') $postID = $post->ID;
    	return get_post_meta($postID, $metakey, true);
    }
  9. Ownersbox
    Member
    Posted 6 years ago #

    @RogerTheriault First of all thank you for your help, I'm not sure what I'm doing wrong, the testing function does display on my post page, but I go to save it doesn't appear to be taking the value.

    //On my index.php in the loop

    <?php get_post_meta($postID, "isBuilding", true);?>

    ////////////////////////////////////////////////////

    //In my functions.php
    function testing(){
    _e("<div id=\"NewOptions\" class=\"postbox\"><h3>");
    _e('Options');
    _e("</h3><div class=\"inside\">Is Building <input name=\"isBuilding\" type=\"checkbox\" value=\"true\" /></div></div>");
    }

    function custom_add_save($postID){
    // called after a post or page is saved
    if ($_POST['isBuilding']) {
    update_custom_meta($postID, $_POST['isBuilding'],"isBuilding"); // your checkbox field ie name=my_custom_item
    }
    }
    function update_custom_meta($postID, $newvalue, $keyname) {
    // ... insert logic ...
    // TO CREATE NEW META
    add_post_meta($postID, $keyname, $newvalue,true);
    // -- OR-- TO UPDATE EXISTING META
    update_post_meta($postID, $keyname, $newvalue);
    }

    add_action('edit_form_advanced', 'testing');
    add_action('save_post', 'custom_add_save');

  10. Roger Theriault
    Member
    Posted 6 years ago #

    Try my code above, in your custom_add_save call xtend_updatemetafrompost($postID,'isBuilding'). You might want to start echoing here and there to debug, make sure your save_post action is getting run. Otherwise it generally looks correct. But you only need one or the other, add or update.

    Did you look in the DB, or in the Custom Fields below your post form to see if there is a meta there after you check the box? If there is, the problem is on retrieval. - and make sure you are really passing a valid postID.

    Also try tweaking your function names just in case there's a conflict.

  11. Ownersbox
    Member
    Posted 6 years ago #

    Ok we are very close. It is updating the post_meta table but it is not setting the post id correctly, keeps getting 0.

    In the custom add function I have my to update functions with one being commented out, both do the same thing.

    function xtend_updatemeta($postID,$metakey,$single_or_list) {
    // permits storing or clearing out of meta from form input,
    // will work with multi-select inputs
    if (is_array($single_or_list)) {
    $commalist = implode(',',$single_or_list); // multiple
    } else {
    $commalist = $single_or_list; // one or blank
    }
    if (xtend_getmeta($metakey,$postID)) {
    update_post_meta($postID, $metakey, $commalist);
    } else {
    if ($commalist) // dont add if blank
    add_post_meta($postID, $metakey, $commalist);
    }
    }
    function xtend_updatemetafrompost($postID,$metakey) {
    // call this when you may have form data to stash away
    $postvalue = $_POST[$metakey];
    xtend_updatemeta($postID,$metakey,$postvalue);
    }
    function xtend_getmeta($metakey,$postID = '') {
    // call this to grab the data
    global $post;
    if ($postID == '') $postID = $post->ID;
    return get_post_meta($postID, $metakey, true);
    }
    function post_testing(){
    global $post;
    $postID = $post->ID;
    _e("<div id=\"NewOptions\" class=\"postbox\"><h3>");
    _e('Options');
    _e("</h3><div class=\"inside\">Is Building <input name=\"isBuilding\" type=\"checkbox\" value=\"true\"");
    if(xtend_getmeta('isBuilding'))
    echo(" checked ");
    echo(" /></div></div>");
    }

    function custom_add_save(){
    global $post;
    $postID = $post->ID;
    xtend_updatemetafrompost($postID,'isBuilding');
    //update_custom_meta($postID, $_POST['isBuilding'],'isBuilding');
    }
    function update_custom_meta($postID, $newvalue, $keyname) {
    global $post;
    $postID = $post->ID;
    add_post_meta($postID, $keyname, $newvalue,true);
    }

  12. Ownersbox
    Member
    Posted 6 years ago #

    I got it now. A slight change to the following function, Thank you.

    function custom_add_save($postID){
    xtend_updatemetafrompost($postID,'isBuilding');
    }

  13. j.michael
    Member
    Posted 6 years ago #

    On a related note, I'm working on adding the ability for a logged-in user to comment anonymously (for HIPAA or other sensitive reasons). I've added a 'comment_anonymous' field to the wp-comments table and a checkbox to the comment form.

    What steps do I need to take to complete the process and suppress the username on anonymous comments?

    Or ... anyone interested in picking up a few dollars to do the work?

  14. nfong
    Member
    Posted 6 years ago #

    I've been playing with this code under WordPress 2.6 and note the following.

    Create a New post.

    Add a custom field (pre-existing) to the post. Draft gets saved. Postmeta entry created with post_id 68

    Type some more in fields. Draft gets saved. Postmeta entry created with post_id 69

    Type some more in fields. Draft gets saved. Postmeta entry created with post_id 70

    Save post.

    Post is now id 70, but of course the postmeta db is littered with values.

    You also see a bunch of extra _edit_lock, _edit_last entries littering postmeta db.

    Suggestions on a fix appreciated.

  15. boonika
    Member
    Posted 6 years ago #

    Maybe it's because of the revisions feature. Try using this plugin:

    http://wp-cms.com/our-wordpress-plugins/post-control-plugin/

  16. Hiranthi
    Member
    Posted 6 years ago #

    You could also try the plugin more-fields. That plugin has been a lifesaver for me for a few times now ;)

Topic Closed

This topic has been closed to new replies.

About this Topic