Fixed href link or caption text per id?
-
Sorry to bother you again with perhaps an obvious question but I cannot find the answer to my question.
I am still experimenting with your great plug-in. Now I am trying to set something fixed for the mla gallery. I know I could use a custom field on the media file but what about a situation that you use the same file in different galleries? Then you would want to set your meta data on the gallery itself in stead of the media file.
I have experimented with this:
[mla_gallery ids="1015,1165,1074" mla_caption="array('test1','test2','test3')"]
The idea being that the first image gets caption test1 and so on.Now I just get the text (including the array bit) as the caption for every image.
How can I accomplish what I want (I also want to set something alike for the href)?
BTW The link in your documentation “Support for Alternative Gallery Output, e.g., Pagination” does not work. It is there more than once.
-
First, thanks for letting me know about the broken links in the Documentation tab. I have found and fixed four bad links to the Pagination section.
Thanks as well for your interesting question and for including the creative experiment you’ve already tried. There is no native MLA support for “Fixed … per ID” substitution, but you can accomplish your goal by hooking the
mla_gallery_item_values
filter provided by the[mla_gallery]
shortcode. You can add the code to an existing PHP file, such as your theme’s ‘functions.php’ file or you can add a small custom plugin, as I have done. Here is the complete source code for the custom plugin:<?php /* Plugin Name: MLA Fixed Values Example Plugin URI: https://fairtradejudaica.org/media-library-assistant-a-wordpress-plugin/ Description: Provides an example of hooking the filters provided by the [mla_gallery] shortcode Author: David Lingren Version: 1.00 Author URI: https://fairtradejudaica.org/our-story/staff/ Copyright 2013, 2014 David Lingren This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You can get a copy of the GNU General Public License by writing to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA */ /** * Class MLA Fixed Values Example hooks a few of the filters provided by the [mla_gallery] shortcode * * Call it anything you want, but give it an unlikely and hopefully unique name. Hiding everything * else inside a class means this is the only name you have to worry about. */ class MLAFixedValuesExample { /** * Initialization function, similar to __construct() */ public static function initialize() { /* * The filters are only useful for front-end posts/pages; exit if in the admin section */ if ( is_admin() ) return; /* * add_filter parameters: */ add_filter( 'mla_gallery_attributes', 'MLAFixedValuesExample::mla_gallery_attributes_filter', 10, 1 ); add_filter( 'mla_gallery_item_values', 'MLAFixedValuesExample::mla_gallery_item_values_filter', 10, 1 ); } /** * Save the shortcode attributes */ private static $shortcode_attributes = array(); /** * MLA Gallery (Display) Attributes * * This filter gives you an opportunity to record or modify the arguments passed in to the shortcode * before they are merged with the default arguments used for the gallery display. * * The $shortcode_attributes array is where you will find any of your own parameters that are coded in the * shortcode, e.g., [mla_gallery my_parameter="my value"]. */ public static function mla_gallery_attributes_filter( $shortcode_attributes ) { /* * Save the attributes for use in the later filter */ self::$shortcode_attributes = $shortcode_attributes; return $shortcode_attributes; } // mla_gallery_attributes_filter /** * MLA Gallery Item Values * * @since 1.00 * * @param array parameter_name => parameter_value pairs * * @return array updated substitution parameter name => value pairs */ public static function mla_gallery_item_values_filter( $item_values ) { /* * We use a shortcode parameter of our own to apply our filters on a gallery-by-gallery basis, * leaving other [mla_gallery] instances untouched. If the "fixed_values" parameter is not present, * we have nothing to do. Here is an example of how the custom parameter can be used: * * [mla_gallery ids="2621,2622,2623" fixed_values="array('test1','test2','test3')" mla_caption="{+fixed_value+}"] */ if ( ! isset( self::$shortcode_attributes['fixed_values'] ) ) { return $item_values; // leave them unchanged } /* * Evaluate the parameter value once per page load. */ static $fixed_values = NULL; if ( NULL === $fixed_values ) { $function = @create_function('', 'return ' . self::$shortcode_attributes['fixed_values'] . ';' ); if ( is_callable( $function ) ) { $fixed_values = $function(); } if ( ! is_array( $fixed_values ) ) { $fixed_values = array(); } } /* * Apply the appropriate value to the current item. */ if ( isset( $fixed_values[ $item_values['index'] - 1 ] ) ) { $item_values['fixed_value'] = $fixed_values[ $item_values['index'] - 1 ]; } return $item_values; } // mla_gallery_item_values_filter } // Class MLAFixedValuesExample /* * Install the filters at an early opportunity */ add_action('init', 'MLAFixedValuesExample::initialize'); ?>
Copy the code to a file named
mla-fixed-values-example.php
and add it to your/plugins
directory. Then go to the Plugins/Installed plugins, find “MLA Fixed Values Example” and activate it.Change your experimental shortcode to something like:
[mla_gallery ids="1015,1165,1074" fixed_values="array('test1','test2','test3')" mla_caption="{+fixed_value+}"]
That should replace the captions in your first three items with the text values. You could extend the idea to multiple fixed parameters by modifying the code to recognize something like:
[mla_gallery ids="1015,1165,1074" fixed_captions="array('test1','test2','test3')" fixed_hrefs="array('https://site.com/link1','https://site.com/link2','https://site.com/link3')" mla_caption="{+fixed_caption+}" mla_link_href="{+fixed_href+}"]
It’s also possible to add code to allow substitution parameters in your “fixed” values to support something like:
fixed_hrefs="array('https://{+page_url+}link1','{+page_url+}link2','{+page_url+}link3')"
But that’s a bit more complex.
I am marking this topic resolved, but if you need help with the example or extending it to multiple parameters, please update it. Thanks for an interesting idea and for your interest in the plugin.
Thanks! This works flawlessly.
Are you considering making this approach part of the MLA plug-in itself in the future? I think it will appeal to others as well.
Or perhaps I can help coding and/or testing it?BTW Do you have some list of future enhancements?
I am happy to hear the solution I suggested is working for you. Let me know if you need help building on it for your other application needs.
For now, I plan to include the custom plugin solution as one of the “example plugins” in the
/media-library-assistant/examples
directory. If it seems popular I may add something like it to a future MLA version. Thank you for your generous offer to help with coding and testing; I will keep that in mind.I do indeed have a long and growing list of future enhancements and I am hacking away at it when time permits.
Thanks again for an interesting question and for your ongoing interest in the plugin.
Hi!
I have updated your example to be able to set up any variable as long as the parm starts with mla_fixed_…..
Also treat a value just separated by a comma as an array.So when you code:
[mla_gallery ids="1015,1165,1074" link=file mla_fixed_caption="array('test1','test2','test3')" mla_fixed_title="'title1','title2','title3'" mla_fixed_alt="array('alttest1','alttest2','alttest3')" mla_caption="{+mla_fixed_caption+}" mla_link_attributes="title={+mla_fixed_title+}" mla_image_attributes="title={+mla_fixed_title+} alt={+mla_fixed_alt+}"]
You will get fixed values in your anchor and img title and alt and caption as well.
Perhaps you would contemplate of adding this code permanently to your plug-in?
<?php /* Plugin Name: Media Library Assistant Fixed Values Plugin URI: https://fairtradejudaica.org/media-library-assistant-a-wordpress-plugin/ Description: Adds the option to specify fixed values and use them in MLA [mla_gallery] shortcode Author: David Lingren Version: 1.00 Author URI: https://fairtradejudaica.org/our-story/staff/ Copyright 2013, 2014 David Lingren This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You can get a copy of the GNU General Public License by writing to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA */ /** * Class MLA Fixed Values hooks a few of the filters provided by the [mla_gallery] shortcode * * Call it anything you want, but give it an unlikely and hopefully unique name. Hiding everything * else inside a class means this is the only name you have to worry about. */ class MLAFixedValuesExample { /** * Initialization function, similar to __construct() */ public static function initialize() { /* * The filters are only useful for front-end posts/pages; exit if in the admin section */ if ( is_admin() ) return; /* * add_filter parameters: */ add_filter( 'mla_gallery_attributes', 'MLAFixedValuesExample::mla_gallery_attributes_filter', 10, 1 ); add_filter( 'mla_gallery_item_values', 'MLAFixedValuesExample::mla_gallery_item_values_filter', 10, 1 ); } /** * Save the shortcode attributes */ private static $shortcode_attributes = array(); /** * MLA Gallery (Display) Attributes * * This filter gives you an opportunity to record or modify the arguments passed in to the shortcode * before they are merged with the default arguments used for the gallery display. * * The $shortcode_attributes array is where you will find any of your own parameters that are coded in the * shortcode, e.g., [mla_gallery my_parameter="my value"]. */ public static function mla_gallery_attributes_filter( $shortcode_attributes ) { /* * Save the attributes for use in the later filter */ self::$shortcode_attributes = $shortcode_attributes; return $shortcode_attributes; } // mla_gallery_attributes_filter /** * MLA Gallery Item Values * * @since 1.00 * * @param array parameter_name => parameter_value pairs * * @return array updated substitution parameter name => value pairs */ public static function mla_gallery_item_values_filter( $item_values ) { /* * We use a shortcode parameter of our own to apply our filters on a gallery-by-gallery basis, * leaving other [mla_gallery] instances untouched. If the "fixed_values" parameter is not present, * we have nothing to do. Here is an example of how the custom parameter can be used: * * [mla_gallery ids="2621,2622,2623" mla_fixed_caption="array('test1','test2','test3')" mla_caption="{+mla_fixed_caption+}"] */ // Is there something that starts with mla_fixed_ ? $mla_fixed_indicator = false; foreach ( self::$shortcode_attributes as $parmkey => $parmvalue ) { if ( substr($parmkey,0,10) == 'mla_fixed_' ) { $mla_fixed_indicator = true; break; } } if ( ! $mla_fixed_indicator ) { return $item_values; // leave them unchanged } /* * Evaluate the parameter value once per page load. */ static $mla_fixed_values = NULL; if ( NULL === $mla_fixed_values ) { $mla_fixed_values = array(); foreach ( self::$shortcode_attributes as $parmkey => $parmvalue ) { if ( substr($parmkey,0,10) == 'mla_fixed_' ) { $function = @create_function('', 'return ' . self::$shortcode_attributes[$parmkey] . ';' ); if ( is_callable( $function ) ) { $mla_fixed_values[$parmkey] = $function(); } if ( ! is_array( $mla_fixed_values[$parmkey] ) ) { $mla_fixed_expl = explode(",",$parmvalue); if ( is_array( $mla_fixed_expl) ) { $mla_fixed_values[$parmkey] = $mla_fixed_expl; } else { $mla_fixed_values[$parmkey] = array(); } } } } } foreach ( $mla_fixed_values as $mla_fixed_key => $mla_fixed_value ) { /* * Apply the appropriate value to the current item. */ if ( isset( $mla_fixed_value[ $item_values['index'] - 1 ] ) ) { $item_values[$mla_fixed_key] = $mla_fixed_value[ $item_values['index'] - 1 ]; } } return $item_values; } // mla_gallery_item_values_filter } // Class MLAFixedValuesExample /* * Install the filters at an early opportunity */ add_action('init', 'MLAFixedValuesExample::initialize'); ?>
Thanks so much for taking the time to improve and extend the example, and for sharing your code in the Support Forum. I have updated the MLA example plugin along the lines you suggested, making a few changes and conforming to WordPress coding standards. The complete plugin will be in the
/examples
directory of my next MLA version. Here is the updated code for the key part of the solution:public static function mla_gallery_item_values_filter( $item_values ) { /* * We use shortcode parameters of our own to apply our filters on a * gallery-by-gallery basis, leaving other [mla_gallery] instances untouched. * If no "mla_fixed_" parameters are present, we have nothing to do. Here is * an example of how the custom parameter can be used: * * [mla_gallery ids="2621,2622,2623" mla_fixed_caption="array('test1','test2','test3')" mla_caption="{+mla_fixed_caption+}"] * * You can have as many "mla_fixed_" parameters as you need for different values. * $mla_fixed_values stores the parameter(s) and values. If none are found, the * initialization code sets it to false so the logic is quickly bypassed. */ static $mla_fixed_values = NULL; if ( false === $mla_fixed_values ) { return $item_values; // leave them unchanged } /* * Evaluate the parameter value(s) once per page load. */ if ( NULL === $mla_fixed_values ) { $mla_fixed_values = array(); foreach ( self::$shortcode_attributes as $parmkey => $parmvalue ) { if ( 'mla_fixed_' == substr( $parmkey, 0, 10 ) ) { if ( 'array(' == substr( $parmvalue, 0, 6 ) ) { $function = @create_function( '', 'return ' . self::$shortcode_attributes[ $parmkey ] . ';' ); if ( is_callable( $function ) ) { $mla_fixed_values[ $parmkey ] = $function(); } if ( ! is_array( $mla_fixed_values[ $parmkey ] ) ) { $mla_fixed_values[ $parmkey ] = array(); } } else { $mla_fixed_values[ $parmkey ] = explode( ",", $parmvalue ); if ( false === $mla_fixed_values[ $parmkey ] ) { $mla_fixed_values[ $parmkey ] = array(); } } } // found mla_fixed_ } // foreach parameter if ( empty( $mla_fixed_values ) ) { $mla_fixed_values = false; return $item_values; } } // initialization code /* * Apply the appropriate value to the current item. */ foreach ( $mla_fixed_values as $mla_fixed_key => $mla_fixed_value ) { /* * Apply the appropriate value to the current item. */ if ( isset( $mla_fixed_value[ $item_values['index'] - 1 ] ) ) { $item_values[ $mla_fixed_key ] = $mla_fixed_value[ $item_values['index'] - 1 ]; } } return $item_values; } // mla_gallery_item_values_filter } // Class MLAFixedValuesExample
I am leaving this as an example plugin for now and if it catches on I will consider it for a future MLA version. Thanks for your contributions!
I have uploaded a new Development Version that contains the revised
/examples/mla-fixed-values-example.php.txt
example plugin.@harm10 – The updated example includes an acknowledgement of the enhancements you’ve made in the course of this support topic; thanks again.
I know I have read somewhere how to get hold of a Development version but I cannot find that instruction any more……. So how can I get this dev.version?
Thanks for the acknowledgement. There was no need for it but still it is a nice gesture!
I was also looking at creating a translation for you (I am Dutch).
First the file you point to in the FAQ is not called MLA Internationalization Guide.php but MLA Internationalization Guide.pdf
When I follow the instructions in that pdf by opening the pot file through Poedit I do not get a language dropdown. I can probably just fill the nl code in there? But what do I fill for source code character set?
And the plurals rule should just be the default setting?Thanks for your update and especially for the offer to produce a translation.
You can find step-by-step instructions for downloading the Development Version in this earlier topic:
I will fix the error in the FAQ; thanks. I will also go back over the instructions in the Guide and try to answer your questions about setting up a Dutch translation. Thanks again for the offer!
I found the development version.
But I also found a bug in the processing of this example.
If I code this
[mla_gallery ids="961,1006,942,922" mla_fixed_title="array('Text number 1','Text number 2','Text number 3','Text number 4')" mla_link_attributes="title={+mla_fixed_title+}" mla_image_attributes="title={+mla_fixed_title+}"]
I have to specify the fixed titles with spaces in between with quotes otherwise I get nothing in my fixed values. This seems logical but the end result of the above is that only the first string gets incorporated into the element (so only showing “Text”).
I can see the values leaving correctly in $item_values array but apparently after this something goes wrong.
If I change the code to
[mla_gallery ids="961,1006,942,922" mla_fixed_title="array('Text number 1','Text number 2','Text number 3','Text number 4')" mla_link_attributes="title='{+mla_fixed_title+}'" mla_image_attributes="title='{+mla_fixed_title+}'"]
so put quotes around the placing of the fixed values everything works OK.
How come?I have an additional question on this topic.
How can I change the example code in such a way that the mla_fixed values are set per mla_gallery code?
Now if I have 2 mla_gallery codes with the same mla_fixed parm but with other values the values of the first one are used when building the second gallery.And btw I have a code that gives me fixed hrefs:
[mla_gallery ids="961,1006,942,922" link=file mla_fixed_page="array('?p=1857','?m=201211','?p=306#comment-52','?page_id=107')" mla_fixed_title="array('Title 1','Title 2','Title 3','Title 4')" mla_fixed_alt="array('Alt 1','Alt 2','Alt 3','Alt 4')" mla_link_attributes="href='{+mla_fixed_page+}' title='{+mla_fixed_title+}'" mla_image_attributes="title='{+mla_fixed_title+}' alt='{+mla_fixed_alt+}'"]
Thanks for your updates and for your continuing development of these examples. First, you wrote “I have to specify the fixed titles with spaces in between with quotes“. It’s not a bug, but it is subtle behavior. The
mla_fixed_title
andmla_fixed_alt
parameters are part of HTML attributes, and spaces are used to separate the attributes within a tag. For example, the final output from the example (on my test system) looks something like:<a title='Title 2' href="https://mladev/?m=201211"><img width="150" height="150" title='Title 2' class="attachment-thumbnail" alt='Alt 2' src="https://mladev/wp-content/uploads/2015/01/blue-150x150.jpg"></a>
Without the quotes around the
title
andalt
values you will have this:<a title=Title 2 href="http ...
where “
2
” looks like a separate attribute; an error. The single quotes inmla_fixed_title="array('Title 1', ...
are stripped away when the array is compiled, so they don’t get passed on to the final result. You could move the quotes into the array specification asmla_fixed_title="array('"Title 1"', ...
, but that seems like more work.I hope that makes sense; I had to look at the source text and carefully think it through.
Moving on to “How can I change the example code in such a way that the mla_fixed values are set per mla_gallery code?” – an excellent question. You’ve found a shortcoming of the original code and something I just overlooked.
The initialization of
$mla_fixed_values
is done once per page load, not once per[mla_gallery]
. The solution is to move the variable declaration to the class level and hook another filter to reset it at the end of each shortcode. I have updated the MLA Development Version with an updatedmla-fixed-values-example.php.txt
example plugin. There are three changes in the new version. First, move the variable declaration:/* * $mla_fixed_values stores the parameter(s) and values. If none are found, the * initialization code sets it to false so the logic is quickly bypassed. */ private static $mla_fixed_values = NULL;
and remove the old declaration:
* You can have as many "mla_fixed_" parameters as you need for different values. */ if ( false === self::$mla_fixed_values ) { return $item_values; // leave them unchanged }
Second, hook one more filter in the
initialization
function:add_filter( 'mla_gallery_close_values', 'MLAFixedValuesExample::mla_gallery_close_values_filter', 10, 1 );
Finally, add the new filter code to the bottom of the class:
public static function mla_gallery_close_values_filter( $markup_values ) { /* * Reset $mla_fixed_values for multiple shortcodes on the same post/page */ self::$mla_fixed_values = NULL; return $markup_values; } // mla_gallery_close_values_filter
I realize those code fragments make more sense in context; you should just pick up the new version of the example from the MLA Development Version.
Finally, thanks for including your “code that gives me fixed hrefs“; it’s a great example. I have two suggestions: 1) include the
{+site_url+}
in the value so it will work from anywhere on your site, and 2) use themla_link_href
parameter, which is designed for exactly this purpose. You can move thehref
attribute from:mla_link_attributes="href='{+mla_fixed_page+} ...'
to
mla_link_href='{+site_url+}/{+mla_fixed_page+}' mla_link_attributes="title='{+mla_fixed_title+}'"
Thanks again for continuing this dialog.
I understand your explanation about the html. Better make sure that the shown example in the example code also has the fixed value applied between quotes. Currently there is
e.g., [mla_gallery mla_fixed_title="my title"].
and in my view this should read something like
e.g., [mla_gallery mla_fixed_title="array('my title','my other title')" mla_image_attributes="title='{+mla_fixed_title+}'"].
I have used your new example code and currently the fixed values are applied to the correct mla_gallery!
I already experimented with mla_link_href setting before I posted my previous example! And it doesn’t work.
This is my code:
[mla_gallery ids="961,1006,942,922" link=file mla_fixed_page="array('?p=1857','?m=201211','?p=306#comment-52','?page_id=107')" mla_fixed_title="array('Title 1','Title 2','Title 3','Title 4')" mla_fixed_alt="array('Alt 1','Alt 2','Alt 3','Alt 4')" mla_link_href="'{+site_url+}/{+mla_fixed_page+}'" mla_link_attributes="'title='{+mla_fixed_title+}'" mla_image_attributes="title='{+mla_fixed_title+}' alt='{+mla_fixed_alt+}'"]
which results in links to the page the gallery is on for all 4 images. If I leave out {+site_url+} the result is the same.And perhaps this post gets too cluttered (please tell me if you want a post to be split) with specific things but there is also something else wrong in the processing of the single and double quote when specified in a fixed value and applied in the html part.
If I code this:
[mla_gallery ids="961,1006,942,922" link=file mla_fixed_page="array('?p=1858','?m=201311','?p=406#comment-42','?page_id=127')" mla_fixed_title="array('Justatitle','Title"Title2','Title to the title','Title of title'Title2')" mla_link_attributes="href='{+mla_fixed_page+}' title='{+mla_fixed_title+}'" mla_image_attributes="title='{+mla_fixed_title+}'"]
the translated single and double quote end up translated again in the html element (for the single quote it looks fairly similar):<a title="Title"Title2" href="?m=201311"> <img ..... title="Title"Title2"></img></a>
Although the mouse-over displays the correct text still I think this is considered to be invalid html?
Thanks for the update and the detailed examples. I will expand the sample code in the plugin along the lines you’ve suggested; thanks.
This whole business of single- and double-quotes can be tricky.
Your code includes this parameter:
mla_link_href="'{+site_url+}/{+mla_fixed_page+}'"
Notice the single quotes you have added just inside the double quotes; that’s the problem. The parameter will generate HTML such as this example from my test system:
<a title='title1' href=''https://mladev/?attachment_id=2620''>
Again, notice the extra single quotes. That isn’t a valid URL so it is ignored. If you change the parameter to
mla_link_href="{+site_url+}/{+mla_fixed_page+}"
it will work as you intend.I’m not sure I understand your second question about “translated” quotes in the HTML elements. Did you mean to use the “ampersand + quot + semicolon” special characters in your code to escape a double quote (sometimes those get lost when you submit your post)? If so, those characters are working without the extra translation on my test system. It is possible that you switched from the “Text” tab to the “Visual” tab in the WordPress page editor; that will immediately convert the escape sequences and if you switch back to the Text tab the sequences are gone. Check for that and restore the escape sequences if they have been converted.
It’s fine to continue this topic – all of the posts are related.
Getting rid of those single quotes made the mla_link_href working as you predicted.
Now I look at my code example I understand why you do not understand me…… ‘Title###Title2’ is what I coded and not ‘Title”Title2’.
So I try to escape the double quote indeed. I expect the resulting html to also be title=”Title###Title2″ which it isn’t.
I understand your explanation what the WP editor will do.
So how I can escape a double quote and still end up with valid HTML?NB This editor also converts those escaped characters…..
So ### should read the & q u o t ; characterThank you for confirming my
mla_link_href
solution and my understanding of your HTML escape sequence issue.I have entered your example shortcode in my test system, using the appropriate “& quot ;” and “& #39 ;” escape sequences for the quote and single quote characters. When I use “View Source” on my browser I can see the escape sequences still present in the title attributes on the page. I do not see the conversion issue you are experiencing.
Without access to your site I do not know what else to say. It may be a browser issue but that is very unlikely. I am using Internet Explorer 11 on Windows Seven. All I can suggest is that you check your source text carefully and try some other experiments to collect more information. If you discover additional clues, let me know.
Thanks for your patience and persistence.
- The topic ‘Fixed href link or caption text per id?’ is closed to new replies.