black friday sale

Custom Fields
Re-Envisioned
Support General Dynamic created metaboxes for specific pages are not saving (using only code)

  • Creator
    Topic
  • #22675
    Resolved Teia Local Studio
    Participant

    Hej guys!
    I have been trying to fix this for the whole weekend, but seems impossible to me. The scenario:

    SETTINGS PAGES extension:

    I did create a settings panel where I can assign custom metaboxes for each page of my site, based on the page POST_ID.

    I have a bunch of PHP files with the metaboxes code inside, created by hand using the classic $meta_boxes[] = …; method. Everything works perfect. I can set different metaboxes for different pages and they load correctly on the editor of each page they were assigned for. But the problem is… They do not save!

    How do I call them:

    When I am editing a page, I simply do a PHP INCLUDE with the desired metabox contents I want to load, based on the $_GET[‘post’] ID that is being edited. The metabox loads fine inside the editor, but it does not save when I hit PUBLISH/UPDATE.

    However… If I manually call the very same PHP file hardcoding it’s path and name, the metabox will save normally.

    It seems to me…

    …the problem resides when I dynamically call the metabox contents via PHP INCLUDE. The MB title, MB ID and all other MB structure items are created based on the current $_GET[‘post’] being edited. Something get lost in the middle of the PUBLISH/UPDATE operation.

    For example, let’s use a page with ID 22

    On my metaboxes “boot page” I have the following “metabox shell”, that is dynamically fed accordingly to a setting defined on my Settings Page panel:

    $meta_boxes[] = array(
        'include' => array('ID' => $_GET['post']),
        'id' => 'meta-'.get_post_type($_GET['post']).'-'.$_GET['post'],
        'title' => 'My title',
        'pages' => array(get_post_type($_GET['post'])),
        'context' => 'side',
        'style' => 'seamless',
        'fields' => $my_custom_fields
    );

    Then I have several .PHP files containing different setups of fields (to feed above):

    my-custom-fields-1.php
    my-custom-fields-2.php
    my-custom-fields-3.php

    my-custom-fields-22.php

    Then, if I call the desired “PHP fields” file manually, the metabox loads and saves perfecty:

    <? include(TEMPLATE_PATH.'/metaboxes/my-custom-fields-22.php'); ?>

    However, if I call using a certain condition, based on the current $_GET[‘post’] value set in the editor, the metabox loads perfectly on the editor ——— but does not save!

    <? 
    // Lets say we are editing page with ID 22
    if (get_post_type($_GET['post']) == 'page') {
    include(TEMPLATE_PATH.'/metaboxes/my-custom-fields-'.$_GET['post'].'.php'); ?>
    } ?>

    Thank you!!!
    G.

Viewing 1 replies (of 1 total)
  • Author
    Replies
  • #22680
    Teia Local Studio
    Participant

    WOW.
    I think I found out (after reading some of Ahn’s past posts)!

    Instead of calling (via PHP include) my metaboxes fields using just $_GET[‘post’], I need to do the following:

    $post_id = null;
    if ( isset( $_GET['post'] ) ) {
        $post_id = intval( $_GET['post'] );
    } elseif ( isset( $_POST['post_ID'] ) ) {
        $post_id = intval( $_POST['post_ID'] );
    }
    $post_type = get_post_type($post_id);

    So, my
    <? include(TEMPLATE_PATH.'/metaboxes/'.$post_type.'/my-custom-fields-'.$post_id.'.php'); ?>
    will work!

    Previously I was using (wrongly!):
    <? include(TEMPLATE_PATH.'/metaboxes/'.get_post_type($_GET['post']).'/my-custom-fields-'.$_GET['post'].'.php'); ?>

    Cheers!

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.