2 comments

  1. The HTML attribute name is what is sent to the server. There is some PHP code reading it and storing it with a specific key in the database.

    The HTML name and the database key don’t have to be the same string. The PHP code can use a completely different name or split one value into multiple database values.

    The template attribute handler does that: It takes the name page_template from page attributes metabox and stores it as _wp_page_template in the database. The leading underscore protects that field from showing up in the custom fields metabox.

    See this part in wp_insert_post():

    if ( !empty($page_template) && 'page' == $data['post_type'] ) {
        $post->page_template = $page_template;
        $page_templates = wp_get_theme()->get_page_templates();
        if ( 'default' != $page_template && ! isset( $page_templates[ $page_template ] ) ) {
            if ( $wp_error )
                return new WP_Error('invalid_page_template', __('The page template is invalid.'));
            else
                return 0;
        }
        update_post_meta($post_ID, '_wp_page_template',  $page_template);
    }
    
  2. Saving data from a form is not automatic as you seem to think…

    When adding custom post meta boxes, I understand that whatever name
    attribute input you put would be passed through the form into the
    server. Hence, if <input type="text" name="my_custom_field"
    id="my_custom_field" />
    , using get_post_meta($post->ID,
    'my_custom_field', true);
    to retrieve the value of that input is
    possible.

    Yes, the form data will be passed to the server as either POST or GET data but nothing happens to that data if it is not intentionally processed by the script. That often means saving to the database but not necessarily. You can email the data instead, as part of a contact form for example.

    The name attribute the form uses does not have to match the key saved to the database. The code that processes the form can save it it under any key the programmer wants.

    The particular piece of data you are talking about is processed by the wp_insert_post function in wp-includes/post.php.

Comments are closed.