WordPress.org

Ready to get started?Download WordPress

Forums

MimeTypes Link Icons
[resolved] Warning when used with ACF (17 posts)

  1. aymate
    Member
    Posted 4 months ago #

    I'm using this plugin in combinati0on with Advanced Custom Fields and have had some success using this in functions.php

    add_filter('acf/load_value', 'mimetypes_to_icons');

    However I get the following warning message:

    Warning: preg_match_all() expects parameter 2 to be string, array given in C:\xampp\htdocs\WP-sandpit\wp-content\plugins\mimetypes-link-icons\mime_type_link_images.php on line 1187

    Cheers - Jesse

    https://wordpress.org/plugins/mimetypes-link-icons/

  2. jrf
    Member
    Plugin Contributor

    Posted 4 months ago #

    Hi aymate,

    Apparently acf at that provides a array for the content, not a string which is why it's not working. Try changing your code to the below and I think it'll work then:

    add_filter('acf/load_value', 'add_mimetypes_to_icons_to_acf', 15);
    
    function add_mimetypes_to_icons_to_acf( $content ) {
    	if( is_string( $content ) && $content !== '' ) {
    		$content = mimetypes_to_icons( $content );
    	}
    	else if( is_array( $content ) && $content !== array() ) {
    		foreach( $content as $key => $value ) {
    			$content[$key] = mimetypes_to_icons( $value );
    		}
    	}
    	return $content;
    }

    Hope this helps.

    Smile,
    Juliette

  3. aymate
    Member
    Posted 4 months ago #

    Thanks Juliete

    When I use the above, it injects this style reference to one of my attached files;

    <style type="text/css">a[rel~="mtli_filesize7992kB"]:after {content:" (79.92 kB)"}</style>

    In each ACF field after the first that includes MIME icons etc...

    The only way around this I have found is to target the specific field I wish to include icons in by;

    add_filter('acf/load_value/name=my_field', 'mimetypes_to_icons');

    j.

  4. jrf
    Member
    Plugin Contributor

    Posted 4 months ago #

    Which sounds like the best solution anyway ;-) Glad that solved it.

    Could you mark the topic as resolved please ?

  5. aymate
    Member
    Posted 4 months ago #

    ta.

  6. aymate
    Member
    Posted 4 months ago #

    My apologies on starting this up again... I turns out that my unique field name solution was not actually working!

    So - If I have multiple instances of ACF fields on a page, each being referenced uniquely like so in fuctions.php;

    add_filter('acf/load_value/name=field_one', 'mimetypes_to_icons');
    add_filter('acf/load_value/name=field_two', 'mimetypes_to_icons');

    The fields after the first one that includes MIME icons have the;

    <style type="text/css">
    a[rel~="mtli_filesize7992kB"]:after {content:" (79.92 kB)"}
    </style>

    reference from the first field injected as inline styles...

    Can you think of any further possible solutions?

    Many thanks in advance...

  7. jrf
    Member
    Plugin Contributor

    Posted 4 months ago #

    Could you share a link to a page where this is happening with me ?

  8. aymate
    Member
    Posted 4 months ago #

    It's on a local dev server right now... I'll get you a link asap! Thanks.

    Jesse

  9. jrf
    Member
    Plugin Contributor

    Posted 4 months ago #

    Hi Jesse, just checking whether you have moved the site from local dev yet. I'd like to try and help you to get this working for your site.

  10. aymate
    Member
    Posted 4 months ago #

    Just waiting for the DNS to propogate - thanks so much for thinking of me.

  11. aymate
    Member
    Posted 4 months ago #

    OK ready! If you have a peek within the markup of each of the accordions (built with ACF), you will see the:

    <style type="text/css">
    <!--
    a[rel~="mtli_filesize7992kB"]:after {content:" (79.92 kB)"}
    -->
    </style>

    If the field is a plain text filed, this style markup gets rendered on screen.

    wace1516.scsa.wa.edu.au/science/

    Thanks. Jesse

  12. jrf
    Member
    Plugin Contributor

    Posted 4 months ago #

    Hi Jesse,

    I've just had a quick look a think I see what you mean, however all the accordions contain a PDF file, the first one is different, the rest all the same. That gives me the impression that the ACF filter is passing the pdf file link to our function - upon which the css (correctly) gets added.

    Could you check what ACF is passing to the filter ?

    Looking at the source, it also looks like ACF is running the filter twice for each content bit which does not help.

    Just an idea: what about building the page content with output buffering turned and then passing the complete content in one go to MTLI ? I think that would bypass whatever strangeness ACF seems to be doing.

    Does that help ?

    Smile,
    Juliette

  13. aymate
    Member
    Posted 4 months ago #

    Hey Juliette

    I have deleted the contents of all but one accordion so you can see the repeated markup in each empty one.

    Each accordion has a unique field referenced like this:

    add_filter('acf/load_value/name=bio_content', 'mimetypes_to_icons');

    Just an idea: what about building the page content with output buffering turned and then passing the complete content in one go to MTLI ? I think that would bypass whatever strangeness ACF seems to be doing.

    That makes absolutely no sense to me I'm afraid. ;) Looks like I've got some reading up to do!

    j.

  14. jrf
    Member
    Plugin Contributor

    Posted 4 months ago #

    Hi Jesse,

    I'll try and explain a little more. Output buffering basically catches anything you are sending to the screen and buffers it for later use.

    In this case I'm suggesting you remove the filter(s) you added as that method does not seem to work well in combination with ACF. Instead in the template where you are building up the page, I suggest you use code along the lines of the below.

    <?php
    	// At the start or the template part file, turn output buffering on
    	ob_start();
    
    	/*
    	... build up the page content with the code you already have, no need to adjust anything
    	*/
    
    	// At the end of the template part file, you retrieve the output from the buffer and put it in a variable.
    	$content = ob_get_clean();
    	// Pass it to the MTLI function to add the icons
    	$content = mimetypes_to_icons( $content );
    	// and send the result to the screen
    	echo $content;
    ?>

    For more information on these functions: http://php.net/ref.outcontrol

    Hope this helps!

    Smile,
    Juliette

  15. aymate
    Member
    Posted 4 months ago #

    Best. Juliette, you are amazing. If your ever in Perth, Western Australia I'll buy you a beer!

    Thanks you so much.

    Jesse

  16. jrf
    Member
    Plugin Contributor

    Posted 4 months ago #

    You're welcome ;-) Can I presume that this helped you to get it working ?

    I look forward to that drink if ever I make it to Perth. Let me know if there are PHP/WP conferences in the area looking for speakers and I might put in for the call for papers and who knows I might just make it.

  17. aymate
    Member
    Posted 4 months ago #

    It's working. ;)

Reply

You must log in to post.

About this Plugin

About this Topic