Ajuda para colocar imagem upada como destaque em post na criação de plugin
Estou construindo um plugin e está dando tudo certo, consegui bons resultados na postagem de videos e com o codigo, consigo fazer o upload de uma imagem atraves de uma url, no entaanto não estou conseguindo ligar a imagem upada ao post que é criado com o envio do formulário. Gostaria muito que me ajudassem. Obrigado.
Segue o código do plugin.
<h1>Vídeos ---ôs PRO</h1>
<!-- New Post Form -->
<div id="postbox">
<form id="new_post" name="new_post" method="post" action="">
<label for="title">Titulo do video</label>
<input type="text" id="title" value="" tabindex="1" size="40" name="title" />
<label for="description">Descrição do Video</label>
<textarea id="description" tabindex="3" name="description" cols="50" rows="6"></textarea>
<label for="site">Site</label>
<select id="site" name="site" size="1">
<option selected="selected">Selecione o site</option>
<option value="xvideos">xvideos</option>
<option value="redtube">redtube</option>
<option value="xhamster">xhamster</option>
<option value="---hub">---hub</option>
<option value="---ocarioca">---ocarioca</option>
<option value="you---">you---</option>
<option value="tube8">tube8</option>
<option value="xtube">xtube</option>
</select>
<label for="categoria">Categoria da postagem</label>
<?php wp_dropdown_categories( 'show_option_none=Slecione a categoria&tab_index=4&taxonomy=category' ); ?>
<label for="url">URL do video</label>
<input type="text" id="url" value="" tabindex="1" size="40" name="url" />
<label for="post_tags">Tags</label>
<input type="text" value="" tabindex="5" size="16" name="post_tags" id="post_tags" />
<label for="url">Status do Video</label>
<input id="status" name="status" type="radio" value="draft" checked="checked">Deixar como Rascunho
<input id="status" name="status" type="radio" value="publish"> Publicar Agora
<br>
<p align="left"><input type="submit" value="Enviar" tabindex="6" id="submit" name="submit" size="10" />
<input type="hidden" name="post_type" id="post_type" value="post" />
<input type="hidden" name="action" value="post" />
<?php wp_nonce_field( 'new-post' ); ?>
</form>
</div><!--// New Post Form -->
<?php
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] )) {
// Do some minor form validation to make sure there is content
if (isset ($_POST['title'])) {
$title = $_POST['title'];
} else {
echo 'Por favor preencha o título!';
}
if (isset ($_POST['description'])) {
$description = $_POST['description'];
} else {
echo 'Please enter the content';
}
if (isset ($_POST['site'])) {
$site = $_POST['site'];
if (isset ($_POST['cat'])) {
$cat = $_POST['cat'];
} else {
echo 'Por favor selecione a categoria!';
}
if (isset ($_POST['url'])) {
//inicio condição
if ( $site == "xvideos" ) {
$src = $_POST['url'];
$video = substr(@parse_url($src, PHP_URL_PATH), 6);
$video_id = strtok($video, '/');//inicio da captura e envio da imagemao servidor
$endereco = file_get_contents($src);
$pegar = preg_match_all('/<meta property="og:image" content=(.+)\/>/', $endereco, $conteudo);$image_s1 = $conteudo[1][0]; // Define the image URL here
$image_s2 = substr($image_s1, 1);
$image_s3 = strtok($image_s2, '"');
$image_url = $image_s3;
$image_name = $video_id.'.jpg';$upload_dir = wp_upload_dir(); // Set upload folder
$image_data = file_get_contents($image_url); // Get image data
$unique_file_name = wp_unique_filename( $upload_dir['path'], $image_name ); // Generate unique name
$filename = basename( $unique_file_name ); // Create image file name
$file_path = get_site_url() . '/wp-content/uploads/' . $image_name; // Get the futur file path of the image
if( !file_exists($file_path) ){ // Check if image is already in upload_dir
// Check folder permission and define file location
if( wp_mkdir_p( $upload_dir['path'] ) ) {
$file = $upload_dir['path'] . '/' . $filename;
} else {
$file = $upload_dir['basedir'] . '/' . $filename;
}// Create the image file on the server
file_put_contents( $file, $image_data );
// Check image file type
$wp_filetype = wp_check_filetype( $filename, null );
// Set attachment data
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name( $filename ),
'post_content' => '',
'post_status' => 'inherit'
);
// Create the attachment
$attach_id = wp_insert_attachment( $attachment, $file, $post_id );
// Include image.php
require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');// Define attachment metadata
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
// Assign metadata to attachment
wp_update_attachment_metadata( $attach_id, $attach_data );
// And finally assign featured image to post
set_post_thumbnail( $post_id, $attach_id );
}
}
elseif ( $site == "redtube" ) {
$src = $_POST['url'];
$video_id = (int) substr(@parse_url($src, PHP_URL_PATH), 1);
}
elseif ( $site == "xhamster" ) {
$src = $_POST['url'];
$video = substr(@parse_url($src, PHP_URL_PATH), 8);
$video_id = strtok($video, '/');
}
elseif ( $site == "---hub" ) {
$src = $_POST['url'];
$posicao = strpos($src, '=');
$video_id = substr($src, $posicao+1);
}
elseif ( $site == "---ocarioca" ) {
$src = $_POST['url'];
$posicao = strpos($src, 'video/');
$video = substr($src, $posicao+6);
$video_id = strtok($video, '"');
}
elseif ( $site == "your---" ) {
$src = $_POST['url'];
$video = substr(@parse_url($src, PHP_URL_PATH), 7);
$video_id = strtok($video, '/');
}
elseif ( $site == "tube8" ) {
$src = $_POST['url'];
$video_id = substr(@parse_url($src, PHP_URL_PATH), 1);
}
elseif ( $site == "xtube" ) {
$src = $_POST['url'];
$video_id = substr(@parse_url($src, PHP_URL_PATH), 13);
}
//fim condição
//$src = $_POST['url'];
//$video = substr(@parse_url($src, PHP_URL_PATH), 6);
//$video_id = strtok($video, '/');
} else {
echo 'Por favor preencha o endereço do video!';
}
}
$tags = $_POST['post_tags'];
if (isset ($_POST['status'])) {
$status = $_POST['status'];
} else {
echo 'Por favor selecione a categoria!';
}
// Add the content of the form to $post as an array
$post = array(
'post_title' => $title,
'post_content' => ''.$description.'[videoin site='.$site.' width=660 height=460 id='.$video_id.'][/videoin]
<br><br>O melhor conteúdo ---ô você encontra aqui. As melhores fotos e os videos mais picantes. Atualizações diárias!',
'post_category' => array($_POST['cat']), // Usable for custom taxonomies too
'tags_input' => $tags,
'post_status' => $status, // Choose: publish, preview, future, etc.
'post_type' => $_POST['post_type'] // Use a custom post type if you want to
);
wp_insert_post($post); // Pass the value of $post to WordPress the insert function
// http://codex.wordpress.org/Function_Reference/wp_insert_post
wp_redirect( home_url() );
} // end IF
// Do the wp_insert_post action to insert it
do_action('wp_insert_post', 'wp_insert_post');
?>Discussão (0)
Carregando comentários...