I used a function to add a custom upload field in the category of a custom post type.
But when i add this function I can not insert an image on an article
this is my function:
/************************************************************************************
crÈation d'un upload d'images dans les "catÈgories de pierres"
************************************************************************************/
function get_the_category_image_src($term_ID)
{
$taxonomy = 'pierre_categories';
return get_metadata($taxonomy, $term_ID, 'thumbnail', true);
}
function the_category_image($term)
{
$src = get_the_category_image_src($term->ID);
if (!empty($src)) {
echo '<img class="my-category-thumb" src="' . $src . '" />';
}
}
add_action( 'pierre_categories_edit_form', 'mycat_extra_fields' );
function mycat_extra_fields()
{
$thumbnail = get_metadata('pierre_categories', $_GET['tag_ID'], 'thumbnail', true);
include 'form.php';
}
function my_admin_scripts()
{
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
wp_register_script('my-custom-upload', get_bloginfo('template_url') . '/custom-upload.js', array('jquery', 'media-upload', 'thickbox'));
wp_enqueue_script('my-custom-upload');
}
/*
* Add thickbox
*/
function my_admin_styles()
{
wp_enqueue_style('thickbox');
}
// Add js to uoload file
add_action('admin_print_scripts', 'my_admin_scripts');
add_action('admin_print_styles', 'my_admin_styles');
add_action('edited_pierre_categories', 'mycategory_save_extra_fields' );
function mycategory_save_extra_fields( $cat_id )
{
// image URL
$url = trim($_POST['upload_url']);
if ( !empty($url) ) {
update_metadata('pierre_categories', $cat_id, 'thumbnail', $url);
} else {
delete_metadata('pierre_categories', $cat_id, 'thumbnail');
}
}
global $wpdb;
// table for custom metadata
$wpdb->pierre_categoriesmeta = $wpdb->prefix . 'pierre_categoriesmeta';
my_create_metadata_table('pierre_categories');
function my_create_metadata_table($type)
{
global $wpdb;
$table_name = $wpdb->prefix . $type . 'meta';
if (!empty ($wpdb->charset)) {
$charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
}
if (!empty ($wpdb->collate)) {
$charset_collate .= " COLLATE {$wpdb->collate}";
}
$sql = "CREATE TABLE IF NOT EXISTS {$table_name} (
meta_id bigint(20) NOT NULL AUTO_INCREMENT,
{$type}_id bigint(20) NOT NULL default 0,
meta_key varchar(255) DEFAULT NULL,
meta_value longtext DEFAULT NULL,
UNIQUE KEY meta_id (meta_id)
) {$charset_collate};";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
/************************************************************************************
Fin de la création d'upload
************************************************************************************/
I could locate the problem
when I delete the lines below, I can again insert images:
// Add js to uoload file
add_action('admin_print_scripts', 'my_admin_scripts');
add_action('admin_print_styles', 'my_admin_styles');
How can I resolve this conflict?
Sébastien | French WordpressDesigner answers:
the problem is here
// Add js to uoload file
add_action('admin_print_scripts', 'my_admin_scripts');
add_action('admin_print_styles', 'my_admin_styles');
look at that : http://wordpress.stackexchange.com/questions/81783/why-is-it-wrong-to-use-admin-print-scripts-hook-to-enqueue-a-script-js-file
Robin Ferrari comments:
I have replace that:
// Add js to uoload file
add_action('admin_print_scripts', 'my_admin_scripts');
add_action('admin_print_styles', 'my_admin_styles');
to that:
// Add js to uoload file
add_action('wp_enqueue_scripts', 'my_admin_scripts');
add_action('wp_enqueue_scripts', 'my_admin_styles');
but it's doesn't work, now i can't upload in the category of a custom post type.
Hariprasad Vijayan answers:
Hello,
Try to change following code
wp_register_script('my-custom-upload', get_bloginfo('template_url') . '/custom-upload.js', array('jquery', 'media-upload', 'thickbox'));
with
wp_register_script('my-custom-upload', get_bloginfo('template_url') . '/custom-upload.js', array('jquery'));
You can also try changing "admin_print_scripts" to "admin_enqueue_scripts" if the above code doesn't work.
Robin Ferrari comments:
I have replace that:
// Add js to uoload file
add_action('admin_print_scripts', 'my_admin_scripts');
add_action('admin_print_styles', 'my_admin_styles');
to that:
// Add js to uoload file
add_action('wp_enqueue_scripts', 'my_admin_scripts');
add_action('wp_enqueue_scripts', 'my_admin_styles');
but it's doesn't work, now i can't upload in the category of a custom post type.
Hariprasad Vijayan comments:
Hi,
can you tell where you using this custom upload?
<blockquote>I used a function to add a custom upload field in the category of a custom post type</blockquote>
didn't understand it
Robin Ferrari comments:
When i creat a new category for my custom post type "pierre" i can set an image.