celtic-project / wordpress-lti-platform

Plugin to enable WordPress to act as an LTI Platform
GNU General Public License v3.0
2 stars 1 forks source link

Add URL Only Output Option to Shortcode in WordPress LTI Platform #7

Closed abdolnabi closed 1 month ago

abdolnabi commented 3 months ago

Issue Description:

In the Celtic project under the wordpress-lti-platform, the current shortcode implementation for displaying LTI tool links and titles ([lti-platform tool=saltire id=iofkljfg target='']) only supports a limited set of target options: "New window", "popup", "iframe", and "embed". There is a common need, especially when integrating with custom elements, to use only the URL of the LTI tools without embedding or opening them in new windows.

Feature Request:

It would be highly beneficial to add an "URL only" output option to the shortcode. This feature would allow developers to retrieve just the URL of an LTI tool, enabling more flexible integration possibilities, such as linking directly from custom buttons or other UI elements without altering the behavior dictated by the target attribute.

Potential Benefits:

Example Use Case:

A developer wants to create a custom button on their site that links directly to an LTI tool without opening it within an iframe or a new window. Specifically, using a WordPress page builder, the developer plans to incorporate custom elements or buttons that require direct URLs to LTI tools for seamless integration. Currently, this is not possible with the existing shortcode options which limit output to embedding methods like iframe or popup windows. The addition of a "URL only" option would enable more streamlined and custom integrations within WordPress environments.

spvickers commented 3 months ago

Thanks for your contribution. As I understand it, what you are looking for is an option which allows a shortcode to be added which is replaced only by the URL to open the tool, and omits any surrounding HTML to create a hyperlink within the WordPress page. If that is correct, then I would not consider this to be a target option which is part of the LTI spec. It seems to me that you merely need an attribute which renders the shortcode as a simple URL. For example, do you think something like the following would solve your issue?

[lti-platform tool=saltire id=iofkljfg urlonly]

which would be replaced with a URL like:

https://my.wordpress.site/?lti-platform&post=[x]&id=[id]

where [x] is the ID of the post containing the shortcode and [id] is the ID of the shortcode link. This link would generate an launch message to the LTI tool (with the setting for the presentation_document_target being included in the message). This assumes that the location where the LTI tool is opened is actually controlled by the HTML code in which the shortcode is embedded, rather than this plugin, so it is possible that the parameter included in the LTI message may not accurately reflect the actual location of the tool. It would rely on the tool or shortcode being consistently configured.

abdolnabi commented 3 months ago

Thanks for the explanation, my requirement is to find a url of the lti course’s (my lti tools) which is working on any page from anywhere, just imagine this scenario, I have some blog posts with this course’s shortcode in custom parameters, and I need to display blog posts on the blog page with the link to the course as a button below each blog post fetching their link from the shortcode stored as a custom parameter, when it is fetching the shortcode the page id and shortcode id is not aligned to the blog page and only works on that specific blog post. Now I’m trying to find a way to make the course urls working on everywhere not only on the main blog post the shortcode defined on it.

abdolnabi commented 3 months ago

Also I tried the shortcode the way you mentioned above like [lti-platform tool=saltire id=iofkljfg urlonly]but it will replace the shortcode with an href tag like below:

`<a href="https://my.wordpress.site?lti-platform&post=[x]&id=[id]" title="Launch [tool_name] tool" target="_blank">[tool_name]</a>`
spvickers commented 3 months ago

Sorry if I was not clear. My previous response was merely trying to outline a proposed solution to meet your needs; it was not intended to describe an existing solution. Do you think my proposal is suitable?

spvickers commented 3 months ago

Some questions about your response:

my requirement is to find a url of the lti course’s (my lti tools)

What do you mean by an LTI course? Does this relate to a page within an LTI tool?

which is working on any page from anywhere

What types of page are you referring to here? More than just those stored in the WordPress posts table?

this course’s shortcode in custom parameters

Are you referring to LTI custom parameters, or a WordPress element?

abdolnabi commented 3 months ago

Thanks very much for your clarifications and detailed questions, please find my responses below: 1. My requirement is to find a URL of the LTI course’s (my LTI tools)

Response: An LTI course in this context refers to a specific educational resource or set of activities from OpenLearning, which is integrated into our WordPress site using the LTI 1.3 standard. The URL of the LTI course is essentially the target URL provided by OpenLearning that allows for LTI-based connections. This URL is used to launch the LTI tool from WordPress and typically contains the endpoint that handles LTI launch requests within the OpenLearning platform.

2. What do you mean by an LTI course? Does this relate to a page within an LTI tool?

Response: Yes, by an LTI course, I am referring to an OpenLearning course that is integrated into our WordPress site via an LTI tool. It does not directly relate to a traditional web page but to a course hosted on OpenLearning, which is made accessible within our WordPress environment through LTI integration. The courses are represented as external content embedded or linked from our WordPress posts, enabling direct interaction with OpenLearning’s LTI-compliant content.

3. Which is working on any page from anywhere. What types of page are you referring to here? More than just those stored in the WordPress posts table?

