WordPress.org

Ready to get started?Download WordPress

Forums

Media Library Assistant
[resolved] format date (7 posts)

  1. Francesco
    Member
    Posted 9 months ago #

    Hi,

    I used this shortcode for my gallery:

    [mla_gallery columns=2 post_parent=all attachment_category=my_category orderby=date order=desc posts_per_page=10 post_mime_type=application/pdf size=icon link=file mla_caption="{+description+} {+date+}"] 
    
    [mla_gallery post_parent=all attachment_category=my_category orderby=date order=desc posts_per_page=10 post_mime_type=application/pdf mla_output=paginate_links]

    I'm just wondering if it's possible to format the date present in the caption in d/m/Y .

    http://wordpress.org/plugins/media-library-assistant/

  2. David Lingren
    Member
    Plugin Author

    Posted 9 months ago #

    Thanks for your interest and for this question. In the current MLA version the only way to do this would be to use the [mla_gallery] Filters and write a bit of PHP code to reformat the caption the way you want it.

    I am on the road and working from memory, so I can't be too specific. If you go to the Settings/Media Library Assistant Documentation tab and look at the WordPress Filters section you can get the information you need. There is a complete example of a small plugin you can adapt to your specific needs. You can add a bit of code to the Item Values filter to replace the "date" field with the format you want; everything else should be fine.

    See what you can do with that. I'll be back home next week and I can give you more specific help if you need it. Thanks for your patience and your understanding.

  3. David Lingren
    Member
    Plugin Author

    Posted 9 months ago #

    I am back home and if you are still interested in pursuing this issue I can give you more assistance.

    Let me know if my suggestion to use the [mla_gallery] filters is something that would work for you, and if you need more details to implement it.

    Thanks for your interest and your patience.

  4. Francesco
    Member
    Posted 9 months ago #

    I've to thank you for your kindness, now I'm busy on another project, but soon I could use this topic as a reference.

  5. David Lingren
    Member
    Plugin Author

    Posted 9 months ago #

    When you get back to this project, here is some code you can use to accomplish your goal.

    Start with the example code you can find in the /plugins/media-library-assistant/examples directory, file mla-hooks-example.php.txt

    Rename the file to remove the ".txt" portion and place it in your /plugins directory. You can change the name of the plugin and file if you like.

    Edit the file and add this block of code just below the error_log( ... statement in the public static function mla_gallery_item_values_filter( $item_values ) function:

    /*
     * For this first example, we will reformat the 'date' value as d/m/Y. We use a shortcode parameter of our
     * own to do this on a gallery-by-gallery basis, leaving other [mla_gallery] instances untouched.
     */
    if ( isset( self::$shortcode_attributes['my_filter'] ) && 'format date' == self::$shortcode_attributes['my_filter'] ) {
    
    	/*
    	 * Default format is YYYY-MM-DD HH:MM:SS (HH = 00 - 23), or 'Y-m-d H:i:s'
    	 * Convert to UNIX timestamp so any reformat is possible
    	 */
    	$old_date = $item_values['date'];
    	$timestamp = mktime( substr( $old_date, 11, 2 ), substr( $old_date, 14, 2 ), substr( $old_date, 17, 2 ), substr( $old_date, 5, 2 ), substr( $old_date, 8, 2 ), substr( $old_date, 0, 4 ) );
    
    	/*
    	 * Update the $item_values and pass them back from the filter.
    	 * We must also update the caption because it was composed before this filter is called.
    	 */
    	$item_values['date'] = date( 'd/m/Y', $timestamp );
    	$item_values['caption'] = $item_values['description'] . $item_values['date'];
    	return $item_values;
    }

    Save the file and activate the plugin. Add the my_filter="format date" parameter to the [mla_gallery] shortcode and you should see the captions with the re-formatted date. ANy other shortcodes that do not contain the added parameter will be unaffected by the filter.

    I'm going to mark this topic resolved for now, but feel free to update it if you have any problems or further questions. Thanks for your interest in the plugin.

  6. David Lingren
    Member
    Plugin Author

    Posted 8 months ago #

    Francesco,

    I just found your post in another topic, where you wrote:

    very well everything works fine, I just modified this line:

    $item_values['caption'] = $item_values['description'] ." - ". $item_values['date'];

    thanks for your kind assistance.

    I assume your comment actually refers to the above suggestion to use the MLA plugin filters for date formatting.

    Thanks for the update and the good news. If you have any problems or further questions, let me know.

  7. Francesco
    Member
    Posted 8 months ago #

    Yes it's true, sorry for the incovenience, yesterday I had a lot of tabs open

    anyway, my answer for this topic is:

    very well everything works fine, I just modified this line:
    $item_values['caption'] = $item_values['description'] ." - ".$item_values['date'];

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.