Created
July 11, 2017 16:27
-
-
Save oneguy9/aa86e6d54de1e6430efa4b64ed84b101 to your computer and use it in GitHub Desktop.
adding images through GF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function royeyal_enqueue_styles() { | |
if ( is_rtl() ) { | |
wp_enqueue_style('parent-theme-rtl', get_template_directory_uri() .'/rtl.css'); | |
wp_enqueue_style('local-theme-rtl', get_stylesheet_directory_uri() .'/css/rtl.min.css'); | |
} | |
} | |
add_action('wp_enqueue_scripts', 'royeyal_enqueue_styles', 100); | |
add_action( 'generate_inside_navigation','lh_inside_navigation' ); | |
function lh_inside_navigation() { | |
?> | |
<div class="grid-100 navigation-right"> | |
<?php echo do_shortcode("[widget id='login_logout-2']"); ?> | |
</div> | |
<?php | |
} | |
if ( ! function_exists( 'custom_navigation_mega' ) ) { | |
// Register Navigation Menus | |
function custom_navigation_mega() { | |
$locations = array( | |
'Mega' => __( 'תפריט חיפוש ראשי', 'text_domain' ), | |
); | |
register_nav_menus( $locations ); | |
} | |
add_action( 'init', 'custom_navigation_mega' ); | |
} | |
function wpcontent_svg_mime_type( $mimes = array() ) { | |
$mimes['svg'] = 'image/svg+xml'; | |
$mimes['svgz'] = 'image/svg+xml'; | |
return $mimes; | |
} | |
add_filter( 'upload_mimes', 'wpcontent_svg_mime_type' ); | |
function df_disable_comments_post_types_support() { | |
$post_types = get_post_types(); | |
foreach ($post_types as $post_type) { | |
if(post_type_supports($post_type, 'comments')) { | |
remove_post_type_support($post_type, 'comments'); | |
remove_post_type_support($post_type, 'trackbacks'); | |
} | |
} | |
} | |
add_action('admin_init', 'df_disable_comments_post_types_support'); | |
// Close comments on the front-end | |
function df_disable_comments_status() { | |
return false; | |
} | |
add_filter('comments_open', 'df_disable_comments_status', 20, 2); | |
add_filter('pings_open', 'df_disable_comments_status', 20, 2); | |
// Hide existing comments | |
function df_disable_comments_hide_existing_comments($comments) { | |
$comments = array(); | |
return $comments; | |
} | |
add_filter('comments_array', 'df_disable_comments_hide_existing_comments', 10, 2); | |
// Remove comments page in menu | |
function df_disable_comments_admin_menu() { | |
remove_menu_page('edit-comments.php'); | |
} | |
add_action('admin_menu', 'df_disable_comments_admin_menu'); | |
// Redirect any user trying to access comments page | |
function df_disable_comments_admin_menu_redirect() { | |
global $pagenow; | |
if ($pagenow === 'edit-comments.php') { | |
wp_redirect(admin_url()); exit; | |
} | |
} | |
add_action('admin_init', 'df_disable_comments_admin_menu_redirect'); | |
// Remove comments metabox from dashboard | |
function df_disable_comments_dashboard() { | |
remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal'); | |
} | |
add_action('admin_init', 'df_disable_comments_dashboard'); | |
// Remove comments links from admin bar | |
function df_disable_comments_admin_bar() { | |
if (is_admin_bar_showing()) { | |
remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60); | |
} | |
} | |
add_action('init', 'df_disable_comments_admin_bar'); | |
if ( class_exists( 'Tribe__Events__Main' ) ) { | |
class ContinualMonthViewPagination { | |
public function __construct() { | |
add_filter( 'tribe_events_the_next_month_link', array( $this, 'next_month' ) ); | |
add_filter( 'tribe_events_the_previous_month_link', array( $this, 'previous_month' ) ); | |
} | |
public function next_month() { | |
$url = tribe_get_next_month_link(); | |
$text = tribe_get_next_month_text(); | |
$date = Tribe__Events__Main::instance()->nextMonth( tribe_get_month_view_date() ); | |
return '<a data-month="' . $date . '" href="' . $url . '" rel="next">' . $text . ' <span>»</span></a>'; | |
} | |
public function previous_month() { | |
$url = tribe_get_previous_month_link(); | |
$text = tribe_get_previous_month_text(); | |
$date = Tribe__Events__Main::instance()->previousMonth( tribe_get_month_view_date() ); | |
return '<a data-month="' . $date . '" href="' . $url . '" rel="prev"><span>«</span> ' . $text . ' </a>'; | |
} | |
} | |
new ContinualMonthViewPagination; | |
} | |
add_filter( 'get_the_archive_title', function ($title) { | |
if ( is_archive() ) { | |
$title = post_type_archive_title( '', false ); | |
} | |
if ( '' == $title ){ | |
$title = 'לוח ארועים'; | |
} | |
return $title; | |
}); | |
if ( ! function_exists('cars_create') ) { | |
// Register Custom Post Type | |
function cars_create() { | |
$labels = array( | |
'name' => _x( 'cars', 'Post Type General Name', 'text_domain' ), | |
'singular_name' => _x( 'car', 'Post Type Singular Name', 'text_domain' ), | |
'menu_name' => __( 'מכוניות', 'text_domain' ), | |
'name_admin_bar' => __( 'מכוניות', 'text_domain' ), | |
'archives' => __( 'Item Archives', 'text_domain' ), | |
'attributes' => __( 'Item Attributes', 'text_domain' ), | |
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ), | |
'all_items' => __( 'כל המכוניות', 'text_domain' ), | |
'add_new_item' => __( 'הוסף מכונית', 'text_domain' ), | |
'add_new' => __( 'הוסף מכונית', 'text_domain' ), | |
'new_item' => __( 'מכונית חדשה', 'text_domain' ), | |
'edit_item' => __( 'ערוך מכונית', 'text_domain' ), | |
'update_item' => __( 'עדכן מכונית', 'text_domain' ), | |
'view_item' => __( 'צפה במכונית', 'text_domain' ), | |
'view_items' => __( 'צפה במכוניות', 'text_domain' ), | |
'search_items' => __( 'חפש מכוניות', 'text_domain' ), | |
'not_found' => __( 'לא נמצא', 'text_domain' ), | |
'not_found_in_trash' => __( 'לא נמצא בפח האשפה', 'text_domain' ), | |
'featured_image' => __( 'תמונה ראשית', 'text_domain' ), | |
'set_featured_image' => __( 'עדכן תמונה ראשית', 'text_domain' ), | |
'remove_featured_image' => __( 'הסר תמונה ראשית', 'text_domain' ), | |
'use_featured_image' => __( 'השתמש כתמונה ראשית', 'text_domain' ), | |
'insert_into_item' => __( 'הכנס למכונית', 'text_domain' ), | |
'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ), | |
'items_list' => __( 'Items list', 'text_domain' ), | |
'items_list_navigation' => __( 'Items list navigation', 'text_domain' ), | |
'filter_items_list' => __( 'Filter items list', 'text_domain' ), | |
); | |
$args = array( | |
'label' => __( 'car', 'text_domain' ), | |
'description' => __( 'cars', 'text_domain' ), | |
'labels' => $labels, | |
'supports' => array( 'title', 'thumbnail', 'revisions', ), | |
'taxonomies' => array( '' ), | |
'hierarchical' => false, | |
'public' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'menu_position' => 5, | |
'menu_icon' => 'dashicons-performance', | |
'show_in_admin_bar' => true, | |
'show_in_nav_menus' => true, | |
'can_export' => true, | |
'has_archive' => true, | |
'exclude_from_search' => false, | |
'publicly_queryable' => true, | |
'capability_type' => 'page', | |
'show_in_rest' => false, | |
'rest_base' => 'cars', | |
); | |
register_post_type( 'cars', $args ); | |
} | |
add_action( 'init', 'cars_create', 0 ); | |
} | |
if ( ! function_exists( 'maker' ) ) { | |
// Register Custom Taxonomy | |
function maker() { | |
$labels = array( | |
'name' => _x( 'יצרנים', 'Taxonomy General Name', 'יצרן' ), | |
'singular_name' => _x( 'יצרן', 'Taxonomy Singular Name', 'יצרן' ), | |
'menu_name' => __( 'יצרנים', 'יצרן' ), | |
'all_items' => __( 'כל היצרנים', 'יצרן' ), | |
'parent_item' => __( 'Parent Item', 'יצרן' ), | |
'parent_item_colon' => __( 'Parent Item:', 'יצרן' ), | |
'new_item_name' => __( 'יצרן חדש', 'יצרן' ), | |
'add_new_item' => __( 'הוסף יצרן', 'יצרן' ), | |
'edit_item' => __( 'ערוך יצרן', 'יצרן' ), | |
'update_item' => __( 'עדכן יצרן', 'יצרן' ), | |
'view_item' => __( 'הצג יצרן', 'יצרן' ), | |
'separate_items_with_commas' => __( 'Separate items with commas', 'יצרן' ), | |
'add_or_remove_items' => __( 'Add or remove items', 'יצרן' ), | |
'choose_from_most_used' => __( 'Choose from the most used', 'יצרן' ), | |
'popular_items' => __( 'Popular Items', 'יצרן' ), | |
'search_items' => __( 'Search Items', 'יצרן' ), | |
'not_found' => __( 'Not Found', 'יצרן' ), | |
'no_terms' => __( 'No items', 'יצרן' ), | |
'items_list' => __( 'Items list', 'יצרן' ), | |
'items_list_navigation' => __( 'Items list navigation', 'יצרן' ), | |
); | |
$args = array( | |
'labels' => $labels, | |
'hierarchical' => true, | |
'public' => true, | |
'show_ui' => true, | |
'show_admin_column' => true, | |
'show_in_nav_menus' => true, | |
'show_tagcloud' => true, | |
); | |
register_taxonomy( 'create', array( 'cars' ), $args ); | |
} | |
add_action( 'init', 'maker', 0 ); | |
} | |
if ( ! function_exists( 'degem' ) ) { | |
// Register Custom Taxonomy | |
function degem() { | |
$labels = array( | |
'name' => _x( ' דגמים', 'Taxonomy General Name', 'ֿדגמים' ), | |
'singular_name' => _x( 'דגם', 'Taxonomy Singular Name', 'דגמים' ), | |
'menu_name' => __( 'דגמים', 'דגמים' ), | |
'all_items' => __( 'כל הדגמים', 'דגמים' ), | |
'parent_item' => __( 'Parent Item', 'דגמים' ), | |
'parent_item_colon' => __( 'Parent Item:', 'דגמים' ), | |
'new_item_name' => __( 'תת דגם חדש', 'דגמים' ), | |
'add_new_item' => __( 'הוסף דגם', 'תתדגמים' ), | |
'edit_item' => __( 'ערוך דגם', ' דגמים' ), | |
'update_item' => __( 'עדכן דגם', ' דגמים' ), | |
'view_item' => __( 'צפה בדגם', 'דגמים' ), | |
'separate_items_with_commas' => __( 'Separate items with commas', ' דגמים' ), | |
'add_or_remove_items' => __( '', ' דגמים' ), | |
'choose_from_most_used' => __( 'Choose from the most used', ' דגמים' ), | |
'popular_items' => __( 'Popular Items', 'דגמים' ), | |
'search_items' => __( 'Search Items', ' דגמים' ), | |
'not_found' => __( 'Not Found', ' דגמים' ), | |
'no_terms' => __( 'No items', ' דגמים' ), | |
'items_list' => __( 'Items list', ' דגמים' ), | |
'items_list_navigation' => __( 'Items list navigation', ' דגמים' ), | |
); | |
$args = array( | |
'labels' => $labels, | |
'hierarchical' => true, | |
'public' => true, | |
'show_ui' => true, | |
'show_admin_column' => true, | |
'show_in_nav_menus' => true, | |
'show_tagcloud' => true, | |
); | |
register_taxonomy( 'degem', array( 'cars' ), $args ); | |
} | |
add_action( 'init', 'degem', 0 ); | |
} | |
if ( ! function_exists( 'subdegem' ) ) { | |
// Register Custom Taxonomy | |
function subdegem() { | |
$labels = array( | |
'name' => _x( 'תת דגמים', 'Taxonomy General Name', 'תת דגמים' ), | |
'singular_name' => _x( 'תת דגם', 'Taxonomy Singular Name', 'דגמים' ), | |
'menu_name' => __( 'תת דגמים', 'תת דגמים' ), | |
'all_items' => __( 'כל הדגמים', 'תת דגמים' ), | |
'parent_item' => __( 'Parent Item', 'דגמים' ), | |
'parent_item_colon' => __( 'Parent Item:', 'דגמים' ), | |
'new_item_name' => __( 'תת דגם חדש', 'דגמים' ), | |
'add_new_item' => __( 'הוסף תת דגם', 'תת דגמים' ), | |
'edit_item' => __( 'ערוך תת דגם', 'תת דגמים' ), | |
'update_item' => __( 'עדכן תת דגם', 'תת דגמים' ), | |
'view_item' => __( 'צפה בדגם', 'דגמים' ), | |
'separate_items_with_commas' => __( 'Separate items with commas', 'תת דגמים' ), | |
'add_or_remove_items' => __( '', 'תת דגמים' ), | |
'choose_from_most_used' => __( 'Choose from the most used', 'תת דגמים' ), | |
'popular_items' => __( 'Popular Items', 'תת דגמים' ), | |
'search_items' => __( 'Search Items', 'תת דגמים' ), | |
'not_found' => __( 'Not Found', 'תת דגמים' ), | |
'no_terms' => __( 'No items', 'תת דגמים' ), | |
'items_list' => __( 'Items list', 'תת דגמים' ), | |
'items_list_navigation' => __( 'Items list navigation', 'תת דגמים' ), | |
); | |
$args = array( | |
'labels' => $labels, | |
'hierarchical' => false, | |
'public' => true, | |
'show_ui' => true, | |
'show_admin_column' => true, | |
'show_in_nav_menus' => true, | |
'show_tagcloud' => true, | |
); | |
register_taxonomy( 'subdegem', array( 'cars' ), $args ); | |
} | |
add_action( 'init', 'subdegem', 0 ); | |
} | |
if ( ! function_exists( 'region' ) ) { | |
// Register Custom Taxonomy | |
function region() { | |
$labels = array( | |
'name' => _x( 'ישובים', 'Taxonomy General Name', 'ישובים' ), | |
'singular_name' => _x( 'ישוב', 'Taxonomy Singular Name', 'ישובים' ), | |
'menu_name' => __( 'ישובים', 'ישובים' ), | |
'all_items' => __( 'כל הישובים', 'ישובים' ), | |
'parent_item' => __( 'Parent Item', 'ישובים' ), | |
'parent_item_colon' => __( 'Parent Item:', 'ישובים' ), | |
'new_item_name' => __( 'דגם חדש', 'ישובים' ), | |
'add_new_item' => __( 'הוסף ישוב', 'ישובים' ), | |
'edit_item' => __( 'ערוך ישוב', 'ישובים' ), | |
'update_item' => __( 'עדכן ישוב', 'ישובים' ), | |
'view_item' => __( 'צפה בישוב', 'ישובים' ), | |
'separate_items_with_commas' => __( 'Separate items with commas', 'ישובים' ), | |
'add_or_remove_items' => __( '', 'ישובים' ), | |
'choose_from_most_used' => __( 'Choose from the most used', 'ישובים' ), | |
'popular_items' => __( 'Popular Items', 'ישובים' ), | |
'search_items' => __( 'Search Items', 'ישובים' ), | |
'not_found' => __( 'Not Found', 'ישובים' ), | |
'no_terms' => __( 'No items', 'ישובים' ), | |
'items_list' => __( 'Items list', 'ישובים' ), | |
'items_list_navigation' => __( 'Items list navigation', 'ישובים' ), | |
); | |
$args = array( | |
'labels' => $labels, | |
'hierarchical' => false, | |
'public' => true, | |
'show_ui' => true, | |
'show_admin_column' => true, | |
'show_in_nav_menus' => true, | |
'show_tagcloud' => true, | |
); | |
register_taxonomy( 'region', array( 'cars' ), $args ); | |
} | |
add_action( 'init', 'region', 0 ); | |
} | |
if ( ! function_exists( 'typeof' ) ) { | |
// Register Custom Taxonomy | |
function typeof() { | |
$labels = array( | |
'name' => _x( 'סוג', 'Taxonomy General Name', 'סוגי רכב' ), | |
'singular_name' => _x( 'סוג', 'Taxonomy Singular Name', 'סוגי רכב' ), | |
'menu_name' => __( 'סוגי רכב', 'סוגי רכב' ), | |
'all_items' => __( 'כל סוגי הרכב', 'סוגי רכב' ), | |
'parent_item' => __( 'Parent Item', 'סוגי רכב' ), | |
'parent_item_colon' => __( 'Parent Item:', 'סוגי רכב' ), | |
'new_item_name' => __( 'סוג רכב חדש', 'סוגי רכב' ), | |
'add_new_item' => __( 'הוסף סוג', 'סוגי רכב' ), | |
'edit_item' => __( 'ערוך סוג', 'סוגי רכב' ), | |
'update_item' => __( 'עדכן סוג', 'סוגי רכב' ), | |
'view_item' => __( 'צפה בסוג', 'סוגי רכב' ), | |
'separate_items_with_commas' => __( 'Separate items with commas', 'סוגי רכב' ), | |
'add_or_remove_items' => __( '', 'סוגי רכב' ), | |
'choose_from_most_used' => __( 'Choose from the most used', 'סוגי רכב' ), | |
'popular_items' => __( 'Popular Items', 'סוגי רכב' ), | |
'search_items' => __( 'Search Items', 'סוגי רכב' ), | |
'not_found' => __( 'Not Found', 'סוגי רכב' ), | |
'no_terms' => __( 'No items', 'סוגי רכב' ), | |
'items_list' => __( 'Items list', 'סוגי רכב' ), | |
'items_list_navigation' => __( 'Items list navigation', 'סוגי רכב' ), | |
); | |
$args = array( | |
'labels' => $labels, | |
'hierarchical' => false, | |
'public' => true, | |
'show_ui' => true, | |
'show_admin_column' => true, | |
'show_in_nav_menus' => true, | |
'show_tagcloud' => true, | |
); | |
register_taxonomy( 'typeof', array( 'cars' ), $args ); | |
} | |
add_action( 'init', 'typeof', 0 ); | |
} | |
function year_shortcode2() { | |
echo '<p class="margin5">'; | |
$field_name = "year_make"; | |
$field = get_field_object($field_name); | |
echo '<span class="label">'; | |
echo $field['label']; | |
echo '</span>'; | |
echo $field['value']; | |
echo '</p>'; | |
the_field('year_make'); | |
} | |
add_shortcode( 'year2', 'year_shortcode2' ); | |
function year_shortcode() { | |
while ( have_posts() ) : the_post(); | |
echo '<div class="cartitlewrap">'; | |
echo '<span class="cartitle">'; | |
the_title(); | |
echo '</span>'; | |
echo '<span class="cartitle">'; | |
the_field('year_make'); | |
echo '</span>'; | |
global $post; | |
$terms = get_the_terms( $post->ID , 'subdegem' ); | |
// Loop over each item since it's an array | |
if ( $terms != null ){ | |
foreach( $terms as $term ) { | |
// Print the name method from $term which is an OBJECT | |
echo '<span class="cartitle">'; | |
print $term->slug ; | |
echo '</span>'; | |
echo '</div>'; | |
// Get rid of the other data stored in the object, since it's not needed | |
unset($term); | |
} } | |
endwhile; // end of the loop. | |
} | |
add_shortcode( 'year', 'year_shortcode' ); | |
add_filter( 'generate_logo', 'generate_category_logo' ); | |
function generate_category_logo() | |
{ | |
if ( is_front_page() ) { | |
return 'http://carsil.co.il/wp-content/uploads/2017/05/logo.png'; | |
} | |
return 'http://carsil.co.il/wp-content/uploads/2017/05/logob.png'; | |
} | |
add_action("gform_after_submission_11", "add_team_member_11", 10, 2); | |
function add_team_member_11($entry, $form) | |
{ | |
$team_member_name = $entry['1']; | |
$team_member_title = $entry['4']; | |
$team_member_bio = $entry['2']; | |
$team_member_image = $entry['3']; | |
$team_member_email = $entry['5']; | |
$field_key = "field_54f61acd460bb"; // Form - Project Add Team Member Repeater field key | |
$post_id = $_SESSION['project_id']; | |
$value = get_field( $field_key, $post_id ); // must return image as ID | |
$value = ( $value == '0' ? array() : $value ); // if value empty, set it as array | |
if( function_exists( 'jdn_create_image_id' ) ) : | |
$att_id = jdn_create_image_id( $entry[3], $post_id ); | |
endif; | |
$value[] = array( | |
'name' => $team_member_name, | |
'bio' => $team_member_bio, | |
'image' => $att_id, // must be an ID | |
'email' => $team_member_email, | |
'title' => $team_member_title, | |
); | |
update_field( $field_key, $value, $post_id ); | |
} | |
class WDSP_Event_Form { | |
/** | |
* Parent plugin class | |
* | |
* @var class | |
* @since NEXT | |
*/ | |
protected $plugin = null; | |
/** | |
* Constructor | |
* | |
* @since NEXT | |
* @param object $plugin Main plugin object. | |
*/ | |
public function __construct( $plugin ) { | |
$this->plugin = $plugin; | |
$this->hooks(); | |
} | |
/** | |
* Initiate our hooks | |
* | |
* @since NEXT | |
*/ | |
public function hooks() { | |
add_filter( 'gform_after_submission', array( $this, 'save_gravity_form_event_registration_file_to_media_library' ), 10, 2 ); | |
} | |
/** | |
* Insert event registration file in the media library and attach it to the post. | |
* Delete the copy of it that was originally saved to the GF uploads directory. | |
* | |
* @since NEXT | |
* @author Kellen Mace | |
* @param array $entry The entry data. | |
* @param array $form The form data. | |
*/ | |
public function save_gravity_form_event_registration_file_to_media_library( $entry, $form ) { | |
if ( 'Submit Event' !== $form['carup'] ) { | |
return; | |
} | |
if ( ! isset( $entry['post_id'] ) ) { | |
return; | |
} | |
$gf_field_id = 21; | |
$acf_field_id = 'field_59539f4fa437f'; | |
$post = get_post( $entry['post_id'] ); | |
$file_url = $entry[ $gf_field_id ]; | |
$relative_file_path = parse_url( $file_url, PHP_URL_PATH ); | |
$absolute_file_path = untrailingslashit( ABSPATH ) . $relative_file_path; | |
$file_type = wp_check_filetype( $absolute_file_path )['type']; | |
$timeout_seconds = 5; | |
// Sideload media file. | |
$sideload_result = $this->sideload_media_file( $file_url, $file_type, $timeout_seconds ); | |
// If an error occurred while trying to sideload the media file; continue to next blog. | |
if ( ! $sideload_result || ! empty( $sideload_result['error'] ) ) { | |
return; | |
} | |
$new_file_path = $sideload_result['file']; | |
$new_file_type = $sideload_result['type']; | |
// Insert file into the media library. | |
$attachment_id = $this->insert_media_file( $new_file_path, $new_file_type ); | |
if ( $attachment_id ) { | |
// Update the ACF field to reference the newly uploaded file. | |
update_field( $acf_field_id, $attachment_id, $post->ID ); | |
} | |
// Delete the original file from the Gravity Forms upload directory. | |
unlink( $absolute_file_path ); | |
} | |
/** | |
* Sideload Media File | |
* | |
* @since 0.1.0 | |
* @author Kellen Mace | |
* @param string $file_url The URL of the file to sideload. | |
* @param string $file_type The file type of the file to sideload. | |
* @param int $timeout_seconds The number of seconds to allow before sideloading times out. | |
* @return array On success, returns an associative array of file attributes. On failure, returns | |
* $overrides['upload_error_handler'](&$file, $message ) or array( 'error'=>$message ). | |
*/ | |
private function sideload_media_file( $file_url, $file_type, $timeout_seconds ) { | |
// Gives us access to the download_url() and wp_handle_sideload() functions. | |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); | |
// Download file to temp dir. | |
$temp_file = download_url( $file_url, $timeout_seconds ); | |
if ( is_wp_error( $temp_file ) ) { | |
return false; | |
} | |
// Array based on $_FILE as seen in PHP file uploads. | |
$file = array( | |
'name' => basename( $file_url ), | |
'type' => $file_type, | |
'tmp_name' => $temp_file, | |
'error' => 0, | |
'size' => filesize( $temp_file ), | |
); | |
$overrides = array( | |
// Tells WordPress to not look for the POST form | |
// fields that would normally be present, default is true, | |
// we downloaded the file from a remote server, so there | |
// will be no form fields. | |
'test_form' => false, | |
// Setting this to false lets WordPress allow empty files – not recommended. | |
'test_size' => true, | |
// A properly uploaded file will pass this test. | |
// There should be no reason to override this one. | |
'test_upload' => true, | |
); | |
// Move the temporary file into the uploads directory. | |
return wp_handle_sideload( $file, $overrides ); | |
} | |
/** | |
* Insert media file into the current site | |
* | |
* @since 0.1.0 | |
* @author Kellen Mace | |
* @param string $file_path The path to the media file. | |
* @param string $file_type The mime type of the media file. | |
* @return int $inserted_attachment_id The inserted attachment ID, or 0 on failure. | |
*/ | |
private function insert_media_file( $file_path = '', $file_type = '' ) { | |
if ( ! $file_path || ! $file_type ) { | |
return false; | |
} | |
// Get the path to the uploads directory. | |
$wp_upload_dir = wp_upload_dir(); | |
// Prepare an array of post data for the attachment. | |
$attachment_data = array( | |
'guid' => $wp_upload_dir['url'] . '/' . basename( $file_path ), | |
'post_mime_type' => $file_type, | |
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file_path ) ), | |
'post_content' => '', | |
'post_status' => 'inherit', | |
); | |
// Insert the attachment. | |
$inserted_attachment_id = wp_insert_attachment( $attachment_data, $file_path ); | |
$inserted_attachment_path = get_attached_file( $inserted_attachment_id ); | |
// Update the attachment metadata. | |
$this->update_inserted_attachment_metadata( $inserted_attachment_id, $inserted_attachment_path ); | |
return $inserted_attachment_id; | |
} | |
/** | |
* Update inserted attachment metadata | |
* | |
* @since 0.1.0 | |
* @author Kellen Mace | |
* @param int $inserted_attachment_id The inserted attachment ID. | |
* @param string $file_path The path to the media file. | |
*/ | |
private function update_inserted_attachment_metadata( $inserted_attachment_id, $file_path ) { | |
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it. | |
require_once( ABSPATH . 'wp-admin/includes/image.php' ); | |
// Generate metadata for the attachment and update the database record. | |
$attach_data = wp_generate_attachment_metadata( $inserted_attachment_id, $file_path ); | |
wp_update_attachment_metadata( $inserted_attachment_id, $attach_data ); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment