'Wordpress Post Format Dropdown Empty

I'm working on a wordpress theme. The theme enables a subset of the default supported post-formats based on user-configured checkboxes as follows:

function mytheme_enable_theme_support(){
  $options = get_option('post_formats');
  $formats = array('aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio', 'chat');
  $output = array();
  foreach($formats as $format){
    if (isset($options[$format])){
      $output[] = $format;
    }
  }
  if(!empty($options)){
    add_theme_support('post-formats', $output);
  }
}
add_action('after_setup_theme', 'mytheme_enable_theme_support');

The GUI settings page for the option retrieved is working just fine and I've verified at the DB level that the post formats I want to see are there.

enter image description here

There are no errors that I can see in the logs. When adding a new post, you're supposed to be able to choose a post format, but the drop-down has no options in it.

enter image description here

Anyone have any idea what gives?

I'm running Wordpress 5.8.1 on NGINX with PHP 8.0.

Update:

I've modified the code above with a debugging routine I wrote debug(), that prints the contents of variables to a file, as follows:

  if(!empty($options)){
    debug(print_r($output,true)."\n");
    debug(print_r($options,true)."\n");
    add_theme_support('post-formats', $output);
  }

The output of the two debug() calls above is as follows:

Array ($output)
(
    [0] => gallery
    [1] => video
    [2] => audio
)

Array ($options)
(
    [gallery] => 1
    [video] => 1
    [audio] => 1
)

Seems to me, regardless of what's happening above this in the code, the call to add_theme_support('post-formats', $output); is happening correctly. So I think my question stands... why am I not seeing these options listed in the post format dropdown? and where else can I look to see what might be going on?

Update 2:

Here's a meticulously commented version of my running code for those that are interested.

//enable the post formats chosen in the theme settings
function mytheme_enable_theme_support(){
  $options = get_option('post_formats'); 
  /* if the option 'post_format' is found in the DB, creates an arrray of what it contains
     in my case, that array looks like this:

     Array (
         [gallery] => 1
         [video] => 1
         [audio] => 1
     )     

     if the option is not found, $options will contain NULL.
  */
  
  $formats = array('aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio', 'chat');
  /* here we initialize a temporary variable with all the supported post formats. This is so that we 
     can use it below to build an array of just the ones configured. There are likely better ways to 
     do this but this works.

  */
  
  $output = array(); //initialize $output as an empty array
  
  foreach($formats as $format){
    /* loop through the $formats array defined above. This loop will execute 9 times.
       each time it executes $format will contain the next string in the sequence 
       ('aside', 'gallery', 'link', etc.)
    */
    if (isset($options[$format])){
      /* this if statement checks to see if the current $format string exists as a key in the $options
      array. 
        if (isset($options['aside']))
        if (isset($options['gallery']))
        if (isset($options['link']))
        if (isset($options['image']))
        etc.
      */  
      
      $output[] = $format;
      /* if it does, this assignment pushes the current format string onto the $output array. 
      $output[] = $format; is equivalent to array_push($output, $format);
      See https://www.php.net/manual/en/function.array-push.php for details.
      */
    }
    // $output[] = ( @$options[$format] == 1 ? $format : ''); // '@' is shorthand for 'isset()'
  }
  if(!empty($options)){
    // when we're all done, if we have any elements in the $options array... 

    debug(print_r($output,true)."\n");
    debug(print_r($options,true)."\n");
    // configure the theme to support them
    add_theme_support('post-formats', $output);
  }


Solution 1:[1]

I found the problem. It had nothing to do with my code. All of that discussion above was a complete red herring.

The absence of the index.php template file in the root directory of the theme somehow breaks the post formats drop-down menu in the post editor. I'm not versed enough in the internals of WordPress to speculate on what might be going on. Some of you may understand this and if you do, please add a comment or another answer for the record.

Per the template hierarchy documented here, index.php, the default template used to render all pages, is not required provided the following templates are present:

  • archive.php
  • single.php
  • page.php
  • home.php
  • 404.php
  • search.php

But this is clearly not the case. If index.php is missing regardless of whatever other templates may be present, there is no functional post-formats drop-down menu.

For the record, my new index.php page is a copy of my home.php and contains the following code:

<?php get_header();
?>
  <div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">
        <?php
          if(have_posts()):

            while(have_posts()): the_post();

                get_template_part('template-parts/content', get_post_format());

            endwhile;

          endif;
         ?>
    </main>
  </div> <!-- #primary -->

<?php get_footer(); ?>

As you can see, there's nothing strange going on here in the code at least. I'm running WordPress 5.8.1 with PHP 7.4. on DevKinsta version 2.4.1 (2.4.1.3185).

Solution 2:[2]

The problem is in your code.

  $options = get_option('post_formats');
  $formats = array('aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio', 'chat');
  $output = array();
  foreach($formats as $format){
    if (isset($options[$format])){
      $output[] = $format;
    }
is shorthand for 'isset()'
  }
  if(!empty($options)){
    add_theme_support('post-formats', $output);
  }

Add this after your $options =

var_dump($options);
die();

Testing if it returns empty it never does anything.

foreach($formats as $format){
        if (isset($options[$format])){
            $output[] = $format;
        }
    }

This just loops through your $formats but never builds an array so $output is just empty.

You could just add your own list:

add_theme_support(
        'post-formats',
        array(
            'link',
            'aside',
            'gallery',
            'image',
            'quote',
            'status',
            'video',
            'audio',
            'chat',
        )
    );

Since you never add to or use the $options its hard to tell if you are trying to use the existing + new or not.

As well, if this is a custom theme those options are already added somewhere and would be better to update that section to include the new options.

enter image description here

enter image description here

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 halfer
Solution 2