Simple pages shortcode question

Is there an easy way to modify or create a short code that I can then use on a simple page? My goal is relatively simple: I only want to display items labeled with a specific “Subject”, i.e. where subject like %subjectname%.

Or it just easier to make my own page to run this script?

Adding new shortcodes is pretty easy: it requires you to write a simple plugin. We have some documentation on creating new shortcodes.

1 Like

Hi John,

I saw that documentation and have a few questions, apologies if they seem silly, but I have not yet done much on the back end (aside from server/db admin). Where do I add the short code, as in specifically: what file do I modify? Will I have to re-add this bit of code with every upgrade?

Also, I’m not too clear on whether or not this is best for our project, so perhaps you can advise. Basically, we will have several simple pages where we want to pull out materials based on “Subject.” The subject list is defined in Simple Vocab on the back end; we have modified it a few times, and the list of options when entering a new record obviously thus has varied with those modifications. I mulled over the issue quite a bit yesterday and wonder if a better long-term solution isn’t simply to group related items into a collection – I think I could probably just run a script on the back end to take all with x subject and dump them into collection “5” or what not. (Ideally, I think, these records should have been tagged or put in collections initially, rather than using “Subject,” but I inherited this site, and they’ve already got 1000+ records in it.) This would, of course, entail more work upfront, but probably make the items easier to deal with in the long-term. What do you think?

Shortcodes might be a good idea – it would be your own plugin, so not really modifying a file so much as creating all your own in a separate plugin. As a plugin, it would be separate from the core code, so no plugin worries.

You might also want to look at my BrowseValues plugin, which might do a lot of what you are looking for, if I understand your description correctly. I grabs all the publisher data for a bunch of newspapers and lists them out. It works in conjunction with SearchByMetadata to produce the links.

An example of what it does is on this list of newspaper publishers in an Omeka site of newspapers. It’s hardcoded to produce the list of DC Publishers, but a quick switch to Subject in the IndexController might be close to what you’re looking for.

(of course, you’d need to remove my hilarious joke of including J. Phineas Zweibel among the list of publishers in the view file)

1 Like

Hi Patrick,

Yes, that’s very similar to what we want to do - except that we only want to pull out specific subjects/values, so I guess I will need to add something to the database call, i.e. where text like %subjectname% ? I think this might work better than simple pages because I think it will give us more flexibility to change the script as needed (i.e. we could order by featured ones or whatever without having to go back and write more short code parameters).

Also, I’m new to the Omeka plug-ins (actually I’m fairly new to plug-ins as a whole… yes I’m that far out of date at this point). So I installed the BV plug-in and activated it, now how do I get it to work? Can I set it up to work with simple pages or do I make it its own separate page? I also can’t figure out where the current code for the short codes are listed… I didn’t seem them in the Simple Pages plug-in?

Sorry for the PHP circa 2004 ignorance.

So how would I connect this to the Simple Page?

No worries…there’s a lot going on!

BV is pretty basic, and doesn’t include many of the usual Omeka features because I built it just for that one-off project on newspapers. In particular, it looks like it doesn’t hook into the navigation system, which is probably why nothing looks different. If you add browse-values to the end of your URL, that should get you directly to the page it creates. Depending on your data, it might be empty, at least until you change the $elementId value in IndexController.php to be the subject element id. For the site that uses it, I likely just added that URL directly in the Appearance/Navigation page. So, no interaction with simple pages, and it should just create its own page.

Making this approach work on only some values might get tricky…it’s not really set up to work with that kinds of restriction – it’s pretty much all or nothing.

[Jumping to your post that came in while I was writing…]

Everything I’ve been describing is completely separate from Simple Pages – BV creates its own pages, and SearchByMetadata is pretty much separate from all pages. It operates directly on the metadata values, wherever they are displayed.

So, concentrating on Simple Pages leads quickly to building a plugin to create a shortcode. What’s here is about approaches that don’t use Simple Pages.

1 Like

I realized this about the same time you were writing… there’s not an easy solution to amending the script with another table value.

OK, so I guess then that Simple Pages short codes is the way to go, or at least I can try it out and see how far I get. Can you tell me: where exactly would I add the short code using the parameters in the developer’s book? In other words: where are the current short codes stored? I couldn’t find them when digging around.

Thanks for your speedy responses by the way!

So, shortcodes aren’t necessarily Simple Pages specific, but that’s the most natural place for them.

In the documentation John linked to above, concentrate on the code example of MyPlugin for a starting point. They aren’t so much ‘stored’ as they are registered in Omeka. Plugins add them with the hook described there.

The built-in shortcodes are created in application/views/helpers/Shortcodes.php. It works by creating parameters to pass to Omeka’s usual get_records() function, so it avoids writing any SQL directly.

For what you are looking for, you’d need to create the equivalent of advanced search parameters that search on a specific element and element text value. The array of parameters for that is fairly complex, but can be generated.

The link function in SearchByMetadata does a similar trick with the array for advanced search. It’s not building a shortcode, but it might be an example to work from as you build up what your plugin needs to do for the search.

1 Like

Yes, it is amazing how “simple” it looks when running the script on the back end (SELECT * FROM omeka_element_texts WHERE element_id = 49 AND text LIKE ‘SubjectText’) … wish there was a simpler way, but the metadata link might do the trick. Thanks for the tip!

OK, so I think my confusion is over how exactly the added shortcode communicates with the user input, if that makes sense? I’ve taken a look at the files, read the documentation, and I’m starting to build the plugin now, but where I get stuck is on this:

public function shortcode($args, $view)
return ‘This is a very simple shortcode.’;

–> exactly how will this connect to what it says in the next section:

"The flipside of adding new shortcodes is supporting shortcode replacement in text input by the user. To support shortcodes, you only need to pass the text through the shortcodes view helper.

Assuming $text is some text input by the user, you barely need to change your code that would have normally output the text:

/* No shortcode support: */
echo $text;

/* Shortcode support: */
echo $this->shortcodes($text);"

So does the plug-in automatically modify the shortcodes view helper? Or am I supposed to modify this myself?

The “flipside” part is only about extending shortcode support generally to new places, for example if you have a plugin that you want users to be able to type shortcodes into.

If you’re just trying to get something to work in Simple Pages, that section isn’t relevant for you: Simple Pages already supports shortcodes. By doing just the add_shortcode part, Simple Pages will work with your shortcode without you needing to make any changes to the Omeka core or to Simple Pages.

I’m sorry, but I still don’t quite understand:

yes, but WHERE? in which file do I add the add_shortcode for the new function? Within the main PHP file within the SimplePages plug-in folder or somewhere else??? (Obviously I can’t just add_shortcode on the front end in the HTML editor, or can I?)

Thanks for your help!

Since creating your new shortcode requires a brand new plugin, reading through the plugin basics documentation might get you on the right foot.

In your plugin, add_shortcode goes in the plugin’s setUp function. Here’s what it looks like in the ShortcodeCarousel plugin

    public function setUp()
        add_shortcode('carousel', array('ShortcodeCarouselPlugin', 'carousel'));
        add_shortcode('featured_carousel', array('ShortcodeCarouselPlugin', 'featuredCarousel'));
        add_shortcode('recent_carousel', array('ShortcodeCarouselPlugin', 'recentCarousel'));

Thanks for the confirmation and the help - that’s actually what I was already doing. (I was just confused as to whether or not that is what I was supposed to be doing based on the previous response.)