Skip to content
This repository has been archived by the owner on May 23, 2018. It is now read-only.

twentysixteen_content_width action breaks oEmbed for Custom Post Types #464

Open
mathetos opened this issue Apr 30, 2016 · 7 comments
Open

Comments

@mathetos
Copy link
Contributor

WordPress 4.5 supports CPT templates for oEmbed now, for example embed-promotion.php. But the embed defaults reference $GLOBALS['content_width'] -- here -- in the same way that Twenty Sixteen does -- here.

This somehow negatively impacts only native CPT embeds. Here's a screenshot:
twenty-sixteen-oembed-bug

STEPS TO REPRODUCE

  1. Create a basic, non-heirarchical CPT that support title, thumbnail, excerpt, revisions, editor
  2. Create a post with that CPT
  3. Try to embed that post on a PAGE somewhere else.
  4. The result will be that the Embed simply won't render

FIX

Adding if ( ! empty( $GLOBALS['content_width'] ) ) directly before that line in the twentysixteen_content_width() function prevents such problems.

CAVEAT

This didn't seem to affect all CPTs and it's not clear to me why exactly, but here's the code I used for the CPT that broke because of this function:

function matt2016_register_promotion_post_type() {
    $labels = array(
        'name' => 'Promotions',
        'singular_name' => 'Promotion',
        'add_new' => 'Add New',
        'add_new_item' => 'Add New Promotion',
        'edit_item' => 'Edit Promotion',
        'new_item' => 'New Promotion',
        'view_item' => 'View Promotion',
        'search_items' => 'Search Promotions',
        'not_found' =>  'No Promotions found',
        'not_found_in_trash' => 'No Promotions found in trash',
        'parent_item_colon' => '',
        'menu_name' => 'Promotions'
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true, 
        'show_in_menu' => true, 
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'has_archive' => true, 
        'hierarchical' => false,
        'menu_position' => 6.1,
        'menu_icon' => 'dashicons-star-empty',
        'supports' => array('title','thumbnail','excerpt','revisions', 'editor')
    ); 

    register_post_type( 'promotion', $args );
}

add_action( 'init', 'matt2016_register_promotion_post_type' );
@grappler
Copy link
Member

Sorry, I was not able to replicate the issue. I tested it with WP 4.4 and 4.5 and used your code.

@mathetos
Copy link
Contributor Author

mathetos commented May 2, 2016

I just tested again on a fresh new local install. The CPT didn't render as an oEmbed at all until I added the conditional before the GLOBAL.

@mathetos
Copy link
Contributor Author

mathetos commented May 2, 2016

It also seems only logical that since it's being defined by a GLOBAL that it should first check whether that Global already exists, right?

@EvanHerman
Copy link

I can't get the custom post type to embed at all. Used the code above on a fresh install (4.5.1) running the latest release of TwentySixteen.

screenshot

I am getting 'failed to embed', both with and without the conditional logic before the GLOBAL.

@mathetos
Copy link
Contributor Author

mathetos commented May 3, 2016

Thanks Evan for the confirmation. Make sure to clear the _oembed cache in wp_postmeta to make sure you're getting the freshest results when you test.

Since this seems to affect a Core theme, and the results are varying so much (with and without the conditional -- for some and not for others), it might be worth creating a Trac ticket on this.

@andreasnrb
Copy link

andreasnrb commented May 4, 2016

Testing this locally I can reproduce the issue. The provided solution also fixes the issue.

@karmatosed
Copy link
Member

karmatosed commented Jul 9, 2016

We need a PR to test this ideally in a variety of circumstances.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants