Pragmatically i am adding custom field images to my page. The process is
- I create the post
- I update to apply a template
- I add my custom fields which includes an image
This is working and i see the image in wordpress admin. However the following doesn't work in my template until i save the post in the editor.
<img src="<?php echo $largeImage['url'] ?>" />
I thought perhaps i need to add a 4th step which is save the post again.
update_post_meta($post_id);
Which doesn't work. What am i missing here. Do i have to clear the cache or something?
EDIT
1) I create the post
$post_id = wp_insert_post(
array(
'post_name' => $slug,
'post_title' => $title,
'post_content' => $content,
'post_type' => $post_type,
'post_author' => $author_id,
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_status' => 'publish',
)
);
2) I apply template
update_post_meta($post_id, '_wp_page_template', $template_rel_path);
3) I save custom fields
public function update_artwork_post_meta($post_id)
{
$meta_values_to_set = array (
'img_url' => $this->raw['img_url'],
'artist' => $this->raw['artist'],
'artwork_html' => $this->raw['artwork_html'],
'dimensions' => $this->raw['dimensions'],
'period_id' => $this->raw['period_id'],
'full_title' => $this->raw['full_title'],
'artist_sort' => $this->raw['artist_sort'],
'artist_id' => $this->raw['artist_id'],
'title' => $this->raw['title'],
'status' => $this->raw['status'],
'description' => $this->raw['description'],
'medium' => $this->raw['medium'],
'id' => $this->raw['id'],
'modification_date' => $this->raw['modification_date'],
'provenance' => $this->raw['provenance'],
'artist_sort_title_year' => $this->raw['artist_sort_title_year'],
'preview_image' => '',
'portrait' => '',
'landscape' => '',
'width' => '',
'height' => ''
);
foreach($meta_values_to_set as $key => $meta_value){
if ($key == "img_url"){
$tempimgurl = $meta_value;
$meta_value = somatic_attach_external_image( $url = $meta_value, $post_id = $post_id);
}
$success = $this->update_meta($post_id, $key, $meta_value );
}
}
function somatic_attach_external_image( $url = null, $post_id = null, $thumb = null, $filename = null, $post_data = array() ) {
if ( !$url || !$post_id ) return new WP_Error('missing', "Need a valid URL and post ID...");
require_once( ABSPATH . 'wp-admin/includes/file.php' );
// Download file to temp location, returns full server path to temp file, ex; /home/user/public_html/mysite/wp-content/26192277_640.tmp
$tmp = download_url( $url );
// If error storing temporarily, unlink
if ( is_wp_error( $tmp ) ) {
@unlink($file_array['tmp_name']); // clean up
$file_array['tmp_name'] = '';
return $tmp; // output wp_error
}
preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $url, $matches); // fix file filename for query strings
$url_filename = basename($matches[0]); // extract filename from url for title
$url_type = wp_check_filetype($url_filename); // determine file type (ext and mime/type)
// override filename if given, reconstruct server path
if ( !empty( $filename ) ) {
$filename = sanitize_file_name($filename);
$tmppath = pathinfo( $tmp ); // extract path parts
$new = $tmppath['dirname'] . "/". $filename . "." . $tmppath['extension']; // build new path
rename($tmp, $new); // renames temp file on server
$tmp = $new; // push new filename (in path) to be used in file array later
}
// assemble file data (should be built like $_FILES since wp_handle_sideload() will be using)
$file_array['tmp_name'] = $tmp; // full server path to temp file
if ( !empty( $filename ) ) {
$file_array['name'] = $filename . "." . $url_type['ext']; // user given filename for title, add original URL extension
} else {
$file_array['name'] = $url_filename; // just use original URL filename
}
// set additional wp_posts columns
if ( empty( $post_data['post_title'] ) ) {
$post_data['post_title'] = basename($url_filename, "." . $url_type['ext']); // just use the original filename (no extension)
}
// make sure gets tied to parent
if ( empty( $post_data['post_parent'] ) ) {
$post_data['post_parent'] = $post_id;
}
// required libraries for media_handle_sideload
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');
// do the validation and storage stuff
$att_id = media_handle_sideload( $file_array, $post_id, null, $post_data ); // $post_data can override the items saved to wp_posts table, like post_mime_type, guid, post_parent, post_title, post_content, post_status
// If error storing permanently, unlink
if ( is_wp_error($att_id) ) {
@unlink($file_array['tmp_name']); // clean up
return $att_id; // output wp_error
}
// set as post thumbnail if desired
if ($thumb) {
set_post_thumbnail($post_id, $att_id);
}
return $att_id;
}
I call this with the following hook
add_action('init', 'process_artwork');
function process_artwork(){
if(isset($_GET['ss-process'])) {
// process $_POST data here
if ( current_user_can( 'manage_options' ) ) {
// business logic that calls wp_insert_post
}
}
}
init
is not the right hook for a post save and I assumed thatupdate_artwork_post_meta()
was the save function but apparently it isn't. It is impossible to tell what you are doing, much less replicate it-- at least I'm not that clever and don't have the time to deduce and reverse engineer. Please try to post the relevant code in some way that isn't just scattered, disconnected bits an pieces.