我正在使用wordpress的前端表单来创建自定义帖子类型并上传附加到该帖子的图片。
以下是我的代码。帖子已创建,但问题是图片未上传到附加到该帖子的服务器。另一方面,我希望在发送表单之前预先显示图像,就像wordpress适用于管理区域一样。
public class ProfileActivity extends AppCompatActivity implements View.OnClickListener {
public static final String JSON_URL = "/*I ommitted my URL on purpose here*/";
private TextView username_txt;
private Button fetch_data_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
username_txt = (TextView)findViewById(R.id.txt_username);
fetch_data_btn = (Button)findViewById(R.id.btn_fetch_data);
SharedPreferences sharedPreferences = getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);
String username = sharedPreferences.getString(Config.USERNAME_SHARED_PREF,"Not Available");
username_txt.setText(username);
sendRequest();
fetch_data_btn.setOnClickListener(this);
}
private void sendRequest(){
final ProgressDialog loading = ProgressDialog.show(this,"Fetching data.","Please wait...", false,false);
StringRequest stringRequest = new StringRequest(Request.Method.GET,JSON_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println(response.substring(0));
showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println("Something went wrong!");
Toast.makeText(ProfileActivity.this, "Something went wrong!", Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String json){
ParseWorkOrderJSON pwoj = new ParseWorkOrderJSON();
pwoj.parseWorkOrderJSON(json);
}
@Override
public void onClick(View v){
sendRequest();
Intent intent = new Intent(ProfileActivity.this, MytabActivity.class);
startActivity(intent);
}
}
形式:
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['new_cpt_action'] ) && $_POST['new_cpt_action'] == "new_cpt" && isset( $_POST['new_cpt-form'] ) && wp_verify_nonce( $_POST['new_cpt-form'], 'create_new_cpt' ) ){
if (isset($_POST['submit'])) {
$error = "";
if (!empty($_POST['s_name'])) {
$s_name = $_POST['s_name'];
} else {
$error = 'Please insert a name';
}
if (empty($error)) {
$new_post = array(
'post_title' => $s_name,
'post_status' => 'pending',
'post_type' => 'my_cpt',
);
//Save the post
$post_id = wp_insert_post($new_post);
if( ! empty( $_FILES ) ) {
foreach( $_FILES as $file ) {
if( is_array( $file ) ) {
$attachment_id = wp_insert_attachment($file, $post_id);
}
}
}
//Redirect to the new post
if ( $post_id ) {
wp_redirect(home_url('site/'.$post_id));
exit;
}
}
}
如果可以,请告诉我我错过的内容会很棒。 感谢
答案 0 :(得分:0)
我设法使用以下代码将附件包含在wp媒体库中:
if ( $_FILES ) {
$files = $_FILES["file"];
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 ("file" => $file);
foreach ($_FILES as $file => $array) {
$newupload = frontend_handle_attachment( $file, $post_id );
}
}
}
}
调用函数frontend_handle_attachment:
function frontend_handle_attachment($file_handler,$post_id) {
// check to make sure its a successful upload
if ($_FILES[$file_handler]['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');
$attach_id = media_handle_upload( $file_handler, $post_id );
// Set featured image
set_post_thumbnail($post_id, $attach_id);
return $attach_id;
}
这可以通过以下形式的简单输入正常工作:
<input type="file" name="file[]" multiple="multiple" />
至于图像的进度条和预可视化,我试图实现dropzone,但到目前为止还没有运气。至少这种简单的方法是有效的。