modify [mla] shortcode so it would filter by custom field
-
Hi,
I’m just getting into incerdible possibilities of this plugin, but since I’m not a coder or IT professional, I came across a problem that is probably simple, but hard to solve for me.I use MLA to generate gallery in my portfolio pages. The shortcode is following:
[mla_gallery post_mime_type="image" size=small link=file columns=4 mla_style=projektova_galerie mla_markup=projektova_galerie_captions mla_rollover_text="{+title+} - {+caption+}" mla_margin=0.5% attachment_category=jakub-chalupa]I have a project with many custom fields. One of the custom fields named “project_slug” is filled with the same information as “attachment_category” field (these two custom fields are filled in with identical string).
What I would like to do is modify the above shotcode so that I wouldn’t have to manually fill in “jakub-chalupa” but this value would be taken from the custom field “project_slug”.Is it possible?
I would really appreciate any help.
Thanks
-
Thanks for the good words about MLA and for your question. I am confident a solution can be found, but I want to make sure I understand how your application is set up.
It looks like you have created many
attachment_categoryterms, such asjakub-chalupa, and assigned them to your images.You wrote “I have a project with many custom fields.“, such as
project_slug. Is this a custom field for the Page, or for the Image? It looks like theproject_slugis associated with the Page.If I understand your question, you want to take the value of the
project_slugcustom field associated with the Page in which the[mla_gallery]shortcode appears and use the value to display all images assigned to that value in theattachment_categorytaxonomy; is that right?If so, the easiest solution will be to hook one of the filters supported by the
[mla_gallery]shortcode. You can read about the filters in the “MLA Gallery Filters and Actions (Hooks)” section of the Settings/Media Library Assistant Documentation tab.I can give you the code for a small custom plugin that will make the parameter substitution you need. Let me know if I have understood your question and if you would like the code for a simple solution.
You understand it absoloutely correctly. Just to make sure I’ll describe my usecase:
I have a lot of projects I want to show in my portfolio. Each project has quite a lot of images. All projects have several custom fields, such as name, cost, client, where it is etc. One of these fields is a custom field called “project_slug”. For each project, I have a separate attachment_category value, which is the same as “project_slug”. This way I should be able to automatically link project and its images. So yes, I have a lot of attachment_category values. (I have more than 50 projects and each could have about 10-20 images).
So yes, I want to take the value of the project_slug custom field associated with the Page in which the [mla_gallery] shortcode appears and use the value to display all images assigned to that value in the attachment_category taxonomy. This would show up the project gallery.
I started to read Filters and Hooks, but I’m afraid it’s way beyond my programming knowledge. I would really be gratefull for the plugin.
Thank you for confirming my understanding of your application. I have developed a small custom plugin example that should get you the results you need. The plugin adds a new
[mla_gallery]parameter,parent_meta. When this parameter is present, the plugin looks up the custom field value and creates a new parameter based on the content. For example, the shortcode in your example above would be re-coded as:[mla_gallery size=small link=file columns=4 mla_style=projektova_galerie mla_markup=projektova_galerie_captions mla_rollover_text="{+title+} - {+caption+}" mla_margin=0.5% parent_meta=project_slug]You can also change the taxonomy by adding the taxonomy slug as the second part of the
parent_metavalue, e.g.,parent_meta="project_slug,attachment_tag".I have included the complete source code for the plugin below. You can copy it into a file named
mla-project-slug-example.phpand put the file in your/wp-content/pluginsdirectory. Then, go to your wp-admin “Plugins/Installed Plugins” screen and activate the “MLA Project Slug Example” plugin.If you would like a copy by e-mail, you can send me your contact information from the Contact Us page at our web site:
I will leave this topic unresolved until I hear back from you. Thanks for an interesting question and for your interest in the plugin.
PLUGIN SOURCE CODE
<?php /** * Creates a taxonomy=slug parameter from a parent's custom field value * * @package MLA Project Slug Example * @version 1.00 */ /* Plugin Name: MLA Project Slug Example Plugin URI: http://fairtradejudaica.org/media-library-assistant-a-wordpress-plugin/ Description: Provides examples of hooking the filters provided by the [mla_gallery] shortcode Author: David Lingren Version: 1.00 Author URI: http://fairtradejudaica.org/our-story/staff/ Copyright 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 Project Slug Example hooks all 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. * * @package MLA Project Slug Example * @since 1.00 */ class MLAProjectSlugExample { /** * Initialization function, similar to __construct() * * @since 1.00 * * @return void */ 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: * $tag - name of the hook you're filtering; defined by [mla_gallery] * $function_to_add - function to be called when [mla_gallery] applies the filter * $priority - default 10; lower runs earlier, higher runs later * $accepted_args - number of arguments your function accepts */ add_filter( 'mla_gallery_attributes', 'MLAProjectSlugExample::mla_gallery_attributes_filter', 10, 1 ); } /** * 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"]. * * @since 1.00 * * @param array the shortcode parameters passed in to the shortcode * * @return array updated shortcode attributes */ public static function mla_gallery_attributes_filter( $shortcode_attributes ) { if ( isset( $shortcode_attributes['parent_meta'] ) ) { global $post; $values = explode( ',', $shortcode_attributes['parent_meta'] ); $meta_key = $values[0]; $taxonomy = ( isset( $values[1] ) ) ? $values[1] : 'attachment_category'; $values = get_post_meta( $post->ID, $meta_key ); if ( is_array( $values ) ) { $shortcode_attributes[ $taxonomy ] = implode( ',', $values ); unset( $shortcode_attributes['parent_meta'] ); } } //error_log( 'MLAProjectSlugExample::mla_gallery_attributes_filter $shortcode_attributes = ' . var_export( $shortcode_attributes, true ), 0 ); return $shortcode_attributes; } // mla_gallery_attributes_filter } // Class MLAProjectSlugExample /* * Install the filters at an early opportunity */ add_action('init', 'MLAProjectSlugExample::initialize'); ?>Hi, thank you for your incredible work. I managed to do the plugin and activate it (MLA Project Slug Example is active). I changed the shortcode to this:
[mla_gallery size=small link=file columns=4 mla_style=projektova_galerie mla_markup=projektova_galerie_captions mla_rollover_text="{+title+} - {+caption+}" mla_margin=0.5% parent_meta=project_slug]but the result is, that all Media images are displayed.
Am I doing something wrong?Do I understand it correctly, if I want to display images with slug specified in “project_slug” and attachment_tag equal to “hlavni” the shortcode would look like this?
[mla_gallery size=small link=file columns=4 mla_style=projektova_galerie mla_markup=projektova_galerie_captions mla_rollover_text="{+title+} - {+caption+}" mla_margin=0.5% parent_meta="project_slug,hlavni"]Sorry for the last post, it works… I just didn’t realize, that the custom field has a different name. It was created by plugin, which gave it its own prefix… Now it works beautifully!
Also generating only images with attachment_tag=hlavni is working… I used this shortcode:
[mla_gallery size=small link=file columns=4 mla_style=projektova_galerie mla_markup=projektova_galerie_captions mla_rollover_text="{+title+} - {+caption+}" mla_margin=0.5% parent_meta=project_slug attachment_tag=hlavni]Many thanks again.
Sorry for the last post, it works… I just didn’t realize, that the custom field has a different name. It was created by plugin, which gave it its own prefix… Now it works beautifully!
Also generating only images with attachment_tag=hlavni is working… I used this shortcode:
[mla_gallery size=small link=file columns=4 mla_style=projektova_galerie mla_markup=projektova_galerie_captions mla_rollover_text="{+title+} - {+caption+}" mla_margin=0.5% parent_meta=project_slug attachment_tag=hlavni]Many thanks again.
Thank you for your updates with the good news. I am happy to hear you were able to implement the custom plugin and that it works well for you.
I regret that my earlier description of the
parent_metaparameter was unclear. The second part of the value is the taxonomy slug you want to use, not a value for theattachment_tagtaxonomy. For example,parent_meta="project_slug,attachment_tag"would take the value of theproject_slugcustom field and use it to form anattachment_tag= ...parameter. I believe you figured this out for yourself.The taxonomy parameters in your last example,
parent_meta=project_slug attachment_tag=hlavni, will match the items with theproject_slugcustom field value in theattachment_categorytaxonomy ANDattachment_tag=hlavni. In other words, when there are multiple taxonomy parameters in the shortcode the connector is AND.I am marking this topic resolved, but please update it if you have any problems with or more questions about the custom plugin. Thanks again for your interest in the plugin.
The topic ‘modify [mla] shortcode so it would filter by custom field’ is closed to new replies.