WordPress.org

Support

Support » Plugins and Hacks » [Resolved] How to add tags ?

[Resolved] How to add tags ?

Viewing 15 replies - 1 through 15 (of 16 total)
  • Plugin Author Rinat Khaziev

    @rinatkhaziev

    Hey,

    Thanks for the kind words. Currently there’s no easy way to add tags to uploaded media (unless you’re ready to write some PHP code), although I plan this feature for v0.6.

    Hi Rinat,

    I’am ready to add PHP Code. In fact I already add tags support for attachment with a bit of code :

    add_action(‘init’, ‘create_image_taxonomies’);

    function create_image_taxonomies() {
    $labels = array(
    ‘name’ => ‘Media Category’
    );

    $args = array(
    ‘labels’ => $labels,
    ‘public’ => true
    );

    register_taxonomy(‘imagetype’, ‘attachment’, $args);
    }

    // Add a new column
    add_filter(‘manage_media_columns’, ‘add_topic_column’);
    function add_topic_column($posts_columns) {
    $posts_columns[‘att_topic’] = _x(‘Topic’, ‘column name’);
    return $posts_columns;
    }

    // Register the column as sortable
    function topic_column_register_sortable( $columns ) {
    $columns[‘att_topic’] = ‘att_topic’;
    return $columns;
    }
    add_filter( ‘manage_upload_sortable_columns’, ‘topic_column_register_sortable’ );

    add_action(‘manage_media_custom_column’, ‘manage_attachment_topic_column’, 10, 2);
    function manage_attachment_topic_column($column_name, $id) {
    switch($column_name) {
    case ‘att_topic’:
    $tagparent = “upload.php?”;
    $tags = wp_get_object_terms( $id, ‘taxonomy_name’, ” );
    if ( !empty( $tags ) ) {
    $out = array();
    foreach ( $tags as $c )
    $out[] = “slug’> ” . esc_html(sanitize_term_field(‘name’
    , $c->name, $c->term_id, ‘post_tag’, ‘display’)) . “
    “;
    echo join( ‘, ‘, $out );
    } else {
    _e(‘No Topics’);
    }
    break;
    default:
    break;
    }
    }

    I works great in admin side, but I wonder how to make it works with Frontend Uploader…

    Plugin Author Rinat Khaziev

    @rinatkhaziev

    Try something like this. It’s untested but the whole idea is to attach an action to the plugin’s custom hook that passes array of successfully uploaded attachments.

    From there, you’ll be able to set terms programmatically.

    <?php
    add_action( 'fu_after_upload', 'my_fu_after_upload', 10, 2 );
    // Custom action to do any additional logic after attachment is uploaded
    function my_fu_after_upload( $media_ids, $success ) {
    
    	// Iterate array with attachment ids
    	foreach( (array) $media_ids as $media_id ) {
    
    		// Term is just an example, all $_POST variables should be available to you
    		$term = sanitize_text_field( $_POST['my_custom_term'] );
    
    		wp_set_object_terms( $media_id, $term, 'imagetype', true );
    	}
    }

    Thank you Rinat.

    I get the whole idea… but I am not sure to know what “my_custom_term” is related to.
    Is it an input name or my taxonomy name ?

    In fact, I have added an input like this to handle tags.

    [input type=”text” name=”tags” id=”ug_tags” description=”Mot-clef” multiple=””]

    I don’t know how to link it to the code you just posted above.

    Plugin Author Rinat Khaziev

    @rinatkhaziev

    It should be input name from your upload form.

    Check out the FAQ if you haven’t yet:
    http://wordpress.org/plugins/frontend-uploader/faq/

    It doesn’t seem to work.

    The code is in frontend-uploader.php, here is the input

    [input type="text" name="post_tags" id="ug_tags" description="Mot-clef" multiple=""]

    and the function

    // Custom action to do any additional logic after attachment is uploaded
    	function my_fu_after_upload( $media_ids, $success ) {
    		// Iterate array with attachment ids
    		foreach( (array) $media_ids as $media_id ) {
    
    			// Term is just an example, all $_POST variables should be available to you
    			$term = sanitize_text_field( $_POST['post_tags'] );
    
    			wp_set_object_terms( $media_id, $term, 'imagetype', true );
    		}
    	}
    add_action( 'fu_after_upload', 'my_fu_after_upload', 10, 2 ); is in function __construct().

    [Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum’s parser.]

    I have no error message, but I cannot retrieve any tag.

    Plugin Author Rinat Khaziev

    @rinatkhaziev

    Oh yeah, there’s a bug, sorry 🙂

    For now, you can just move all the hooks definition except add_action( 'init', array( $this, 'action_init' ) ); from __construct into action_init method in the plugins file. That should do.

    I already fixed this issue but did not release new version yet.

    Is this correct ?

    Plugin Author Rinat Khaziev

    @rinatkhaziev

    You should put your custom hook part into your theme’s functions.php. E.g.

    Move add_action( 'fu_after_upload', 'my_fu_after_upload', 10, 2 ); and the function itself. Otherwise you’ll lose it with the next update of the plugin.

    I moved action_init and all hooks in function.php.
    Got no errors, but I can’t retrieve the tag from the upload form.

    Do you have any idea ?

    Hey Rinat,

    After cleaning function.php it works like a charm !
    Thanks a lot for your help !

    If anyone want to do the same thing, here is how to do.

    In function.php add the following:

    // Custom action to do any additional logic after attachment is uploaded
    	function my_fu_after_upload( $media_ids, $success ) {
    		// Iterate array with attachment ids
    		foreach( (array) $media_ids as $media_id ) {
    
    			// Term is just an example, all $_POST variables should be available to you
    			$term = sanitize_text_field( $_POST['post_tags'] );
    
    			wp_set_object_terms( $media_id, $term, 'imagetype', true );
    		}
    	}
    add_action( 'fu_after_upload', 'my_fu_after_upload', 10, 2 );

    Then in the form, add an input:
    [input type="text" name="post_tags" id="ug_tags" description="Mot-clef" multiple=""]

    Be careful, $term = sanitize_text_field( $_POST[‘post_tags‘] ); where post_tags is your input’s name 😉

    function.php in plugins/frontend-uploader/lib/php/ ???

    Please, I need help…

    Hello shnalla,

    I really love this idea but I am a little confused.

    After adding the code you suggested into my theme’s function.php, it won’t give me an error but it also doesn’t attach any Tags. Any help would be much appreciated.

    Code in functions.php:

    <?php
    // Custom action to do any additional logic after attachment is uploaded
    	function my_fu_after_upload( $media_ids, $success ) {
    		// Iterate array with attachment ids
    		foreach( (array) $media_ids as $media_id ) {
    
    			// Term is just an example, all $_POST variables should be available to you
    			$term = sanitize_text_field( $_POST['post_tags'] );
    
    			wp_set_object_terms( $media_id, $term, 'imagetype', true );
    		}
    	}
    add_action( 'fu_after_upload', 'my_fu_after_upload', 10, 2 );
    ?>

    Shortcode on page:

    [fu-upload-form form_layout="post_image" category="7" title="Submit Content"]
    [input type="text" name="post_tags" id="ug_tags" description="Tags" multiple=""]
    [/fu-upload-form]

    I just realized this was specifically for Media Tags. Is there a way to do this with normal post tags?

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘[Resolved] How to add tags ?’ is closed to new replies.