function sf_get_post_format_image_src($post_id) { $format_meta = get_post_format_meta($post_id); $match = array(); if ($format_meta['image'] != "") { preg_match('/<img.*?src="([^"]+)"/s', $format_meta['image'], $match); return $match[1]; } }
function ishyoboy_get_post_format_quote_source() { if (function_exists('get_post_format_meta')) { /** * Adding support fo WP >= 3.6.0 */ $meta = get_post_format_meta(get_the_ID()); return $meta['quote_source']; } else { /** * WP <= 3.5.9 */ return IshYoMetaBox::get('ishyoboy_post_quote_source'); } }
function get_the_post_format_url($id = 0) { $post = empty($id) ? get_post() : get_post($id); if (empty($post)) { return ''; } $format = get_post_format($post->ID); if (in_array($format, array('image', 'link', 'quote'))) { $meta = get_post_format_meta($post->ID); $meta_link = ''; switch ($format) { case 'link': if (!empty($meta['link_url'])) { $meta_link = $meta['link_url']; } break; case 'image': if (!empty($meta['url'])) { $meta_link = $meta['url']; } break; case 'quote': if (!empty($meta['quote_source_url'])) { $meta_link = $meta['quote_source_url']; } break; } if (!empty($meta_link)) { return apply_filters('get_the_post_format_url', esc_url_raw($meta_link), $post); } } return apply_filters('the_permalink', get_permalink()); }
/** * Return the first image in the current (@global) post's content * * @since 3.6.0 * * @param string $attached_size If an attached image is found, the size to display it. * @param WP_Post $post Optional. Used instead of global $post when passed. */ function get_the_post_format_image($attached_size = 'full', &$post = null) { if (empty($post)) { $post = get_post(); } if (empty($post)) { return ''; } $cache_key = "image:{$attached_size}"; if (isset($post->format_content[$cache_key])) { return $post->format_content[$cache_key]; } if (!isset($post->format_content)) { $post->format_content = array(); } $matched = false; $meta = get_post_format_meta($post->ID); $link_fmt = '%s'; if (!empty($meta['url'])) { $link_fmt = '<a href="' . esc_url($meta['url']) . '">%s</a>'; } if (!empty($meta['image'])) { if (is_numeric($meta['image'])) { $image = wp_get_attachment_image(absint($meta['image']), $attached_size); // wrap image in <a> if (!empty($meta['url'])) { $image = sprintf($link_fmt, $image); } } elseif (has_shortcode($meta['image'], 'caption')) { // wrap <img> in <a> if (!empty($meta['url']) && false === strpos($meta['image'], '<a ')) { $meta['image'] = preg_replace('#(<img[^>]+>)#', sprintf('<a href="%s">$1</a>', esc_url($meta['url'])), $meta['image']); } $attachment_id = img_html_to_post_id($meta['image'], $matched_html); if ($attachment_id && $matched_html) { $meta['image'] = str_replace($matched_html, wp_get_attachment_image($attachment_id, $attached_size), $meta['image']); $attachment = wp_get_attachment_image_src($attachment_id, $attached_size); $attachment_width = !empty($attachment[1]) ? $attachment[1] : 0; if ($attachment_width && preg_match_all('#width=([\'"])(.+?)\\1#is', $meta['image'], $matches) && !empty($matches)) { foreach ($matches[2] as $width) { if ($width != $attachment_width) { $meta['image'] = str_replace($matches[0], sprintf('width="%d"', $attachment_width), $meta['image']); } } } } $image = do_shortcode($meta['image']); } elseif (!preg_match('#<[^>]+>#', $meta['image'])) { // not HTML, assume URL $attachment_id = attachment_url_to_postid($meta['image']); if ($attachment_id) { $image = wp_get_attachment_image($attachment_id, $attached_size); } else { $image = sprintf('<img src="%s" alt="" />', esc_url($meta['image'])); } } else { // assume HTML $image = $meta['image']; $attachment_id = img_html_to_post_id($image, $matched_html); if ($attachment_id && $matched_html) { $image = str_replace($matched_html, wp_get_attachment_image($attachment_id, $attached_size), $image); } } if (false === strpos($image, '<a ')) { $post->format_content[$cache_key] = sprintf($link_fmt, $image); } else { $post->format_content[$cache_key] = $image; } return $post->format_content[$cache_key]; } $medias = get_attached_images($post->ID); if (!empty($medias)) { $media = reset($medias); $sizes = get_intermediate_image_sizes(); $sizes[] = 'full'; // Add original image source. $urls = array(); foreach ($sizes as $size) { $image = wp_get_attachment_image_src($media->ID, $size); if ($image) { $urls[] = reset($image); } } // Add media permalink. $urls[] = get_attachment_link($media->ID); $count = 1; $content = $post->post_content; if (preg_match_all('/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER) && !empty($matches)) { foreach ($matches as $shortcode) { if ('caption' === $shortcode[2]) { foreach ($urls as $url) { if (strstr($shortcode[0], $url)) { if (!$matched) { $matched = do_shortcode($shortcode[0]); } $content = str_replace($shortcode[0], '', $content, $count); } } } } } foreach (array('a', 'img') as $tag) { if (preg_match_all('#' . get_tag_regex($tag) . '#', $content, $matches, PREG_SET_ORDER) && !empty($matches)) { foreach ($matches as $match) { foreach ($urls as $url) { if (strstr($match[0], $url)) { if (!$matched) { $matched = $match[0]; } $content = str_replace($match[0], '', $content, $count); } } } } } $post->split_content = $content; if (!$matched) { $image = wp_get_attachment_image($media->ID, $attached_size); $post->format_content[$cache_key] = sprintf($link_fmt, $image); } else { $post->format_content[$cache_key] = $matched; if (!empty($meta['url']) && false === stripos($matched, '<a ')) { $post->format_content[$cache_key] = sprintf($link_fmt, $matched); } } return $post->format_content[$cache_key]; } $content = $post->post_content; $htmls = get_content_images($content, true, true, 1); if (!empty($htmls)) { $html = reset($htmls); $attachment_id = img_html_to_post_id($html, $matched_html); if ($attachment_id && $matched_html) { $html = str_replace($matched_html, wp_get_attachment_image($attachment_id, $attached_size), $html); } $post->split_content = $content; $post->format_content[$cache_key] = sprintf($link_fmt, $html); return $post->format_content[$cache_key]; } }
/** * Render the screen options tab. * * @since 3.3.0 */ public function render_screen_options() { global $wp_meta_boxes, $wp_list_table; $columns = get_column_headers($this); $hidden = get_hidden_columns($this); $post = get_post(); ?> <div id="screen-options-wrap" class="hidden" tabindex="-1" aria-label="<?php esc_attr_e('Screen Options Tab'); ?> "> <form id="adv-settings" action="" method="post"> <?php if (isset($wp_meta_boxes[$this->id]) || $this->get_option('per_page') || $columns && empty($columns['_title'])) { ?> <h5><?php _e('Show on screen'); ?> </h5> <?php } if (isset($wp_meta_boxes[$this->id])) { ?> <div class="metabox-prefs"> <?php meta_box_prefs($this); if ('dashboard' === $this->id && has_action('welcome_panel') && current_user_can('edit_theme_options')) { if (isset($_GET['welcome'])) { $welcome_checked = empty($_GET['welcome']) ? 0 : 1; update_user_meta(get_current_user_id(), 'show_welcome_panel', $welcome_checked); } else { $welcome_checked = get_user_meta(get_current_user_id(), 'show_welcome_panel', true); if (2 == $welcome_checked && wp_get_current_user()->user_email != get_option('admin_email')) { $welcome_checked = false; } } echo '<label for="wp_welcome_panel-hide">'; echo '<input type="checkbox" id="wp_welcome_panel-hide"' . checked((bool) $welcome_checked, true, false) . ' />'; echo _x('Welcome', 'Welcome panel') . "</label>\n"; } elseif ('post' == $this->base && post_type_supports($this->post_type, 'post-formats') && apply_filters('enable_post_format_ui', true, $post)) { // If the user has explicitly set a screen option, use it, otherwise the UI is shown // when the theme supports formats, or if the site has formats assigned to posts. $post_format_user_option = get_user_option('post_formats_' . $post->post_type); if (false !== $post_format_user_option) { $show_post_format_ui = (bool) $post_format_user_option; } else { $show_post_format_ui = current_theme_supports('post-formats') || (bool) get_terms('post_format', array('number' => 1)); } if (!$show_post_format_ui && 'auto-draft' != $post->post_status) { $meta = get_post_format_meta($post->ID); $format_meta_keys = array('link' => array('linkurl'), 'image' => array('url', 'image'), 'quote' => array('quote_source_name', 'quote_source_url'), 'video' => array('video_embed'), 'audio' => array('audio_embed')); // If there's any structured post format data, enforce the UI display. $format_meta_keys = isset($format_meta_keys[get_post_format()]) ? $format_meta_keys[get_post_format()] : array(); foreach ($format_meta_keys as $key) { if (!empty($meta[$key])) { $show_post_format_ui = true; } } } echo '<label for="show_post_format_ui">'; echo '<input type="checkbox" id="show_post_format_ui"' . checked($show_post_format_ui, true, false) . ' />'; echo __('Post Formats') . "</label>\n"; } ?> <br class="clear" /> </div> <?php } if ($columns) { if (!empty($columns['_title'])) { ?> <h5><?php echo $columns['_title']; ?> </h5> <?php } ?> <div class="metabox-prefs"> <?php $special = array('_title', 'cb', 'comment', 'media', 'name', 'title', 'username', 'blogname'); foreach ($columns as $column => $title) { // Can't hide these for they are special if (in_array($column, $special)) { continue; } if (empty($title)) { continue; } if ('comments' == $column) { $title = __('Comments'); } $id = "{$column}-hide"; echo '<label for="' . $id . '">'; echo '<input class="hide-column-tog" name="' . $id . '" type="checkbox" id="' . $id . '" value="' . $column . '"' . checked(!in_array($column, $hidden), true, false) . ' />'; echo "{$title}</label>\n"; } ?> <br class="clear" /> </div> <?php } $this->render_screen_layout(); $this->render_per_page_options(); echo $this->_screen_settings; ?> <div><?php wp_nonce_field('screen-options-nonce', 'screenoptionnonce', false); ?> </div> </form> </div> <?php }
/** * Image post format custom output */ function duena_post_format_image() { global $post; if (function_exists('get_post_format_meta')) { $meta = get_post_format_meta($post->ID); } if (!empty($meta['image'])) { $att_id = img_html_to_post_id($meta['image']); $image = wp_get_attachment_image($att_id, array(770, 295)); $cur_url = wp_get_attachment_url($att_id, false); } elseif (has_post_thumbnail()) { $post_thumbnail_id = get_post_thumbnail_id(); $image = wp_get_attachment_image($post_thumbnail_id, array(770, 295)); $cur_url = wp_get_attachment_url($post_thumbnail_id, false); } else { $args = array('post_type' => 'attachment', 'post_mime_type' => 'image', 'numberposts' => 1, 'post_status' => null, 'post_parent' => $post->ID, 'orderby' => 'menu_order', 'order' => 'asc'); $attachments = get_posts($args); if ($attachments) { foreach ($attachments as $attachment) { $image = wp_get_attachment_image($attachment->ID, array(770, 295)); $cur_url = wp_get_attachment_url($attachment->ID, false); } } } if (isset($cur_url)) { echo '<a href="' . $cur_url . '" class="lightbox_img single" data-effect="mfp-zoom-in">' . $image . '</a>'; } }
$post_format = $_REQUEST['format']; } } $admin_body_class .= ' wp-format-' . $post_format; $show_post_format_ui = false; if (apply_filters('enable_post_format_ui', true, $post)) { // If the user has explicitly set a screen option, use it, otherwise the UI is shown // when the theme supports formats, or if the site has formats assigned to posts. $post_format_user_option = get_user_option('post_formats_' . $post->post_type); if (false !== $post_format_user_option) { $show_post_format_ui = (bool) $post_format_user_option; } else { $show_post_format_ui = current_theme_supports('post-formats') || (bool) get_terms('post_format', array('number' => 1)); } if (!$show_post_format_ui) { $meta = get_post_format_meta($post->ID); $format_meta_keys = array('link' => array('linkurl'), 'image' => array('url', 'image'), 'quote' => array('quote_source_name', 'quote_source_url'), 'video' => array('video_embed'), 'audio' => array('audio_embed')); // If there's any structured post format data, enforce the UI display. $format_meta_keys = isset($format_meta_keys[get_post_format()]) ? $format_meta_keys[get_post_format()] : array(); foreach ($format_meta_keys as $key) { if (!empty($meta[$key])) { $show_post_format_ui = true; } } } } if ($show_post_format_ui) { $admin_body_class .= ' wp-post-format-show-ui'; } } if (wp_is_mobile()) {
function wp_rp_get_attached_img_url($related_post, $size) { $extracted_image = get_post_meta($related_post->ID, '_wp_rp_image', true); if ($extracted_image === 'empty') { return false; } $image_data = wp_rp_get_image_data((int) $extracted_image); if (!$image_data && $extracted_image) { // image_id in the db is incorrect delete_post_meta($related_post->ID, '_wp_rp_image'); } if (!$image_data && has_post_thumbnail($related_post->ID)) { $image_data = wp_rp_get_image_data(get_post_thumbnail_id($related_post->ID)); } if (!$image_data && function_exists('get_post_format_meta') && function_exists('img_html_to_post_id')) { // WP 3.6 Image post format. Check wp-includes/media.php:get_the_post_format_image for the reference. $meta = get_post_format_meta($related_post->ID); if (!empty($meta['image'])) { if (is_numeric($meta['image'])) { $image_id = absint($meta['image']); } else { $image_id = img_html_to_post_id($meta['image']); } $image_data = wp_rp_get_image_data($image_id); } } if (!$image_data) { wp_rp_extract_images_from_post($related_post); return false; } if ($img_src = wp_rp_get_image_with_exact_size($image_data, $size)) { return $img_src['url']; } wp_rp_extract_images_from_post($related_post, $image_data['id']); return false; }
<?php /** * Structured data fields for the Post Formats UI * * @package WordPress * @subpackage Administration */ defined('ABSPATH') or die; global $wp_embed; $format_meta = get_post_format_meta($post_ID); wp_nonce_field('show-post-format-ui_' . $post_type, 'show_post_format_ui_nonce', false); ?> <div class="wp-post-format-ui"> <div class="post-formats-fields"> <input type="hidden" name="post_format" id="post_format" value="<?php echo esc_attr($post_format); ?> " /> <div class="field wp-format-quote"> <label for="wp_format_quote_source"><?php _e('Quote source'); ?> </label> <input type="text" id="wp_format_quote_source" name="_format_quote_source_name" value="<?php echo esc_attr($format_meta['quote_source_name']); ?> " class="widefat" /> </div>