Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create post, add attachment and update field via ACF
<?php
/*
*
* This small scripts creates a post and attaches a file using Advanced custom field.
*
* Created by Miguel Garrido
* miguel.co.nz | miguel@blacksheepdesign.co.nz
*
* Notes:
*
* The purpose of this GIST is for reference only as it may be helpful for you.
*
* Further reading and references:
*
* http://codex.wordpress.org/Function_Reference/wp_insert_attachment
* http://www.jqui.net/wordpress/acf-upload-image-made-easy/
* http://stackoverflow.com/questions/15638046/how-to-upload-an-image-on-acf-with-update-field-on-wordpress
*
*/
class import {
public static function init(){
$files = self::get_files();
for($i = 0 ; $i < sizeof($files) ; $i++ ){
// Base url to test if file exists
$base_url = '/full/path/to/file/';
$_full_file_url = $base_url . $files[$i]->file;
// File url under wp-content/uploads/
$file_url = 'docs/' . $files[$i]->file;
// Test if file exists
$file = fopen( $_full_file_url , 'r' );
if( $file !== false ){
// Display on screen, just for fun
echo 'Creating post: ' . $files[$i]->name . '<br />';
// Posts arguments for our new post
$args = array(
'post_title' => $files[$i]->name,
'post_type' => 'resources',
'post_status' => 'publish',
'post_content' => $files[$i]->desc
);
$new_post = wp_insert_post($args);
// Add the file we will update
$att = self::update_attachment( 'file', $new_post , $file_url );
// This is part ACF: field_5590967110b9f is our key
update_field('field_5590967110b9f',$att['attach_id'], $new_post );
}
}
}
// Update attachment file
public static function update_attachment( $f, $pid, $file_url ){
wp_update_attachment_metadata( $pid, $f );
if( empty( $file_url ) )
return false;
$wp_upload_dir = wp_upload_dir();
$filetype = wp_check_filetype( basename( $file_url ), null );
// Prepare an array of post data for the attachment.
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $file_url ),
'post_parent' => $pid,
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file_url ) ),
'post_type' => 'attachment',
'post_content' => '',
'post_status' => 'inherit',
'post_mime_type' => $filetype['type'],
);
$attach_id = wp_insert_attachment( $attachment, $file_url );
return array(
'pid' => $pid,
'url' => $file_url,
'attach_id' => $attach_id
);
}
// Our files data (name, description and file_url) were in another database and this info was important to keep
public static function get_files(){
if( is_user_logged_in() ){
$old_db = new wpdb(DB_USER, DB_PASS, DB_NAME, DB_HOST);
$old_db->show_errors();
// This was my query, it does not mean your query will need to be like this
$files = $old_db->get_results( "SELECT p.file as 'file', p.name as 'name', p.description as 'desc', p.user_id as 'user_id' FROM wp_documents p" );
return $files;
}
}
}
/* Run with
*
* import::init();
*
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment