twentysixteen
twentysixteen copied to clipboard
twentysixteen_content_width action breaks oEmbed for Custom Post Types
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:
STEPS TO REPRODUCE
- Create a basic, non-heirarchical CPT that support title, thumbnail, excerpt, revisions, editor
- Create a post with that CPT
- Try to embed that post on a PAGE somewhere else.
- 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' );
Sorry, I was not able to replicate the issue. I tested it with WP 4.4 and 4.5 and used your code.
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.
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?
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.
I am getting 'failed to embed', both with and without the conditional logic before the GLOBAL.
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.
Testing this locally I can reproduce the issue. The provided solution also fixes the issue.
We need a PR to test this ideally in a variety of circumstances.