i created a code which publish product from front-end issue is when i upload gallery images from frontend only last image upload.
here is code
<form action="#" method="post" enctype="multipart/form-data"> <input type="file" name="agp_gallery[]" id="agp_image_files" style="width:90%; margin-left:5px;" multiple>
here is function multiupload
agp_process_image('agp_image_file', $post_id, $result['caption'], $result['content'], $result['author']);
if ( $_FILES ) {
$files = $_FILES['agp_gallery'];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$_FILES = array("agp_gallery" => $file);
foreach ($_FILES as $file => $array) {
agp_process_wooimage($file, $post_id, $result['caption']);
}
}
}
}
here is main function
function agp_process_wooimage($file, $post_id, $caption){
if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) __return_false();
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$attachment_id = media_handle_upload($file, $post_id);
update_post_meta($post_id, '_product_image_gallery', $attachment_id);
$attachment_data = array(
'ID' => $attachment_id,
'post_excerpt' => $caption
);
wp_update_post($attachment_data);
return $attachment_id;
}
here is a final code :-
// Get the upload attachment files
if ( $_FILES ) {
$files = $_FILES['agp_gallery'];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$_FILES = array("agp_gallery" => $file);
foreach ($_FILES as $file => $array)
{
$newupload = agp_process_wooimage($file,$post_id);
}
}
}
}
function agp_process_wooimage($file, $post_id){
if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) __return_false();
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$attachment_id = media_handle_upload($file, $post_id);
update_post_meta($post_id, array_push($post_id, '_product_image_gallery', $attachment_id));
return $attachment_id;
}
Related
I am researching more and use the code..
I have done this code.
if (!defined('ABSPATH')) {
die;
}
if(!class_exists('ArtistPublic')):
class ArtistPublic
{
public static function createTemplate( ) {
global $user_ID;
$new_post = array(
'post_title' => 'Artist',
'post_content' => 'hello Artist',
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'page',
);
$post_id = wp_insert_post($new_post);
if( !$post_id )
wp_die('Error creating template page');
else
update_post_meta( $post_id, '_wp_page_template', 'artist.php' );
// Filter page template
// add_filter('page_template', 'catch_plugin_template');
}
// Page template filter callback
public static function catch_plugin_template($template) {
if( is_page_template('artist.php') )
$template = WP_PLUGIN_DIR . '/ItgArtist/public/templates/artist.php';
return $template;
}
}
endif;
but create multiple page after refresh.
Is there any good way to create the page like as woocommerce does??
So code be like something
if (!defined('ABSPATH')) {
die;
}
if(!class_exists('ArtistPublic')):
class ArtistPublic {
public static function createTemplate( ) {
if( get_option('artist_page_id') ) :
global $user_ID;
$new_post = array(
'post_title' => 'Artist',
'post_content' => 'hello Artist',
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'page',
);
$post_id = wp_insert_post($new_post);
if( !$post_id ) :
wp_die('Error creating template page');
else :
update_option('artist_page_id', $post_id);
update_post_meta( $post_id, '_wp_page_template', 'artist.php' );
endif;
endif;
// Filter page template
// add_filter('page_template', 'catch_plugin_template');
}
// Page template filter callback
public static function catch_plugin_template($template) {
if( is_page_template('artist.php') )
$template = WP_PLUGIN_DIR . '/ItgArtist/public/templates/artist.php';
return $template;
}
}
endif;
I am trying to update my repeater field from front end-with files. In case with one field file - it works. But when I'm trying to fill repeater with files it doesn't work.
// JS. Ajaxly sending files
var ajaxurl = '<?php echo site_url() ?>/wp-admin/admin-ajax.php';
$('.js-new-msg-send').click(function (e) {
e.preventDefault();
var form = document.forms.namedItem("new_theme");
var formData = new FormData(form);
formData.append('user_id', '<?php echo $userID; ?>');
formData.append('action', 'msg_to_acf');
var xhr = new XMLHttpRequest();
xhr.open('POST', ajaxurl, true);
xhr.send(formData);
})
I found function to handle files uploading to acf field and modified it to handle multiple files from $_FILES variable:
// FOR MULTIPLE FILES
if ( !empty($_FILES[$f]['name']) && is_array($_FILES[$f]['name']) ) {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$file_list = array();
foreach ($_FILES[$f]['name'] as $key => $value) {
$temp_file = array(
'name' => $_FILES[$f]['name'][$key],
'type' => $_FILES[$f]['type'][$key],
'tmp_name' => $_FILES[$f]['tmp_name'][$key],
'error' => $_FILES[$f]['error'][$key],
'size' => $_FILES[$f]['size'][$key]
);
wp_update_attachment_metadata( $pid, $temp_file );
$file = wp_handle_upload( $temp_file , array('test_form' => FALSE, 'action' => 'editpost') );
if ( isset( $file['error'] )) {
return new WP_Error( 'upload_error', $file['error'] );
}
$file_type = wp_check_filetype($temp_file['name'], array(
'jpg|jpeg' => 'image/jpeg',
'gif' => 'image/gif',
'png' => 'image/png',
));
if ($file_type['type']) {
$name_parts = pathinfo( $file['file'] );
$name = $file['filename'];
$type = $file['type'];
$title = $t ? $t : $name;
$content = $c;
$attachment = array(
'post_title' => $title,
'post_type' => 'attachment',
'post_content' => $content,
'post_parent' => $pid,
'post_mime_type' => $type,
'guid' => $file['url'],
);
foreach( get_intermediate_image_sizes() as $s ) {
$sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => true );
$sizes[$s]['width'] = get_option( "{$s}_size_w" ); // For default sizes set in options
$sizes[$s]['height'] = get_option( "{$s}_size_h" ); // For default sizes set in options
$sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options
}
$sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes );
foreach( $sizes as $size => $size_data ) {
$resized = image_make_intermediate_size( $file['file'], $size_data['width'], $size_data['height'], $size_data['crop'] );
if ( $resized )
$metadata['sizes'][$size] = $resized;
}
$attach_id = wp_insert_attachment( $attachment, $file['file'] /*, $pid - for post_thumbnails*/);
if ( !is_wp_error( $id )) {
$attach_meta = wp_generate_attachment_metadata( $attach_id, $file['file'] );
wp_update_attachment_metadata( $attach_id, $attach_meta );
}
$final_temp_arr = array(
'pid' =>$pid,
'url' =>$file['url'],
'file'=>$file,
'attach_id'=>$attach_id
);
array_push($file_list, $final_temp_arr);
}
}
return $file_list;
}
In final, I using this function to get an array of information with attach-id variable that I inserting to repeater.
$att = my_update_attachment( 'msg-file-list' , $post_id );
$files_final_list = array();
foreach ($att as $key => $val) {
array_push($files_final_list, $val['attach_id']);
}
$value_arr = array(
array(
"msg-author" => $user_name,
"msg-date" => $date,
"msg-read-check" => true,
"msg-cont" => $msg_cont,
'msg-file-list' => $files_final_list
)
);
update_field( 'test_file_list' , $files_final_list , $post_id );
I guess that mistake is in last part , where I trying to upload files to repeater. Can someone show me what I'am doing wrong? Thanks for helping.
I want to load my post category in the drop-down select box
how I do this
help me please I am new to programming:
this is my code that creates the custom meta boxes and now in the category boxenter image description here, I want that it loads all the category of post in the drop-down menu: the output of the code is shown in the image attached
<?php
//Start: Adding custom metaboxes
class leaderboarddetailMetabox {
private $screen = array(
'ld-leaderboard',
);
private $meta_fields = array(
array(
'label' => 'Number of Students',
'id' => 'numberofstudent_81418',
'type' => 'number',
),
array(
'label' => 'Select Point Type',
'id' => 'selectpointtype_39141',
'type' => 'select',
'options' => array(
'Select',
'Int',
'Float',
'String',
),
),
array(
'label' => 'Category',
'id' => 'category_59112',
'type' => 'select',
'options' => array(
'Select',
'Course',
'Lesson',
'Topic',
'Quiz',
'Category',
),
),
array(
'label' => 'Time',
'id' => 'time_93126',
'type' => 'date',
),
array(
'label' => 'Shortcode',
'id' => 'shortcode_85946',
'type' => 'text',
'readonly' => true,
),
);
public function __construct() {
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
add_action( 'save_post', array( $this, 'save_fields' ) );
}
// show meta boxes on admin page
public function add_meta_boxes() {
foreach ( $this->screen as $single_screen ) {
add_meta_box(
'leaderboarddetail',
__( 'leader board detail', 'textdomain' ),
array( $this, 'meta_box_callback' ),
$single_screen,
'advanced',
'default'
);
}
}
public function meta_box_callback( $post ) {
wp_nonce_field( 'leaderboarddetail_data', 'leaderboarddetail_nonce' );
$this->field_generator( $post );
}
public function field_generator( $post ) {
$output = '';
foreach ( $this->meta_fields as $meta_field ) {
$label = '<label for="' . $meta_field['id'] . '">' . $meta_field['label'] . '</label>';
$meta_value = get_post_meta( $post->ID, $meta_field['id'], true );
if ( empty( $meta_value ) ) {
$meta_value = $meta_field['default']; }
switch ( $meta_field['type'] ) {
case 'select':
$input = sprintf(
'<select id="%s" name="%s">',
$meta_field['id'],
$meta_field['id']
);
foreach ( $meta_field['options'] as $key => $value ) {
$meta_field_value = !is_numeric( $key ) ? $key : $value;
$input .= sprintf(
'<option %s value="%s">%s</option>',
$meta_value === $meta_field_value ? 'selected' : '',
$meta_field_value,
$value
);
}
$input .= '</select>';
break;
default:
$input = sprintf(
'<input %s id="%s" name="%s" type="%s" value="%s" %s >',
$meta_field['type'] !== 'color' ? 'style="width: 100%"' : '',
$meta_field['id'],
$meta_field['id'],
$meta_field['type'],
$meta_value,
$meta_field['readonly'] ? "readonly" : ""
);
}
$output .= $this->format_rows( $label, $input );
}
echo '<table class="form-table"><tbody>' . $output . '</tbody></table>';
}
public function format_rows( $label, $input ) {
return '<tr><th>'.$label.'</th><td>'.$input.'</td></tr>';
}
// save custom field data to databse and in the field
public function save_fields( $post_id ) {
if ( ! isset( $_POST['leaderboarddetail_nonce'] ) )
return $post_id;
$nonce = $_POST['leaderboarddetail_nonce'];
if ( !wp_verify_nonce( $nonce, 'leaderboarddetail_data' ) )
return $post_id;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
foreach ( $this->meta_fields as $meta_field ) {
if ( isset( $_POST[ $meta_field['id'] ] ) ) {
switch ( $meta_field['type'] ) {
case 'email':
$_POST[ $meta_field['id'] ] = sanitize_email( $_POST[ $meta_field['id'] ] );
break;
case 'text':
$_POST[ $meta_field['id'] ] = sanitize_text_field( $_POST[ $meta_field['id'] ] );
break;
}
update_post_meta( $post_id, $meta_field['id'], $_POST[ $meta_field['id'] ] );
} else if ( $meta_field['type'] === 'checkbox' ) {
update_post_meta( $post_id, $meta_field['id'], '0' );
}
// generate shortcode and save
if( $meta_field['id'] == 'shortcode_85946' ) {
// update meta
update_post_meta( $post_id, $meta_field['id'], "[custom-shortcode id=" . $post_id . "]");
}
}
}
}
if (class_exists('leaderboarddetailMetabox')) {
new leaderboarddetailMetabox;
};
?>
I'm trying to update remote woocommerce product but i'm stucked. How can we complete this code:
class myDemo {
public function __construct() {
$this->url = 'http://localhost/alfa/wp-json/wc/v2/products/2213';
$this->meta_key = 'whatever';
$this->consumer_key = 'ck_5978846499ea9bb1554b420e59072e46b8dedb1d';
$this->consumer_secret = 'cs_1009590c5443306f2082b2e508098be37fba2ee5';
$this->access_token = '';
$this->access_token_secret = '';
$this->method = 'PUT';
$this->set_signature_key();
$this->set_nonce();
$this->set_headers();
$this->set_base_string();
$this->set_signature();
$this->set_headers();
$this->set_header_string();
}
function get_response() {
$url = $this->url;
if( ! empty( $this->meta_key ) ) {
$url = add_query_arg( array( 'meta_key' => $this->meta_key ), $url );
}
$args = array(
'method' => $this->method,
'headers' => array(
'Authorization' => 'OAuth ' . $this->header_string,
),
);
$out = wp_remote_request( $url, $args );
return $out;
}
function set_signature_key() {
$this->signature_key = urlencode( $this->consumer_secret ) . '&' . urlencode( $this->access_token_secret );
}
function set_nonce() {
$this->nonce = wp_create_nonce( rand() . $this->url . $this->method );
}
function set_headers() {
if( ! isset( $this->headers ) ) {
$this->headers = array(
'oauth_consumer_key' => $this->consumer_key,
'oauth_nonce' => $this->nonce,
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_timestamp' => time(),
'oauth_token' => $this->access_token,
'oauth_version' => '1.0',
);
} elseif( isset( $this->signature ) ) {
$this->headers['oauth_signature'] = $this->signature;
}
}
function set_base_string() {
$headers = $this->headers;
$url_params = array(
'meta_key' => $this->meta_key
);
$headers_and_params = array_merge( $headers, $url_params );
ksort( $headers_and_params );
$headers_and_params_string = '';
foreach( $headers_and_params as $key => $value ) {
$headers_and_params_string .= "$key=$value&";
}
$headers_and_params_string = rtrim( $headers_and_params_string, '&' );
$out = $this->method . '&' . rawurlencode( $this->url ) . '&' . rawurlencode( $headers_and_params_string );
$this->base_string = $out;
}
function set_signature() {
$out = hash_hmac( 'sha1', $this->base_string, $this->signature_key, TRUE );
$out = base64_encode( $out );
$this->signature = $out;
}
function set_header_string() {
// Will hold the combined string of headers.
$out = '';
foreach( $this->headers as $key => $value ) {
$value = rawurlencode( $value );
$out .= $key . '=' . '"' . $value . '"' . ', ';
}
$out = rtrim( $out, ', ' );
$this->header_string = $out;
}
}
$demo = new myDemo();
die(json_encode($demo->get_response()));
I'm interesting with that:
https://woocommerce.github.io/woocommerce-rest-api-docs/?php#update-a-product
I saw this code part:
$data = [
'regular_price' => '24.54'
];
I must send this data array to remote woocommerce. But i don't know how.
Thanks for everything.
so i'm trying about 2 3 with this and i can't managed to make it work.
I have this theme : Aruna and he has front end post and when i'm posting a image
i need to write title and upload a image from my computer ,
ok after i done this , he put the image that i upload into one custom field by theme:
The trick is when i want to upload a image from front end i wanna put it automatically on Featured image.
If i use User submited post plugin and i post from him it works .. but i wanna modifiy on my own front end.
It's important for me to make this work because i share a lot on facebook and other social media and feature image do the thing :)
I don't know what code to put here but i try to show you :
Functions.php
case "window-image":
$format = 'image';
$title = esc_html($_POST['title_image']);
$image = esc_url($_POST['postimage']);
$tags = $_POST['tags_image'];
$comments = isset($_POST['comments_image']) ? 1 : 0;
$anonymous = isset($_POST['anonymous_image']) ? 1 : 0;
$comment_status = $comments == 1 ? 'open' : 'closed';
$user_id = $id;
if(($_FILES['image_imagepost']['error'] != 0 && $image == '') || $title == '') {
wp_redirect(home_url('/') . '?posterror=1');
exit;
}
$post = array(
'comment_status' => $comment_status,
'ping_status' => 'open',
'post_author' => $user_id,
'post_content' => '',
'post_status' => $status,
'post_title' => $title,
'post_type' => 'post',
);
$post_id = wp_insert_post($post);
if($image != '') {
$pattern = '~' . get_bloginfo('url') . '~';
if(preg_match($pattern, $image) ) {
add_post_meta($post_id, '_standard_image', $image);
}
else {
//we have an external image :) we save it with the cURL library
$filedetails = pathinfo($image);
$mime = '';
if($filedetails['extension'] == 'jpe' || $filedetails['extension'] == 'jpg' || $filedetails['extension'] == 'jpeg') {
$mime = 'image/jpeg';
}
elseif($filedetails['extension'] == 'png') {
$mime = 'image/png';
}
elseif($filedetails['extension'] == 'gif') {
$mime = 'image/gif';
}
if($mime != '') {
require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
$ch = curl_init($image);
$uploaddir = wp_upload_dir();
$filename = $filedetails['filename'] . rand(1, 9999) . '.' . $filedetails['extension'];
//we add some random digits to make sure it's unique
$fp = fopen(trailingslashit($uploaddir['path']) . $filename, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
$guid = trailingslashit($uploaddir['url']) . $filename;
$attachment = array(
'post_mime_type' => $mime,
'guid' => $guid,
'post_title' => '[External image] : ' . $filedetails['filename'] ,
'post_content' => '',
'post_author' => $user_id,
'post_status' => 'inherit',
'post_date' => date( 'Y-m-d H:i:s' ),
'post_date_gmt' => date( 'Y-m-d H:i:s' )
);
// Add the file to the media library and generate thumbnail.
$attach_id = wp_insert_attachment( $attachment, trailingslashit($uploaddir['url']) . $filename, $post_id );
$attach_data = wp_generate_attachment_metadata( $attach_id, trailingslashit($uploaddir['path']) . $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
add_post_meta($post_id, '_standard_image', trailingslashit($uploaddir['url']) . $filename);
}
}
}
else {
if($_FILES['image_imagepost']['error'] == 0) {
$image = $tmp->saveUpload( 'image_imagepost' );
add_post_meta($post_id, '_standard_image', wp_get_attachment_url($image['attachment_id']) );
}
}
HTML code on front end
<textarea name="title_image" class="form-control" rows="1" placeholder="<?php _e('Post title', 'Aruna');?>"></textarea>
<input type="file" name="image_imagepost" />
<input type="file" name="image_imagepost" /> this do the trick for upload on custom field ...
I can't understand , when i'm posting videos(yt) he take the thumbnail automatically and put it on featured image, I try to take a look at video and image and unsuccessful:
Here is the video code :
<?php
case "window-video":
$format = 'video';
$title = esc_html($_POST['title_video']);
$tags = $_POST['tags_video'];
if($title == '') {
wp_redirect(home_url('/') . '?posterror=1');
exit;
}
$comments = isset($_POST['comments_video']) ? 1 : 0;
$anonymous = isset($_POST['anonymous_video']) ? 1 : 0;
$comment_status = $comments == 1 ? 'open' : 'closed';
$user_id = $id;
$post = array(
'comment_status' => $comment_status,
'ping_status' => 'open',
'post_author' => $user_id,
'post_content' => '',
'post_status' => $status,
'post_title' => $title,
'post_type' => 'post',
);
$post_id = wp_insert_post($post);
if(isset($_FILES['video_file']) && $_FILES['video_file']['error'] == 0) {
$video_file = $tmp->saveUpload( 'video_file' );
add_post_meta($post_id, '_video_localvideo', wp_get_attachment_url($video_file['attachment_id']) );
}
else {
$video = esc_url($_POST['video']);
if (strpos($video,'youtube') !== false) {
//it's youtube, we need toget the thumbnail! :)
//we get the id of the video first
$videotemp = explode('/watch?v=', $video);
if(isset($videotemp[1]) && $videotemp[1] != '') {
$idvideo = $videotemp[1];
//we have the id, now we put the thumbnail
$url_video = 'http://i1.ytimg.com/vi/' . $idvideo . '/hqdefault.jpg';
}
}
elseif (strpos($video,'vimeo') !== false) {
//it's youtube, we need toget the thumbnail! :)
//we get the id of the video first
$videotemp = explode('vimeo.com/', $video);
if(isset($videotemp[1]) && $videotemp[1] != '') {
$idvideo = $videotemp[1];
//we have the id, now we put the thumbnail
$json_vimeo = 'http://vimeo.com/api/v2/video/' . $idvideo . '.json';
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $json_vimeo);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$json_vimeo = curl_exec($ch);
curl_close($ch);
$jsontemp = json_decode($json_vimeo, true);
if(isset($jsontemp[0]) && isset($jsontemp[0]['thumbnail_large']) && $jsontemp[0]['thumbnail_large'] != '') {
$url_video = $jsontemp[0]['thumbnail_large'];
}
}
}
if(isset($url_video) && $url_video != '') {
//we have an external youtube image :) we save it with the cURL library
$filedetails = pathinfo($url_video);
$mime = '';
if($filedetails['extension'] == 'jpe' || $filedetails['extension'] == 'jpg' || $filedetails['extension'] == 'jpeg') {
$mime = 'image/jpeg';
}
elseif($filedetails['extension'] == 'png') {
$mime = 'image/png';
}
elseif($filedetails['extension'] == 'gif') {
$mime = 'image/gif';
}
if($mime != '') {
require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
$ch = curl_init($url_video);
$uploaddir = wp_upload_dir();
$filename = $filedetails['filename'] . rand(1, 9999) . '.' . $filedetails['extension'];
//we add some random digits to make sure it's unique
$fp = fopen(trailingslashit($uploaddir['path']) . $filename, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
$guid = trailingslashit($uploaddir['url']) . $filename;
$attachment = array(
'post_mime_type' => $mime,
'guid' => $guid,
'post_title' => '[External image] : ' . $filedetails['filename'] ,
'post_content' => '',
'post_author' => $user_id,
'post_status' => 'inherit',
'post_date' => date( 'Y-m-d H:i:s' ),
'post_date_gmt' => date( 'Y-m-d H:i:s' )
);
// Add the file to the media library and generate thumbnail.
$attach_id = wp_insert_attachment( $attachment, trailingslashit($uploaddir['url']) . $filename, $post_id );
$attach_data = wp_generate_attachment_metadata( $attach_id, trailingslashit($uploaddir['path']) . $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
update_post_meta($post_id, '_thumbnail_id', $attach_id);
}
}
add_post_meta($post_id, '_video_externalvideo', $video);
}
$nsfw = isset($_POST['nsfw_video']) ? 1 : 2;
add_post_meta($post_id, '_video_nsfw', $nsfw);
add_post_meta($post_id, '_anonymous', $anonymous);
?>
and here is the image code :
case "window-image":
$format = 'image';
$title = esc_html($_POST['title_image']);
$image = esc_url($_POST['postimage']);
$tags = $_POST['tags_image'];
$comments = isset($_POST['comments_image']) ? 1 : 0;
$anonymous = isset($_POST['anonymous_image']) ? 1 : 0;
$comment_status = $comments == 1 ? 'open' : 'closed';
$user_id = $id;
if(($_FILES['image_imagepost']['error'] != 0 && $image == '') || $title == '') {
wp_redirect(home_url('/') . '?posterror=1');
exit;
}
$post = array(
'comment_status' => $comment_status,
'ping_status' => 'open',
'post_author' => $user_id,
'post_content' => '',
'post_status' => $status,
'post_title' => $title,
'post_type' => 'post',
);
$post_id = wp_insert_post($post);
if($image != '') {
$pattern = '~' . get_bloginfo('url') . '~';
if(preg_match($pattern, $image) ) {
add_post_meta($post_id, '_standard_image', $image);
}
else {
//we have an external image :) we save it with the cURL library
$filedetails = pathinfo($image);
$mime = '';
if($filedetails['extension'] == 'jpe' || $filedetails['extension'] == 'jpg' || $filedetails['extension'] == 'jpeg') {
$mime = 'image/jpeg';
}
elseif($filedetails['extension'] == 'png') {
$mime = 'image/png';
}
elseif($filedetails['extension'] == 'gif') {
$mime = 'image/gif';
}
if($mime != '') {
require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
$ch = curl_init($image);
$uploaddir = wp_upload_dir();
$filename = $filedetails['filename'] . rand(1, 9999) . '.' . $filedetails['extension'];
//we add some random digits to make sure it's unique
$fp = fopen(trailingslashit($uploaddir['path']) . $filename, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
$guid = trailingslashit($uploaddir['url']) . $filename;
$attachment = array(
'post_mime_type' => $mime,
'guid' => $guid,
'post_title' => '[External image] : ' . $filedetails['filename'] ,
'post_content' => '',
'post_author' => $user_id,
'post_status' => 'inherit',
'post_date' => date( 'Y-m-d H:i:s' ),
'post_date_gmt' => date( 'Y-m-d H:i:s' )
);
// Add the file to the media library and generate thumbnail.
$attach_id = wp_insert_attachment( $attachment, trailingslashit($uploaddir['url']) . $filename, $post_id );
$attach_data = wp_generate_attachment_metadata( $attach_id, trailingslashit($uploaddir['path']) . $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
add_post_meta($post_id, '_standard_image', trailingslashit($uploaddir['url']) . $filename);
}
}
}
else {
if($_FILES['image_imagepost']['error'] == 0) {
$image = $tmp->saveUpload( 'image_imagepost' );
add_post_meta($post_id, '_standard_image', wp_get_attachment_url($image['attachment_id']) );
}
}
$nsfw = isset($_POST['nsfw_image']) ? 1 : 2;
add_post_meta($post_id, '_standard_nsfw', $nsfw);
add_post_meta($post_id, '_anonymous', $anonymous);
break;