رفتن به مطلب

مشکل در اجرای کد ارسال مطلب از قالب


پست های پیشنهاد شده

سلام

من کد زیر رو برای ارسال مطلب به سایت که بشه از قالب اینکار رو انجام داد ، درست کردم .


<form action="" id="primaryPostForm" enctype="multipart/form-data" method="POST">
<input type="text" name="postTitle" class="titleiqwkaksdqw" placeholder="یک عنوان" id="postTitle" value="<?php if(isset($_POST['postTitle'])) echo $_POST['postTitle'];?>" class="required" />
<?php if($postTitleError != '') { ?>
<span class="error"><?php echo $postTitleError; ?></span>
<div class="clearfix"></div>
<?php } ?>
<div class="clear"></div>
<textarea class="tetopsdlkxcmmm" placeholder="پاراگراف مورد علاقه ات رو بنویس ..." name="postContent" id="postContent" rows="8" cols="30"><?php if(isset($_POST['postContent'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['postContent']); } else { echo $_POST['postContent']; } } ?></textarea>
<div class="clear"></div>
<input type="file" name="kkwiqfileswoewqe"/>
<div class="clear"></div>
<?php wp_nonce_field('post_nonce', 'post_nonce_field'); ?>
<input type="hidden" name="submitted" id="submitted" value="true" />
<button class="sendpotsmiddenrweqwe" type="submit">انتشار</button>
</form>
<?php
set_time_limit(0);
define('WP_USE_THEMES', true);
require('./wp-blog-header.php');
$body = 'Content';
$image_url = $_FILES['kkwiqfileswoewqe']['name'];
$post = array(
'post_title' => esc_attr(strip_tags($_POST['postTitle'])),
'post_content' => esc_attr(strip_tags($_POST['postContent'])),
'post_author' => 1,
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_type' => 'post'
//'post_date' => [ Y-m-d H:i:s ] //The time post was made.
//'ID' => [ <post id> ] //Are you updating an existing post?
//'post_category' => [ array(<category id>, <...>) ] //Add some categories.
//'post_date_gmt' => [ Y-m-d H:i:s ] //The time post was made, in GMT.
//'post_excerpt' => [ <an excerpt> ] //For all your post excerpt needs.
//'post_name' => [ <the name> ] // The name (slug) for your post
//'post_parent' => [ <post ID> ] //Sets the parent of the new post.
//'post_password' => [ ? ] //password for post?
//'tags_input' => [ '<tag>, <tag>, <...>' ] //For tags.
//'to_ping' => [ ? ] //?
);
$post_id = wp_insert_post( $post, $wp_error );
$upload_dir = wp_upload_dir();
$image_data = file_get_contents($image_url);
$filename = basename($image_url);
if(wp_mkdir_p($upload_dir['path']))
$file = $upload_dir['path'] . '/' . $filename;
else
$file = $upload_dir['basedir'] . '/' . $filename;
file_put_contents($file, $image_data);
$wp_filetype = wp_check_filetype(basename($filename), null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($filename),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $image_url, $post_id );
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $image_url );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $post_id, $attach_id );
?>

ولی یه مشکلی داره ..

در خط 21 یعنی = image_url$ وقتی مقدارش رو برابر آدرس یک تصویر قرار میدم ، میاد اون تصویر رو خیلی راحت به جای تصویر شاخص قرار میده .

ولی طبق کد بالا هرکاری میکنم که بشه تصویر رو از اینپوت فایل بخونه ، نمیشه و ارور میده .

لطفا اگه میشه غلط کد من رو بگیرید ، نزدیک 6 ساعت دارم روی کد کار میکنم ولی هنوز مشکلم حل نشده .

ممنون ..

ویرایش شده توسط alitna
لینک به ارسال

کد رو به کد زیر تغییر دادم :


<form action="" id="primaryPostForm" enctype="multipart/form-data" method="POST">
<input type="text" name="postTitle" class="titleiqwkaksdqw" placeholder="یک عنوان" id="postTitle" value="<?php if(isset($_POST['postTitle'])) echo $_POST['postTitle'];?>" class="required" />
<?php if($postTitleError != '') { ?>
<span class="error"><?php echo $postTitleError; ?></span>
<div class="clearfix"></div>
<?php } ?>
<div class="clear"></div>
<textarea class="tetopsdlkxcmmm" placeholder="پاراگراف مورد علاقه ات رو بنویس ..." name="postContent" id="postContent" rows="8" cols="30"><?php if(isset($_POST['postContent'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['postContent']); } else { echo $_POST['postContent']; } } ?></textarea>
<div class="clear"></div>
<input type="file" name="kkwiqfileswoewqe"/>
<div class="clear"></div>
<?php wp_nonce_field('post_nonce', 'post_nonce_field'); ?>
<input type="hidden" name="submitted" id="submitted" value="true" />
<button class="sendpotsmiddenrweqwe" type="submit">انتشار</button>
</form>
<?php


