Selecting Post Formats

Support MB Frontend Submission Selecting Post FormatsResolved

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #24697
    Rebecca RobertsonRebecca Robertson
    Participant

    When adding a new post on the frontend of the site, I need to be able to select the post format the same way I can on the backend. How would I go about creating a form element for doing that? ACF has the option to choose Format in the taxonomy field, but I can't find anything similar in Meta Box other than with conditional logic, which doesn't help if I can't select a post format in the first place.

    #24700
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Meta Box has not supported a field to select the post format yet. But we can create a custom field radio with options like WordPress post formats. Sample code below:

    $meta_boxes[] = [
      'title'  => __( 'Post Meta', 'your-text-domain' ),
      'id'     => 'post-meta',
      'fields' => [
        [
          'name'    => __( 'Post Format', 'your-text-domain' ),
          'id'      => $prefix . 'post_format',
          'type'    => 'radio',
          'options' => [
            'gallery' => __( 'Gallery', 'your-text-domain' ),
            'aside'   => __( 'Aside', 'your-text-domain' ),
            'status'  => __( 'Status', 'your-text-domain' ),
            'link'    => __( 'Link', 'your-text-domain' ),
            'image'   => __( 'Image', 'your-text-domain' ),
            'quote'   => __( 'Quote', 'your-text-domain' ),
            'video'   => __( 'Video', 'your-text-domain' ),
            'audio'   => __( 'Audio', 'your-text-domain' ),
            'chat'    => __( 'Chat', 'your-text-domain' ),
          ],
          'inline'  => false,
        ],
      ],
    ];

    Use the action hook rwmb_{$field_id}_after_save_field to set post format after saving the custom field.

    add_action( 'rwmb_post_format_after_save_field', function( $null = true, $field, $new, $old, $object_id ) {
        set_post_format( $object_id, $new );
    }, 10, 5 );

    Ok, then we can use the function get_post_format() to get the post format as default feature post format.

    #24891
    Rebecca RobertsonRebecca Robertson
    Participant

    This is the part I'm confused about. Where do I put action hook and function?

    #24905
    Long NguyenLong Nguyen
    Moderator

    Hi Rebecca,

    You can add the code to the file functions.php in the theme/child theme folder or use the plugin Code Snippets https://wordpress.org/plugins/code-snippets/.

    #24907
    Rebecca RobertsonRebecca Robertson
    Participant

    Thank you.

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.