Response: The phrase “working on any page from anywhere” refers to the flexibility of placing LTI tool links or buttons on various parts of our WordPress site, not limited to the individual course posts. While each course has its own dedicated post, we also have a centralized course list page that aggregates these courses. The LTI links or start course buttons can be included on this list page, other posts, pages, or even widgets throughout the site, facilitating access to the LTI courses from various navigational points within the WordPress environment.

4. This course’s shortcode in custom parameters. Are you referring to LTI custom parameters, or a WordPress element?

Response: In this context, the reference to “shortcode in custom parameters” pertains to a WordPress element — specifically, a shortcode that we use to embed or link to the LTI course. This shortcode is designed to utilize custom parameters that are stored within each course’s WordPress post as metadata. These parameters might include identifiers, keys, or other course-specific data that configure the LTI link to launch the corresponding course in OpenLearning. The shortcode facilitates the dynamic generation of these links based on the metadata stored in each course’s post, ensuring that the correct course is launched via the LTI integration.

spvickers commented 3 months ago

Thanks for the further details. Some follow-up questions:

  1. I am still not clear on whether any of the pages on which you wish to place a shortcode are not stored within the posts table. For example, is your "centralized course list page" a bespoke PHP/HTML page? (I have no experience of support for shortcodes outside a WordPrress page/post.)
  2. The LTI launch message needs to contain some specific values to identify the destination within the LTI tool (OpenLearning). If these values are saved as metadata within the WordPress page for the course, how would they be found by a link on the centralized course list page, for example?
  3. I assume the course-specific values are to be passed to the LTI tool (OpenLearning) as LTI custom parameters in the LTI message; or are they to be passed as query parameters on the LTI launch URL?
  4. Is there any reason why the course-specific values cannot be specified within the shortcode rather than as metadata in the course's post?
abdolnabi commented 3 months ago

Here's your response formatted in Markdown:


Apologies for the delayed response, and thank you for your patience. I’ve reviewed our setup and would like to clarify the following points regarding the implementation of the LTI launch and shortcode integration:

  1. Page Storage:

    • All pages where I wish to place a shortcode are indeed stored within the WordPress posts table. This should simplify integration as they are standard WordPress posts.
  2. LTI Launch Metadata:

    • I intend to store an LTI shortcode that contains the URL of the LTI tool as an attribute of the posts. This will allow me to easily use these URLs in buttons that launch the courses on OpenLearning.
  3. Passing Course-Specific Values:

    • I am currently passing the class_id attribute to OpenLearning as part of the custom parameters in the LTI tool. This method aligns with our need to specify certain course details directly within the launch parameters.
  4. Design and Usability:

    • The reason for having the link to each course outside of the post content is due to our design scenario. We plan to use a page builder to loop over course posts and render certain attributes on the course list page based on each course/post. This approach is more manageable and flexible for our needs than embedding everything directly inside the post content.

I hope this clarifies our approach and the requirements. I look forward to your guidance on how we can best implement these functionalities within the current framework of the plugin.

Thank you for your support.


spvickers commented 3 months ago

Thanks for the clarifications. It sounds like there is no need to include metadata from the WordPress page in the LTI message, so your original request of merely being able to render the shortcode as just the URL needed to launch the LTI tool so that it can be included in bespoke HTML is sufficient for your needs. So my original proposal to add an attribute to the shortcode of something like urlonly would support this requirement.

spvickers commented 3 months ago

