• Resolved aymate

    (@aymate)


    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/

Viewing 15 replies - 1 through 15 (of 16 total)
  • 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

    Thread Starter aymate

    (@aymate)

    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.

    Which sounds like the best solution anyway 😉 Glad that solved it.

    Could you mark the topic as resolved please ?

    Thread Starter aymate

    (@aymate)

    ta.

    Thread Starter aymate

    (@aymate)

    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…

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

    Thread Starter aymate

    (@aymate)

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

    Jesse

    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.

    Thread Starter aymate

    (@aymate)

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

    Thread Starter aymate

    (@aymate)

    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

    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

    Thread Starter aymate

    (@aymate)

    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.

    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

    Thread Starter aymate

    (@aymate)

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

    Thanks you so much.

    Jesse

    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.

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Warning when used with ACF’ is closed to new replies.