Значит есть у меня госолование в записи, тама можно создавать вопросы и загружать картинки, но как обычно картинки загружаются /uploads/2012/08/1-150×150.jpg
как мне сделать так чтобы картинки загружадлись в /uploads/vote/ а название картинки соответствоволо короткой ссылки статьи типо если статья краткая ссылка http: // …/?p=725 то картинка заливалась бы так /uploads/vote/750p-1-150×150.jpg или 750p-1.jpg,
вот код который у меня есть походу он отвчает за все это
[code- <?php
function sv_add_meta_box()
{
add_meta_box("sv_votes_workplace", "Голосование", "sv_votes_workplace", "post", "normal", "low");
}
add_action(‘add_meta_boxes’, ‘sv_add_meta_box’);
function sv_votes_workplace($post)
{
global $post;
wp_nonce_field(plugin_basename( __FILE__ ), ‘sv_noncename’);
$output = ‘<div id="sv_wrapper"></div>’;
$output .= ‘<input type="hidden" value="’ . time() . ‘" name="sv_time" />’; // костыль
$output .= PHP_EOL . ‘<script type="text/javascript">’ . PHP_EOL;
$data = get_post_meta($post->ID, "sv_content", true);
if($data)
{
$data = unserialize($data);
$data = $data[0];
}
if(is_array($data) && count($data))
{
$output .= ‘var sv_old = [‘ . PHP_EOL;
foreach($data as $key => $unit)
{
$name = str_replace(‘\”, ‘"’, $unit[‘name’]);
$descr = str_replace(‘\”, ‘"’, $unit[‘description’]);
$image = str_replace(‘\”, ‘"’, wp_get_attachment_url($unit[‘image’]));
$id = $key;
$output .= "[‘$name’, ‘$descr’, ‘$image’, ‘$id’]," . PHP_EOL;
}
$output .= ‘];’ . PHP_EOL;
}
$output .= ‘</script>’;
echo $output;
}
function post_edit_form_tag() {
echo ‘ enctype="multipart/form-data"’;
}
add_action(‘post_edit_form_tag’, ‘post_edit_form_tag’);
function sv_save($post_id)
{
global $post;
if(defined(‘DOING_AUTOSAVE’) && DOING_AUTOSAVE)
return;
if (!isset($_POST[‘sv_noncename’])
|| !wp_verify_nonce($_POST[‘sv_noncename’], plugin_basename( __FILE__ )))
return;
if (‘page’ == $_POST[‘post_type’])
{
if(!current_user_can(‘edit_page’, $post->ID))
return;
}
else
{
if(!current_user_can(‘edit_post’, $post->ID))
return;
}
$time = $_POST[‘sv_time’];
$data = array();
$old_data = get_post_meta($post->ID, "sv_content", true);
if($old_data)
{
$old_data = unserialize($old_data);
if($old_data[1] == $time) return; // костыль
$old_data = $old_data[0];
}
if(!$old_data) $old_data = array();
if(!isset($_POST[‘sv_name’])) return;
$files = $_FILES[‘sv_image’];
foreach($_POST[‘sv_name’] as $key => $name)
{
$id = (is_numeric($_POST[‘sv_id’][$key])) ? intval($_POST[‘sv_id’][$key]) : -1;
$prev = (isset($old_data[$id])) ? $old_data[$id] : null;
$name = trim(strip_tags($name));
if(!$name)
{
if($prev) wp_delete_attachment($prev[‘image’]);
continue;
}
if(!$files[‘name’][$key] && !$prev) continue;
$image_id = null;
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]
);
$upload = wp_handle_upload($file, array(‘test_form’ => false));
if(!isset($upload[‘error’]) && isset($upload[‘file’]))
{
$mimes = array(
‘jpg|jpeg|jpe’ => ‘image/jpeg’,
‘gif’ => ‘image/gif’,
‘png’ => ‘image/png’
);
$filetype = wp_check_filetype(basename($upload[‘file’]), $mimes);
if($filetype[‘type’])
{
$title = $file[‘name’];
$ext = strrchr($title, ‘.’);
$title = ($ext !== false) ? substr($title, 0, -strlen($ext)) : $title;
$attachment = array(
‘post_mime_type’ => $filetype[‘type’],
‘post_title’ => addslashes($title),
‘post_content’ => ”,
‘post_status’ => ‘inherit’,
‘post_parent’ => $post->ID
);
$image_id = wp_insert_attachment($attachment, $upload[‘file’]);
$attach_data = wp_generate_attachment_metadata( $image_id, $upload[‘file’] );
wp_update_attachment_metadata( $image_id, $attach_data );
if($prev) wp_delete_attachment($prev[‘image’]);
}
else if($prev) $image_id = $prev[‘image’];
else continue;
}
else if($prev) $image_id = $prev[‘image’];
else continue;
}
else $image_id = $prev[‘image’];
$data
= array(
‘name’ => $name,
‘description’ => trim(strip_tags($_POST[‘sv_description’][$key])),
‘image’ => $image_id
);
}
update_post_meta($post->ID, "sv_content", serialize(array($data, $time)));
}
add_action(‘save_post’, ‘sv_save’);
function sv_scripts_main()
{
wp_enqueue_style(‘sv_main_css’, plugins_url(‘wp-social-votes/css/sv_main.css’), false, ‘1.0’, ‘all’);
//wp_register_script(‘openapi’, plugins_url(‘wp-social-votes/openapi.js’));
wp_enqueue_script(‘sv_main3_js’, plugins_url(‘wp-social-votes/js/jquery-ui-1.8.20.custom.min.js’), ‘1.0’); // sV: nE
wp_enqueue_script(‘sv_main_js’, plugins_url(‘wp-social-votes/js/sv_main.js’), ‘1.0’); // sV: nE
//wp_localize_script(‘sv_js’, ‘sv_loc_data’, array(‘ajax_url’ => plugins_url(‘wp-polls/wp-polls.php’)));
wp_register_style(‘jquery-style’, plugins_url(‘wp-social-votes/css/ui-lightness/jquery-ui-1.8.20.custom.css’), true); // sV: nE
wp_enqueue_style(‘jquery-style’);
}
add_action(‘wp_enqueue_scripts’, ‘sv_scripts_main’);
function sv_scripts_admin()
{
wp_enqueue_style(‘sv_admin_css’, plugins_url(‘wp-social-votes/css/sv_admin.css’), false, ‘1.0’, ‘all’);
wp_enqueue_script(‘sv_admin_js’, plugins_url(‘wp-social-votes/js/sv_admin.js’), array(‘jquery’), ‘1.0’);
}
add_action(‘admin_enqueue_scripts’, ‘sv_scripts_admin’);
function sv_show_vote()
{
global $post;
$data = get_post_meta($post->ID, "sv_content", true);
if($data)
{
$data = unserialize($data);
$data = $data[0];
}
if(is_array($data) && count($data))
{
echo ‘<div align="center">’;
foreach($data as $key => $unit)
{
$name = $unit[‘name’];
$description = $unit[‘description’];
$data[$key][‘image’] = wp_get_attachment_image($unit[‘image’], ‘sV’, 0, array(
‘alt’ => $unit[‘name’], ‘title’ => $unit[‘name’]
));
$image = $data[$key][‘image’];
include plugin_dir_path(__FILE__) . ‘/tpl/unit.php’;
}
echo ‘</div>’;
$data[‘count’] = count($data);
$link = get_permalink($post->ID);
if(substr($link, -1) != ‘/’) $link .= ‘/’;
$data[‘link’] = $link;
echo ‘<div style="clear: both;"></div>’;
echo ‘<script type="text/javascript">’ .
‘var sv_data = ‘ . json_encode($data) . ‘;’ .
‘</script>’;
include plugin_dir_path(__FILE__) . ‘/tpl/add.php’;
}
}
/**
sV: new Era @ 05/07/2012 04:29 GMT
*/
function sV_thumbnails() {
if(!current_theme_supports(‘post-thumbnails’)) {
add_theme_support(‘post-thumbnails’);
}
add_image_size( ‘sV’, 150, 150, 1);
}
add_action(‘init’, ‘sv_thumbnails’);
function sV_shortcode ($atts) {
ob_start();
sv_show_vote();
$output = ob_get_contents();
ob_end_clean();
return $output;
}
add_shortcode("social_votes", "sV_shortcode");[/code]
и хотелось бы чтобы вордпрес не создавал дублекаты с разными разрешениями, а только какоето определенное 150х150