To address your issue, my proposal is to add a new optional attribute to the shortcode named "output". The attribute can have a value of "url", "title", "width" or "height". When present the shortcode will be replaced with just the value of the specified item with no additional HTML. Any double quotes will be replaced with """ to allow the value to be included within an HTML tag. Does that sound like it will address your needs?

abdolnabi commented 3 months ago

I’m not sure if I get it correctly, can you give an example of the shortcode output with those attributes you’ve mentioned earlier?

spvickers commented 3 months ago

Sure. Updating my example from above, the shortcode would be:

[lti-platform tool=saltire id=iofkljfg output=url][/lti-platform]

which would render as:

https://my.wordpress.site/?lti-platform&post=[x]&id=[id]

where [x] is the ID of the post containing the shortcode and [id] is the ID of the shortcode link.

A shortcode of:

[lti-platform tool=saltire output=title][/lti-platform]

would render as:

Launch saltire tool

Note that the id attribute is not required when the output attribute is not set to url. The following shortcode would render a title of "Launch LTI tool" but is rather redundant as it could just be replaced with the value itself:

[lti-platform tool=saltire title="Launch LTI tool" output=title][/lti-platform]

A shortcode of:

[lti-platform tool=saltire output=width][/lti-platform]

would render as:

750

assuming this is the width specified in the tool's configuration.

spvickers commented 2 months ago

NB previous comment has been edited to add closing shortcode tags.

abdolnabi commented 2 months ago

Given our ongoing discussion, the solution seems viable. However, I'm curious about a couple of scenarios concerning the WordPress page context, particularly for a specific post type like 'portfolios' which uses the posts table but doesn't support Gutenberg—only the classic editor is available.

These questions aim to ensure clarity on how the shortcodes interact with different editors and across various display contexts. Looking forward to your insights on this.

spvickers commented 2 months ago

I have not tried adding the LTI Platform shortcodes to portfolio pages, but since they can be typed manually (as in my examples above), I see no reason why they should not work, assuming that WordPress parses these pages for shortcodes when displaying them. Each shortcode to launch a tool must have a unique value for its id attribute, so displaying multiple links from multiple pages should work. Perhaps you can try it out with the existing shortcode functionality to add LTI tool launch links and see if it does?

abdolnabi commented 2 months ago

That's a fantastic discovery! Utilizing custom parameters within the shortcode to pass the page_id and differentiate LTI tools for specific courses sounds like an effective solution. Here’s how I implemented it based on your insight:

[lti-platform tool="xxx" id="xxyzxx" custom="page_id=xxyyzz; class_id=6668...367845"]

This approach enables the use of a single LTI tool across multiple courses by specifying unique class_idsfor each, ensuring the correct course context is maintained when navigating from the general course link to the specific course page on the LTI platform.

abdolnabi commented 2 months ago

It seems we might be encountering a specific challenge with how the shortcode interacts with custom fields. When the shortcode is embedded in a custom field of WordPress posts rather than directly in the post content, it appears that the shortcode does not function properly when called from a list on another page, resulting in the "No LTI tools found." message.

spvickers commented 2 months ago

Two comments on your last two messafes:

  1. The id value should not need to be repeated as a custom parameter as it is used as the LTI resource_link_id parameter which is one of the few required parameters in an LTI launch message.
  2. The plugin has not been designed to have its shortcodes entered outside of a page content - have you experienced other shortcodes being used elsewhere in WordPress?
spvickers commented 2 months ago

Just to make sure I am understanding your requirements correctly:

  1. You are wanting to have a link (e.g. a button) on a page (whose contents are stored in the wp_posts table) which will launch an LTI tool.
  2. You wish to be able to place the shortcode which helps to achieve this within the custom fields of the page, rather than within the body of the page.
  3. A script (e.g. a theme) will extract the value of this custom field when the page is rendered.

Does this sound correct? If so, are you able to provide an example of the value(s) which might be entered in a page's custom field(s) and the HTML which your script might render from it/them?

abdolnabi commented 2 months ago

That sounds correct. For the third point, I won't be writing a script as I'm utilizing the Cornerstone WordPress page builder, which can extract custom fields from the posts/pages. Using the theme and page builder's capabilities, I've set up the custom field like this: [lti-platform tool=i2d33d5c6d7d id=da3602335 target=urlonly] This is then rendered on the page as a button link for one of the courses. When users click on "see details", they see the following: image

spvickers commented 2 months ago

Thanks for the response. It sounds like my proposal will work for you, just changing the shortcode from your sample to:

[lti-platform tool=i2d33d5c6d7d id=da3602335 output=url] 

Does your code currently work without adding a closing [/lti-platform] tag? I suspect it might as this is the only contents of the custom field.

So my only remaining concern is how to prevent more than one link being added to a page with the same resource link ID parameter (id attribute), which is a requirement of the LTI spec. Is there anything within the Cornerstone WordPress page builder which would prevent the contents of the custom field from being used more than once?

spvickers commented 2 months ago

One further issue to be considered. The shortcode is currently replaced with a link to a URL of the following form:

https://my.wordpress.site/?lti-platform&post=[x]&id=[id]

where [x] is the ID of the post containing the shortcode and [id] is the ID of the shortcode link. The post ID is needed so that the shortcode can be located when a launch is processed (so its attribute values can be accessed). If your use case is assembling elements from different posts (pages) onto a single page, the question in my mind is which post ID is passed to the shortcode method when it is being rendered. Is it the post ID where the shortcode is specified (which is what is needed) or is it the ID of the post on which the post is being included? If you can inspect the URL generated on a sample page then you should be able to tell from the post parameter included in the launch URL generated by this plugin. If it is for the page being displayed then the issue will be how can the ID of the post from which the shortcode comes be found by the plugin.

abdolnabi commented 1 month ago

Sorry for the late response — I've been quite swamped over the last couple of weeks and just realised I hadn’t yet replied to your latest question. Regarding your query about the shortcode's behaviour, the post ID that gets passed is indeed the one containing the shortcode, not the ID of the page where the posts are displayed. Let me know if there's anything else you need clarity on!

spvickers commented 1 month ago

Excellent, thanks for confirming this, it should make life easier. All I now need to do is add code to look for shortcodes in a post's custom fields.

On reflection, I am also planning to change the output=url attribute to output=href to avoid possible confusion with the LTI message URL.

spvickers commented 1 month ago

I believe the latest commits to this repo add support for your use case. Are you able to check it out before I make a formal release? Use a shortcode like the following:

[lti-platform tool=saltire id=iofkljfg output=href][/lti-platform]

Note that the support for checking custom fields for shortcodes must be enabled on the plugin's default settings page.

abdolnabi commented 1 month ago

Sorry for the late reply; I was pretty busy last week. I checked out your latest commit, and it was working as expected. Thanks very much.

spvickers commented 1 month ago

Excellent, thanks for confirming this. I will make a new release for these changes soon.