set_time_limit(0);
define('WP_USE_THEMES', true);
require('./wp-blog-header.php');
$body = 'Content';
$image_url = $_FILES['kkwiqfileswoewqe']['name'];
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;
}
$post = array(
'post_title' => esc_attr(strip_tags($_POST['postTitle'])),
'post_content' => esc_attr(strip_tags($_POST['postContent'])),
'post_author' => 1,
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_type' => 'post'
//'post_date' => [ Y-m-d H:i:s ] //The time post was made.
//'ID' => [ <post id> ] //Are you updating an existing post?
//'post_category' => [ array(<category id>, <...>) ] //Add some categories.
//'post_date_gmt' => [ Y-m-d H:i:s ] //The time post was made, in GMT.
//'post_excerpt' => [ <an excerpt> ] //For all your post excerpt needs.
//'post_name' => [ <the name> ] // The name (slug) for your post
//'post_parent' => [ <post ID> ] //Sets the parent of the new post.
//'post_password' => [ ? ] //password for post?
//'tags_input' => [ '<tag>, <tag>, <...>' ] //For tags.
//'to_ping' => [ ? ] //?
);
$post_id = wp_insert_post( $post, $wp_error );
$upload_dir = wp_upload_dir();
// $image_data = file_get_contents($image_url);
$filename = basename($image_url);
if(wp_mkdir_p($upload_dir['path']))
$file = $upload_dir['path'] . '/' . $filename;
else
$file = $upload_dir['basedir'] . '/' . $filename;
// file_put_contents($file, $image_data);
$wp_filetype = wp_check_filetype(basename($filename), null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($filename),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $image_url, $post_id );
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $image_url );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $post_id, $attach_id );
?>

نام فایل و لینکش رو جای تصویر شاخص مطلب قرار میده ، ولی آپلود نمیکنه ...

لینک به ارسال

در کد بالا دستوری برای آپلود فایل داده نمیشه

تابع آپلود وردپرس wp_upload_bits هست

نمونه

http://www.htmlcenter.com/blog/wordpress-import-images-from-another-website/

لینک به ارسال

سلام . ممنون از پاسختون

تابع رو بررسی کردم و امتحان کردم ولی بازم جواب نداد .

دقیقا مثل همین :

http://codex.wordpress.org/Function_Reference/wp_upload_bits

در مقدار wp_upload_bits از کد دوم که دادم ، چه متغییر هایی رو باید به عنوان تابع ورودی بهش بدم ؟

لینک به ارسال

یک نمونه:


$upload = wp_upload_bits($_FILES["usp_post_file"]["name"], null, file_get_contents($_FILES["usp_post_file"]["tmp_name"]));
if(empty($upload['error'])){
$filename = $upload['file'];
$wp_filetype = wp_check_filetype(basename($filename), null );
$wp_upload_dir = wp_upload_dir();
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
'post_content' => '',
'post_status' => 'inherit'
);
$attachment_id = wp_insert_attachment( $attachment, $filename, 1423 );
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
wp_update_attachment_metadata( $attachment_id, $attachment_data );
}else{
$img_out .= "خطا در آپلود پیوست: ".$upload['error']." <br />";
}

لینک به ارسال

سلام

تشکر فراوان

درست شد ..

کد زیر رو :


$image_url = $_FILES['kkwiqfileswoewqe']['name'];

به این :


$upload = wp_upload_bits($_FILES["kkwiqfileswoewqe"]["name"], null, file_get_contents($_FILES["kkwiqfileswoewqe"]["tmp_name"]));
$image_url = $upload['file'];

تبدیل کردم ، درست شد ..

لینک به ارسال

به گفتگو بپیوندید

هم اکنون می توانید مطلب خود را ارسال نمایید و بعداً ثبت نام کنید. اگر حساب کاربری دارید، برای ارسال با حساب کاربری خود اکنون وارد شوید .

مهمان
ارسال پاسخ به این موضوع ...

×   شما در حال چسباندن محتوایی با قالب بندی هستید.   حذف قالب بندی

  تنها استفاده از 75 اموجی مجاز می باشد.

×   لینک شما به صورت اتوماتیک جای گذاری شد.   نمایش به صورت لینک

×   محتوای قبلی شما بازگردانی شد.   پاک کردن محتوای ویرایشگر

×   شما مستقیما نمی توانید تصویر خود را قرار دهید. یا آن را اینجا بارگذاری کنید یا از یک URL قرار دهید.

×
×
  • اضافه کردن...