Function — Add custom fields for product data grid - martindubenet/Wordpress GitHub Wiki

A Custom Field is composed of a key and a value metadatas stored in the database and associated with a post type. You only need to create a new key once, after which you can assign a value to that key for every post (of the same type), if you so desire. You can also assign more than one value to a key.

Tutorials

  1. WordPress Custom Fields 101: Tips, Tricks, and Hacks
  2. Adding WooCommerce custom fields programmatically

 

Adding Custom Fields (CF) data

Input CF in a post :

  1. Edit the post or page,
  2. Click on the vertical ellipsis « » button (located at the top of the right sidebar of the screen),
  3. From the contextual menu, click « Options »,
  4. From the « Options » modal window,
    • Check the ✅ « Custom fields » option located the Advanced panels section,
    • Click « Enable & Reload » button.
  5. This will enable the Custom Fields widget located under the main (Gutenberg) text editor area.

Custom Fields widget

Output CF in the theme :

Use the_meta() function to display a list of post custom fields.

Note : This function will ignore meta_keys that begin with an underscore (_*).

Display CF as an HTML Description List section.post-meta-dl

function the_meta_dl() {
    $keys = get_post_custom_keys();
    if ( $keys ) {
        $dl_html = '';
        foreach ( (array) $keys as $key ) {
            $keyt = trim( $key );
            if ( is_protected_meta( $keyt, 'post' ) ) {
                continue;
            }
 
            $values = array_map( 'trim', get_post_custom_values( $key ) );
            $value  = implode( ', ', $values );
 
            $html = sprintf(
                '<dl class="meta-set"><dt class="post-meta-key">%s</dt><dd>%s</dd></dl>\n',
                /* translators: %s: Post custom field name. */
                sprintf( _x( '%s:', 'Post custom field name' ), $key ),
                $value
            );
 
            /**
             * Filters the HTML output of the definition list element within the post custom fields section.
             *
             * @since 2.2.0
             *
             * @param string $html  The HTML output for the li element.
             * @param string $key   Meta key.
             * @param string $value Meta value.
             */
            $dl_html .= apply_filters( 'the_meta_key', $html, $key, $value );
        }
 
        if ( $dl_html ) {
            echo '<section class="post-meta-list">\n{$li_html}</section>\n';
        }
    }
}

 

Plugin Custom Field Suite

mgibbs189.github.io/custom-field-suite/

Available for FREE via on GitHub or Wordpress.org

Code exemple for your theme page: <?php echo CFS()->get( 'your_field_name' ); ?>

Edit Field Group

 

Plugin Advanced Custom Fields

ACF logo

FREE or PRO version are available.

It is highly recommanded to use « Advanced Custom Fields » plugin to manage custom fields in your site. See a bunch of code examples.

⚠️ **GitHub.com Fallback** ⚠️