0

I've created a custom post type and added some custom meta boxes using this great resource: https://github.com/WebDevStudios/Custom-Metaboxes-and-Fields-for-WordPress

The problem, I've found, is that the post meta fields have a different id depending on whether the post is from the first category of type or not. I had had a problem pulling the post meta on a page template because of it, here: New WP_query in template not working with CPT+category on some pages, resulting in having to reference a different meta box id for different page templates, depending on the category I was pulling.

Not a huge deal, but it now brings me to my current situation: Pulling thumbnails from oembedded videos and setting them as the featured image automatically, using this fantastic plugin, Featured Image of Video from oembed, works on that first category, but none of the others. It has to be related somehow, right?

Here's my custom meta box code:

//Initialize the meta boxes
add_action( 'init', 'wpt_initialize_cmb_meta_boxes', 9999 );   
function wpt_initialize_cmb_meta_boxes() {

if ( ! class_exists( 'cmb_Meta_Box' ) )
    require_once dirname( __FILE__ ) . '/includes/metaboxes/init.php';
}

//add custom fields
add_filter( 'cmb_meta_boxes' , 'wpt_create_metaboxes' );
function wpt_create_metaboxes( $meta_boxes ) {

// Start with an underscore to hide fields from custom fields list
$prefix = '_cmb_';

//Films details meta boxes
$meta_boxes[] = array(
'id' => 'film_contents',
'title' => 'Films',
'pages' => array('films'),//Add our post_type() we created earlier.
'context' => 'normal',
'priority' => 'low',
'show_names' => true,
'fields' => array(
    array(    //Add video embed field
    'name' => 'Film URL',
    'desc' => 'Enter a youtube or vimeo URL. Supports services listed at <a href="http://codex.wordpress.org/Embeds">http://codex.wordpress.org/Embeds</a>.',
    'id' => $prefix .'film_embed',
    'type' => 'oembed',
    ),
    array(    //Add a text area
    'name' => 'Film Description',
    'desc' => 'A few words about the film.',
    'std' => '',
    'id' => $prefix .'film_textarea',
    'type' => 'textarea',
    ),
)
);
return $meta_boxes;
}

Here's my modified "Featured Image from oembed" code to work with my meta box:

/**
* Plugin Name: oEmbed Featured Image
* Plugin URI: https://wordpress.stackexchange.com/q/70752/1685
* Description: Automatically set the featured image if an oEmbed-compatible embed is found in the post content.
* Version: 1.0
* Author: TheDeadMedic
* Author URI: https://wordpress.stackexchange.com/users/1685/thedeadmedic
*
* @package oEmbed_Featured_Image
*/

add_action( 'wp_insert_post', array( 'ofi', 'init' ) );

/**
* @package oEmbed_Featured_Image
*/
class ofi
{
/**
 * The post thumbnail ID
 *
 * @var int
 */
private $_thumb_id;

/**
 * The post ID
 *
 * @var int
 */
private $_post_id;

/**
 * Sets up an instance if called statically, and attempts to set the featured
 * image from an embed in the post content (if one has not already been set).
 *
 * @param  int $post_id
 * @return object|null
 */
public function init( $post_id )
{
    if ( ! isset( $this ) )
        return new ofi( $post_id );

    global $wp_embed;

    $this->_post_id = absint( $post_id );

    if ( ! $this->_thumb_id = get_post_meta( $this->_post_id, '_thumbnail_id', true ) ) {
        if ( $content = get_post_field( 'film_embed', $this->_post_id, 'raw' ) ) {

            add_filter( 'oembed_dataparse', array( $this, 'oembed_dataparse' ), 10, 3 );
            $wp_embed->autoembed( $content );
            remove_filter( 'oembed_dataparse', array( $this, 'oembed_dataparse' ), 10, 3 );

        }
    }
}

/**
 * @see init()
 */
public function __construct( $post_id )
{
    $this->init( $post_id );
}

/**
 * Callback for the "oembed_dataparse" hook, which will fire on a successful
 * response from the oEmbed provider.
 *
 * @see WP_oEmbed::data2html()
 *
 * @param string $return The embed HTML
 * @param object $data   The oEmbed response
 * @param string $url    The oEmbed content URL
 */
public function oembed_dataparse( $return, $data, $url )
{
    if ( ! empty( $data->thumbnail_url ) && ! $this->_thumb_id ) {
        // if ( in_array( @ $data->type, array( 'video' ) ) ) // Only set for video embeds
            $this->set_thumb_by_url( $data->thumbnail_url, @ $data->title );
    }
}

/**
 * Attempt to download the image from the URL, add it to the media library,
 * and set as the featured image.
 *
 * @see media_sideload_image()
 *
 * @param string $url
 * @param string $title Optionally set attachment title
 */
public function set_thumb_by_url( $url, $title = null )
{
    /* Following assets will already be loaded if in admin */
    require_once ABSPATH . 'wp-admin/includes/file.php';
    require_once ABSPATH . 'wp-admin/includes/media.php';
    require_once ABSPATH . 'wp-admin/includes/image.php';

    $temp = download_url( $url );

    if ( ! is_wp_error( $temp ) && $info = @ getimagesize( $temp ) ) {
        if ( ! strlen( $title ) )
            $title = null;

        if ( ! $ext = image_type_to_extension( $info[2] ) )
            $ext = '.jpg';

        $data = array(
            'name'     => md5( $url ) . $ext,
            'tmp_name' => $temp,
        );

        $id = media_handle_sideload( $data, $this->_post_id, $title );
        if ( ! is_wp_error( $id ) )
            return update_post_meta( $this->_post_id, '_thumbnail_id', $this->_thumb_id = $id );
    }

    if ( ! is_wp_error( $temp ) )
        @ unlink( $temp );
}
}

I tried modifying this part (from directly above):

if ( $content = get_post_field( 'film_embed', $this->_post_id, 'raw' ) ) {

    add_filter( 'oembed_dataparse', array( $this, 'oembed_dataparse' ), 10, 3 );
    $wp_embed->autoembed( $content );
    remove_filter( 'oembed_dataparse', array( $this, 'oembed_dataparse' ), 10, 3 );

}

...like this to include the possibility of the _cmb_ prefix to film_embed:

if ( $content = get_post_field( 'film_embed', $this->_post_id, 'raw' ) || $content = get_post_field( '_cmb_film_embed', $this->_post_id, 'raw' ) ) {

    add_filter( 'oembed_dataparse', array( $this, 'oembed_dataparse' ), 10, 3 );
    $wp_embed->autoembed( $content );
    remove_filter( 'oembed_dataparse', array( $this, 'oembed_dataparse' ), 10, 3 );

}

...but it didn't work. What am I missing?

1

From what I can tell, this stems from your use of get_post_field(), which is reserved for fields found in the posts table of the database (ie: built-in to WordPress, rather than custom fields). For custom fields you should use get_post_meta().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.