• Resolved mosheeshel

    (@mosheeshel)


    Funny Situation
    Meta Box and Fields are rendered fine, but no data is saved
    If I create a the field array “hardcoded” (with same id’s etc…) everything works just great…

    Code:

    $forties_meta  = new Super_Custom_Post_Meta( 'page' );
            $forties_meta->add_meta_box( array(
                'id' => 'page-forties',
                'title' => '1940s',
                'fields' => auto_add_historybox('1940')
            ) );
    
    function auto_add_historybox($year) {
        /* $boxes_array = array(
                    'strauss_1950_image_1' => array( 'type' => 'file', 'label' => '1 - Image' ),
                    'strauss_1950_text_1' => array( 'label' => '1 - Paragraph', 'type' => 'textarea')
                ); */
        $boxes_array = array();
    
        //Add background image option
        $new_bg = 'strauss_'.$year.'_bg';
        $boxes_array[$new_bg] = array( 'type' => 'file', 'label' => 'Background image for '.$year.'s' );
        $counter = 1;
    
        if (isset($_GET['post']))
        {
            $postid = $_GET['post'];
            if (get_post_meta($postid, 'strauss_'.$year, true) != '1')
                return;
    
            $continue = true;
            $stopInLoop = false;
    
            while($continue) {
                $new_img_fieldname = 'strauss_'.$year.'_image_'.$counter;
                $new_year_fieldname = 'strauss_'.$year.'_year_'.$counter;
                $new_title_fieldname = 'strauss_'.$year.'_title_'.$counter;
                $new_text_fieldname = 'strauss_'.$year.'_text_'.$counter;
    
                $boxes_array[$new_img_fieldname] = array( 'type' => 'file', 'label' => $counter . ' - Image' );
                $boxes_array[$new_year_fieldname] = array( 'label' => $counter . ' - Year', 'type' => 'text');
                $boxes_array[$new_title_fieldname] = array( 'label' => $counter . ' - Title', 'type' => 'text');
                $boxes_array[$new_text_fieldname] = array( 'type' => 'textarea', 'label' => $counter . ' - Paragraph' );
    
                if ($stopInLoop)
                    break;
    
                if (
                    metadata_exists( 'post', $postid, $new_img_fieldname )  ||
                    metadata_exists( 'post', $postid, $new_year_fieldname )  ||
                    metadata_exists( 'post', $postid, $new_title_fieldname )  ||
                    metadata_exists( 'post', $postid, $new_text_fieldname )
                )
                {
                    // Check If anything exists
                    if (!(
                        (strlen(get_post_meta($postid, $new_img_fieldname, true)) > 0) ||
                        (strlen(get_post_meta($postid, $new_year_fieldname, true)) > 0) ||
                        (strlen(get_post_meta($postid, $new_title_fieldname, true)) > 0) ||
                        (strlen(get_post_meta($postid, $new_text_fieldname, true)) > 0)                    ))
                    {
                        $stopInLoop = true;
                    }
                }
                else
                {
                    $continue = false;
                }
                $counter++;
            }
    
            return $boxes_array;
        }
        else
        {
            if ( empty($_POST) ) {
                $new_img_fieldname = 'strauss_'.$year.'_image_'.$counter;
                $new_year_fieldname = 'strauss_'.$year.'_year_'.$counter;
                $new_title_fieldname = 'strauss_'.$year.'_title_'.$counter;
                $new_text_fieldname = 'strauss_'.$year.'_text_'.$counter;
                $boxes_array[$new_img_fieldname] = array( 'type' => 'file', 'label' => $counter . ' - Image' );
                $boxes_array[$new_year_fieldname] = array( 'label' => $counter . ' - Year', 'type' => 'text');
                $boxes_array[$new_title_fieldname] = array( 'label' => $counter . ' - Title', 'type' => 'text');
                $boxes_array[$new_text_fieldname] = array( 'type' => 'textarea', 'label' => $counter . ' - Paragraph' );
                return $boxes_array;
            }
        }
    
    }

    http://wordpress.org/extend/plugins/super-cpt/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Matthew Boynes

    (@mboynes)

    Interesting use of SuperCPT! I love seeing what people come up with.

    My hunch is, your code conditional breaks down to this:

    if (isset($_GET['post'])) {
    } else {
        if ( empty($_POST) ) {
        }
    }

    When a post is saved, $_GET['post'] is not set and $_POST is not empty, so your function has nowhere to go. What you probably want to do is first set $postid to whichever is set, $_GET['post'] or $_POST['post_ID'] (or false if neither), then set your first conditional to be if ( $postid ) { ....

    Thread Starter mosheeshel

    (@mosheeshel)

    Happy to make you happy 🙂
    It’s not the best use I think, a repeater field(s) would have been much preferable, but I’ve been brought on as a consultant and everybody expects me to resolve the issues I didn’t create 🙂

    If what “bothers” the function is POST vs GET, how do I avoid calling it at all during the save action? or do I have to define the meta fields each page load?

    BTW, are there any plans to continue development of the plugin? where do you see it going in the future?

    Plugin Author Matthew Boynes

    (@mboynes)

    The fields need to be defined in order for SuperCPT to actually save them. SuperCPT won’t save fields it doesn’t know about (it’s a security thing). Here’s the relevant bit of code; you can see it loops over the fields and for each one, adds or updates the meta.

    Yes, this plugin is definitely going to continue development. I’m giving a talk on it at WordCamp Chicago in a couple weeks, and I’ll be releasing some enhancements before the talk. My goal is to keep it simple and flexible. SuperCPT won’t solve every problem and won’t save you from having to write code for really complex data structures. Much like WordPress itself, it’s meant to be a solid foundation and be flexible enough to let you do whatever you need to do.

    You should also check out http://fieldmanager.org/. It’s in beta right now (but used in production on some really large, big-name sites), and we’re anticipating a release in the wordpress.org repository soon. Fieldmanager is very similar to SuperCPT, but with a much richer feature set. While the focus of SuperCPT is custom post types and taxonomies (and providing basic meta box/field support as a bonus), the focus of Fieldmanager is meta boxes and fields.

    Thread Starter mosheeshel

    (@mosheeshel)

    I see, that is what is causing the issue in effect, I just couldn’t put my finger on the exact place this happens (I tried modifying save_meta to remove the security, but that didn’t do the trick for some reason…

    As I wrote, I believe the approach they took in the code is wrong – the whole thing should have been resolved with a CPT instead of multiple (unending) meta values (multiple posts grouped by taxonomy) – but you can’t win them all 🙂 Their final solution was to pre-create 20 such combo fields (per decade = 20 combos X 4 meta fields X 10 decades = 800 meta fields per page – lets see how the page performance is going to be 🙁 – then they’ll call me again… )

    Anyway, really happy to hear about the future plans, how does the fieldmanager project integrate with super-cpt (they have tons of overlap as I see it)? how do I make them work together in an effective way?

    Wouldn’t it be better to merge these two projects and create a comprehensive wordpress data extension plugin?

    One thing I’d like to see in a Meta Box plugin is the ability to have nested boxes, that would be really cool!

    Thanks again for all your help

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Meta Field not saving/updating’ is closed to new replies.