• Hi guys,

    I need to create a custom meta box in the main column, after the editor and after the wordpress custom field boxes.

    So I added this code:

    function custom_meta_box_markup($object)
    {
        wp_nonce_field(basename(__FILE__), "meta-box-nonce");
        ?>
    <div>
        Please remember customer's code!    
    </div>
        <?php
    }
    
    function add_custom_meta_box()
    {
        add_meta_box("demo-meta-box", "Read me", "custom_meta_box_markup", "post", "advanced", "high", null);
    }
    add_action("add_meta_boxes", "add_custom_meta_box");

    But it did not work as I expected: my custom meta box ‘Read me’ is in the right column, together with Publish and Categories meta boxes. I use the right context ‘advanced’, but wordpress seems to understand the ‘side’ one.

    What’s wrong?
    THXX!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Your code works correctly on my installation. Is there any chance you previously moved a previous meta box with the same ID to the side column? Check your user meta keyed “meta-box-order_post”. If the ID is in this array, the box has been moved from its default position.

    If not, your theme or one of your plugins must be manipulating how meta boxes work for some reason. If you want to identify the culprit, deactivate all plugins and switch to a default twenty* theme. Add your code temporarily to the theme’s functions.php file. You’ll find the meta box appears correctly. Restore your normal theme and again temporarily place your code in the functions.php file again. See if your box still appears correctly. Activate plugins one by one, testing after each. Once the box moves to the other column you’ve identified the culprit.

    Thread Starter sandra11

    (@sandra11)

    @bcworkz thanks and sorry for delay

    You solved my problem!

    Your code works correctly on my installation. Is there any chance you previously moved a previous meta box with the same ID to the side column? Check your user meta keyed “meta-box-order_post”. If the ID is in this array, the box has been moved from its default position.

    Yes, its ID was (is!) in this array.
    Finally, I’ve changed ID (from demo-meta-box to demo-meta-box2) and all works perfectly!
    Just a question: why this behavior?
    If I MUST use the first ID, can I clear the wordpress cache? Or what?
    Thank you very much!! 🙂

    • This reply was modified 9 years, 6 months ago by sandra11.
    Moderator bcworkz

    (@bcworkz)

    Delays are no problem, I’m here most days. There was also a holiday yesterday in the USA. And it’s you issue, not mine 🙂

    Anytime a user moves a meta box from it’s default positioning, the new positioning is stored by ID in that user’s meta data. It pretty much persists there permanently, even when the meta box code is removed. A cache flush will not help, the data is written into the DB. There’s also no automatic cleanup mechanism. It’s up to the box’s developer to attempt such a clean up upon removal of their code. Even if such an attempt is made, it would depend on the user using the provided delete action. If they simply delete the code by FTP, there’s nothing a developer can do to cleanup after themselves.

    If you come along and use the same ID, your box will “inherit” this old positioning even if this is the first time your box ever appeared to the user. Ideally, everyone would use a unique ID to avoid such issues. Attempting to reset any such old data upon activation could cause problems when a user temporarily deactivates/reactivates the theme/plugin for some reason. While it’s always a good idea to clean up after yourself upon removal (not the same as deactivation, each action fires a different action hook), it doesn’t always work and may not even be desirable. The user should typically be given the option to not do a cleanup upon removal.

    Thread Starter sandra11

    (@sandra11)

    Ok understood…

    In the same way, on create new custom meta box, I think it would be useful not to use a “generic” ID too, because this could create problems in other plugins.

    Thanks for clear explanation!!! 🙂

    [Resolved]

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Custom meta box in post admin page. Placement error (codex)’ is closed to new replies.