Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@teledirigido
Created July 6, 2015 01:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save teledirigido/e95868946cc2a0bf9e80 to your computer and use it in GitHub Desktop.
Save teledirigido/e95868946cc2a0bf9e80 to your computer and use it in GitHub Desktop.
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