Support Forum
Support › General › Creating a Citation Insert Plugin that Uses Shortcodes to Generate List of CTPsResolved
Hello,
I am testing metabox during the 14-day trial period, and I'm not yet sure if it will give me all of the functionality I desire given my current skillset. I am working on a site that needs to reference scientific literature. I have used MB Builder to create a CPT called "Publications," and I used MB Views to create an archive page that outputs all publications in a citation list.
The next step that I would like is to be able to create a shortcode that I can use to insert a reference anywhere on the site and then automatically generate a reference list at the bottom of the post/page. Ideally, I would have a shortcode that looked something like [cite ref_id=""] where ref_id is unique to every publication post. Then, this shortcode would output a number corresponding to the citations position on the page (the first ref is number one, the second is number 2, and if you use the same reference twice then both instances just use the first number). Then, at the end of the page/post, I would like to insert a numbered list with the publication citations written out in the order they were used in.
Is it possible to do all of this with metabox/html/css? I've been looking for resources to help me, but so far I don't have enough info to tell.
The most promising links I have found:
https://support.metabox.io/topic/add-attributes-to-mb-views-shortcode-4/
https://www.sitepoint.com/accessible-footnotes-css/
Hi,
Thank you for using our plugin!
Yes, it is possible with MB Views. For example,
{% set post = mb.get_post( my_post_id ) %}
{{ post.post_title }}
my_post_id
in the code, then we can use the shortcode of View and pass a post ID to the shortcode. See more in the documentation https://docs.metabox.io/extensions/mb-views/#custom-data.[ mbv id="2308" my_post_id=1783 ]
Hi Long,
Thanks for those helpful links. I will give that a shot and report back once I have a better sense of how things work.
Thanks,
Graceson
So I have now had a chance to try test out building this shortcode. I decided to try using php in a custom plugin so that I could give the shortcode my own name. However, I can't figure out how to solve a couple problems.
I am planning on having the shortcode look as follows [ref pub_id=399], where pub_id is the post ID for the publication post that I am trying to cite.
The first issue I am having is that I would like the shortcode to return a number every time it is called. The first instance should return 1, the second time it should return 2, and so on. However, if the same publication post is called multiple times, I would like all of those shortcodes to return the same value.
The second issue that I can't figure out is how to output my reference list at the end of the page. My goal is that at the end of the_content WordPress will automatically insert a numbered list of all the publications I am trying to cite. I think I know how to do this, if I can only figure out a good way to determining all of the instances of the shortcode used on the page and what order they were used in.
My initial though of how to handle these two issues was to write the shortcode function to set up an array and then add the pub_id value to that array each time the shortcode is used. I would then use that array in another function hooked to the_content. I tried using a global variable to do this, but I can't seem to get the values to be passed. After some research, it looks like shortcodes are only meant to return strings, and global variables are discouraged.
The next thing idea I had was to write a function that looks for all of the times the shortcode has been used in the_content and then use that information to append the citations into the-content. I found this question which seems very related: https://stackoverflow.com/questions/56627970/wordpress-get-all-shortcodes-from-content-field. I haven't tried this method yet, but it should probably work for creating the citation list at the end of the post. The issue then becomes, how do I get the shortcode to return the proper numbers at each location as described above?
I am a php and wordpress beginner, so I think that what would be most helpful is to get suggestions on high-level approaches to getting the functionality I desire. Basically, the end goal is to get LaTeX-like citing functionality within WordPress. I haven't found any existing plugins that do this the way I would like, and so this shortcode approach seemed like a good idea to me, but maybe there is another option that I haven't considered at all.