Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can add metabox to custom post type with following code.</p> <p>First, create metabox</p> <pre><code>add_action('admin_init', 'my_theme_on_admin_init'); function my_theme_on_admin_init() { add_meta_box('my_metabox', __('My metabox', 'textdomain'), 'my_metabox_render', 'my_custom_post_type', 'normal', 'high' ); } </code></pre> <p>Notice, that <code>my_custom_post_type</code> is a name of you custom post type and <code>my_metabox_render</code> - name of a function that renders metabox.</p> <p>Rendering function should create all nessesary fields </p> <pre><code>function my_metabox_render($post) { $data = get_post_meta($post-&gt;ID, '_meta_key', true); // Use nonce for verification wp_nonce_field('add_my_meta', 'my_meta_nonce'); ?&gt; &lt;div class="inside"&gt; &lt;table class="form-table"&gt; &lt;tr valign="top"&gt; &lt;th scope="row"&gt;&lt;label for="my_meta_value"&gt;&lt;?php _e('My meta', 'textdomain'); ?&gt;&lt;/label&gt;&lt;/th&gt; &lt;td&gt;&lt;textarea id="my_meta_value" name="my_meta_value" cols="40" rows="5"&gt;&lt;?php echo (isset($data)) ? $data : ''; ?&gt;&lt;/textarea&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt; &lt;?php } </code></pre> <p>Than you should update you metadata when user saves post</p> <pre><code>add_action('wp_insert_post', 'save_my_meta', 10, 2); function save_my_meta($id) { if ( defined('DOING_AUTOSAVE') &amp;&amp; DOING_AUTOSAVE ) return $id; if (!current_user_can('edit_posts')) return; if (!isset($id)) $id = (int) $_REQUEST['post_ID']; if (isset($_POST['my_meta_value']) &amp;&amp; wp_verify_nonce($_POST['my_meta_value'], 'add_my_meta')) { $data = $_POST['my_meta_value']; if (isset($data)) { update_post_meta($id, '_meta_key', $data); } else { delete_post_meta($id, '_meta_key'); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload