Skip to content

Instantly share code, notes, and snippets.

@tcelestino
Last active December 15, 2015 23:49
Show Gist options
  • Save tcelestino/5343212 to your computer and use it in GitHub Desktop.
Save tcelestino/5343212 to your computer and use it in GitHub Desktop.
Criação de um post no WordPress no front-end com upload de imagem.
<?php
// adiciona o post ao WordPress
add_action('wp_ajax_nopriv_post_insert', 'post_insert');
add_action('wp_ajax_post_insert', 'post_insert');
function post_insert(){
require_once(ABSPATH . 'wp-admin/includes/image.php');
if('POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] )) {
$avatar = trim(wp_strip_all_tags($_POST["avatar"]));
$username = trim(wp_strip_all_tags($_POST["username"]));
$name = trim(wp_strip_all_tags($_POST["name"]));
$email = trim(wp_strip_all_tags($_POST["email"]));
$title = trim(wp_strip_all_tags($_POST['title']));
$content = nl2br(trim(wp_strip_all_tags($_POST['content'])));
$media = trim(wp_strip_all_tags(!isset($_POST["media"]))) ? NULL : $_POST["media"];
$photo = trim(wp_strip_all_tags(!isset($_POST["photo"]))) ? NULL : base64_decode($_POST["photo"]);
$return = array(); // aqui que faco da putaria
// trato e vejo se os campos (titulo / content) estao vazio
if(empty($title)) {
$return["status"] = 0;
$return["message"] = "Digite o titulo companheiro";
} elseif (empty($content)) {
$return["status"] = 0;
$return["message"] = "Digite um texto.";
} elseif(empty($name)) {
$return["status"] = 0;
$return["message"] = "Digite seu nome";
} elseif(empty($email)) {
$return["status"] = 0;
$return["message"] = "Digite seu email";
} elseif(!preg_match("/^[A-Za-z0-9]+([_.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_.-][A-Za-z0-9]+)*\\.[A-Za-z0-9]{2,4}$/", $email)) {
$return["status"] = 0;
$return["message"] = "Digite um email válido";
}else {
// verifico se o usuario ja existe atraves do username do facebook
// caso ele já existe, adiciona o post com o id do usuario já cadastrado
if(username_exists($username)) {
$user = get_user_by("login", $username);
$post = array(
"post_title" => $title,
"post_content" => $content,
"post_status" => "draft",
"post_author" => $user->ID
);
} elseif(email_exists($email)) {
$user = get_user_by("email", $email);
$post = array(
"post_title" => $title,
"post_content" => $content,
"post_status" => "draft",
"post_author" => $user->ID
);
} else {
$user = save_user($avatar, $username, $name, $email);
$post = array(
"post_title" => $title,
"post_content" => $content,
"post_status" => "draft",
"post_author" => $user
);
}
$post_id = wp_insert_post($post);
if(!empty($photo)) {
$wp_filetype = wp_check_filetype(basename($photo),null );
$wp_upload_dir = wp_upload_dir();
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename( $photo ) ),
'post_content' => '',
'post_status' => 'inherit',
'guid' => $wp_upload_dir['baseurl'] . _wp_relative_upload_path($photo)
);
$attachment_id = wp_insert_attachment($attachment, $photo, $post_id);
if($attachment_id) {
$attach_data = wp_generate_attachment_metadata( $attachment_id, $photo );
//wp_update_attachment_metadata( $attachment_id, $attach_data );
set_post_thumbnail( $post_id, $attachment_id ); // seta a imagem enviada como imagem destacada
} else {
$return["status"] = 0;
$return["message"] = "Erro ao tentar enviar sua imagem.";
}
}
if($post_id) {
if($media) {
add_post_meta($post_id, "_youtube_url", $media, true);
} else {
$return["status"] = 0;
$return["message"] = "Erro ao tentar enviar seu vídeo.";
}
} else {
$return["status"] = 0;
$return["message"] = "Erro ao tentar enviar seu post.";
}
$return["status"] = 1;
$return["message"] = "Sua ideia foi enviada com sucesso. Em breve, estará no ar.";
}
$output = json_encode($return);
if(is_array($output)){
print_r($output);
}
else{
echo $output;
}
die;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment