protected function build_block_elements() { // Subheader. $section_header_text = !empty($this->input['uploaded_files_header']) ? $this->input['uploaded_files_header'] : __('Available downloads', EXCHANGE_PLUGIN); $section_header_mods = array('type' => 'taped_header', 'colour' => exchange_slug_to_hex('blue-3')); $section_header = new SectionHeader($section_header_text, $this->element, $section_header_mods); $this->output .= $section_header->embed(); // Paragraph. $paragraph_text = '<ul class="documentblock__file-list dont-break-out">'; foreach ($this->input['add_file'] as $doc) { if (!is_numeric($doc['file'])) { continue; } if (function_exists('acf_get_attachment')) { $meta = acf_get_attachment($doc['file']); } if (empty($meta)) { continue; } $description = !empty($meta['description']) ? $meta['description'] : $meta['filename']; $paragraph_text .= '<li class="documentblock__file-list__item">'; $paragraph_text .= '<a href="' . $meta['url'] . '" target="_blank">' . $description . '</a>'; $paragraph_text .= '</li>'; } $paragraph_text .= '</ul>'; $paragraph = new Paragraph($paragraph_text, $this->element); $this->output .= $paragraph->embed(); }
/** * Get user image. * * @return object $image Image Pattern object. */ private function get_user_image() { $image = $this->user_meta['user_image'][0]; if (empty($image)) { return; } if (function_exists('acf_get_attachment')) { $input = acf_get_attachment($image); if (empty($input)) { return; } $image_mods = array('style' => 'rounded'); $image_obj = new Image($input, $this->element, $image_mods); if (!$image_obj instanceof Image) { return; } } return $image_obj; }
function format_value($value, $post_id, $field) { // bail early if no value if (empty($value)) { return $value; } // convert to int $value = intval($value); // format if ($field['return_format'] == 'url') { return wp_get_attachment_url($value); } elseif ($field['return_format'] == 'array') { return acf_get_attachment($value); } return $value; }
function format_value($value, $post_id, $field) { // bail early if no value if (empty($value)) { // return false as $value may be '' (from DB) which doesn't make much sense return false; } // get posts $posts = acf_get_posts(array('post_type' => 'attachment', 'post__in' => $value)); // update value to include $post foreach (array_keys($posts) as $i) { $posts[$i] = acf_get_attachment($posts[$i]); } // return return $posts; }
function format_value($value, $post_id, $field) { // bail early if no value if (empty($value)) { return false; } // get posts $posts = $this->get_attachments($value); // update value to include $post foreach (array_keys($posts) as $i) { $posts[$i] = acf_get_attachment($posts[$i]); } // return return $posts; }
function format_value($value, $post_id, $field) { // bail early if no value if (empty($value)) { return $value; } // force value to array $value = acf_force_type_array($value); // convert values to int $value = array_map('intval', $value); // load posts in 1 query to save multiple DB calls from following code $posts = get_posts(array('posts_per_page' => -1, 'post_type' => 'attachment', 'post_status' => 'any', 'post__in' => $value, 'orderby' => 'post__in')); // reset value $value = array(); // populate value foreach ($posts as $post) { $value[] = acf_get_attachment($post); } // return return $value; }
if ($boxColor == 'Light Blue') { $btnClass = 'btn-blue'; } elseif ($boxColor == 'Yellow') { $btnClass = 'btn-yellow'; } elseif ($boxColor == 'Red') { $btnClass = 'btn-red'; } elseif ($boxColor == 'Green') { $btnClass = 'btn-green'; } elseif ($boxColor == 'Purple') { $btnClass = 'btn-purple'; } else { $btnClass = 'btn-dark-blue'; } $image = get_field('button'); if (!is_array($image)) { $image = acf_get_attachment($image); } if ($image != '') { $url = $image['url']; $title = $image['title']; $alt = $image['alt']; $caption = $image['caption']; $size = 'large'; $thumb = $image['sizes'][$size]; $width = $image['sizes'][$size . '-width']; $height = $image['sizes'][$size . '-height']; } $title = get_the_title(); $santiTitle = sanitize_title_with_dashes($title); $link = $parentLink . 'services-pricing/pet-sitting-dog-walking/#' . $santiTitle; ?>
/** * Create the image attachment and return the new media upload id. * * @author Joshua David Nelson, josh@joshuadnelson.com * * @see http://codex.wordpress.org/Function_Reference/wp_insert_attachment#Example * * @link https://joshuadnelson.com/programmatically-add-images-to-media-library/ * * @param string $image_url The url to the image you're adding to the Media library. * @param int $parent_post_id Optional. Use to attach the media file to a specific post. */ function exchange_create_image_id($image_url, $parent_post_id = null) { // Bail if the image url isn't valid. if (empty($image_url) || !esc_url($image_url)) { return false; } // Escape the url, just to be safe. $image_url = esc_url($image_url); // Cache info on the wp uploads dir. $wp_upload_dir = wp_get_upload_dir(); $wp_upload_path = $wp_upload_dir['basedir']; // Get the file path. $path_array = explode('uploads', $image_url); // File base name, e.g. image.jpg. $file_base_name = basename($image_url); // Combine the two to get the uploaded file path. $uploaded_file_path = $wp_upload_path . $path_array[1]; // Check the type of file. We'll use this as the 'post_mime_type'. $filetype = wp_check_filetype($file_base_name, null); // Error check. if (!empty($filetype) && is_array($filetype)) { // Create attachment title - basically, pull out the text. $post_title = preg_replace('/\\.[^.]+$/', '', $file_base_name); // Prepare an array of post data for the attachment. $attachment = array('guid' => $wp_upload_dir['url'] . '/' . basename($uploaded_file_path), 'post_mime_type' => $filetype['type'], 'post_title' => esc_attr($post_title), 'post_content' => '', 'post_status' => 'inherit'); // Set the post parent id if there is one. if (!is_null($parent_post_id) && absint($parent_post_id)) { $attachment['post_parent'] = absint($parent_post_id); } // Insert the attachment. $attach_id = wp_insert_attachment($attachment, $uploaded_file_path); // Error check. if (!is_wp_error($attach_id)) { // Generate wp attachment meta data. if (file_exists(ABSPATH . 'wp-admin/includes/image.php') && file_exists(ABSPATH . 'wp-admin/includes/media.php')) { require_once ABSPATH . 'wp-admin/includes/image.php'; require_once ABSPATH . 'wp-admin/includes/media.php'; $attach_data = wp_generate_attachment_metadata($attach_id, $uploaded_file_path); wp_update_attachment_metadata($attach_id, $attach_data); } // End if file exists check. $attachment_acf = acf_get_attachment($attach_id); } return $attach_id; } else { return false; } // End if $filetype. }
/** * get_acf_posts * * This function queries multiple posts and returns also all the Advanced Custom Fields data set saved in the posts meta. * Meta data is handled the same way as in the get_posts-function. * * @type function * @date 20/3/2015 * @since 0.0.1 * * @param [array] $args Arguments to override the defaults defined in get_wp_query_defaults. * @return [array] array of posts as an associative array with acf fields and meta data */ public static function get_acf_posts($args) { // Some redundancy, but we need these $defaults = self::get_wp_query_defaults(); $options = array_merge($defaults, $args); extract($options); // Perform the basic query first self::get_posts($options); // Temporarily set 'query_object' to 'true' $args['query_object'] = true; self::get_posts($args); // Extend the posts with acf data if (is_array(self::$query->posts)) { // loop through posts and get all acf fields foreach (self::$query->posts as &$p) { $p->fields = get_fields($p->ID); if ($whole_fields) { foreach ($p->fields as $name => &$field) { $field = get_field_object($name, $p->ID, true); } } // Add attachment image to post if (function_exists("acf_get_attachment")) { $attachment_id = get_post_thumbnail_id($p->ID); if ($attachment_id) { $p->image = acf_get_attachment($attachment_id); } } } // Maybe return the whole query object if ($query_object || false === $no_found_rows) { return self::$query; } else { return self::$query->posts; } } else { return false; } }
protected function set_gallery() { if ($this->container->has_gallery) { return; } $unique_arrs = $this->get_gallery_from_acf(); if (empty($unique_arrs) && 'story' === $this->container->type) { // Start over with a new gallery array to be filled with a query. $unique_arrs = array(); $unique_ids = $this->get_gallery_from_query(); if (empty($unique_ids)) { return; } foreach ($unique_ids as $img_id) { if (function_exists('acf_get_attachment')) { $img_arr = acf_get_attachment($img_id); if (!empty($img_arr)) { $unique_arrs[] = $img_arr; } } } } $gallery = $this->prepare_gallery_images($unique_arrs); if (empty($gallery)) { if ($this->container->has_video) { $this->container->has_gallery = true; } } else { $this->container->gallery = $gallery; $this->container->has_gallery = true; } }