0

Automatic short code generation is a technique that I see many plugin authors use but unfortunately, I have yet to come across a tutorial that details this subject so I am hoping that someone in this community will help me understand it clearly as I am struggling to implement it in my own plugin.

The automation process that I am talking about is this: After a user selects/inputs parameters in a plugin's custom post type metabox(es), he or she saves the information and immediately a shortcode is generated and outputted within a custom post column (others also display a message containing the shortocde whereby the user simply copies it and pastes it wherever he or she intended to use it).

For example, in a plugin that displays book information, a user inputs the book name, ISBN number, category under which it falls under etc. After keying in all those details, a shortcode '[book id="1"]' is automatically generated after performing a save/publish action. Keying in details of another book also results in the same action with only the ID changing i.e. it generates '[book id="2"]'

My problem is how this is done. More specifically:

  1. How do authors fuse the functionality of saving metabox values with the functionalities of generating a shortcode?
  2. Within the above process, how are IDs auto generated?

Use the book example above,I am hoping someone can explain to me clearly how the 'automation process is done'. Even though I haven't given a code sample, I feel the above is enough to get an answer(plus it helps focus on the actual problem). However, if you do really need something then I will code up a sample :-)

As a bit of a background:

  1. I know how to create custom post types
  2. I know how to create custom columns for custom post types
  3. I know how to output values into those custom columns
  4. I have an understanding of saving custom values from metaboxes
  5. I know how to create shortcodes(including ones with parameters). The thing now is that I have to type the shortcode (including parameters) manually in the front-end and this is the process I want to automate (i.e. autogenerate shortcode and place it somewhere like custom columns, user simply copy pastes to wherever he/she wants the output to appear)

1 Answer 1

2

I am a little confused as to your question. If you know how to create shortcodes with attributes, then I think the answer to your question is to simply output text with the correct attributes predefined.

For example, just add text into your custom column for the appropriate arguments, such as echo "[book id=" . $post->ID . "]". The shortcode isn't generated in any permanent way, it's just a textual representation of the arguments required to display that specific information. In this example your shortcode would extract the ID argument and select the correct post via it's ID.

7
  • that simple line of code has given me an idea. Just wondering how to create my own unique ID in a manner in which even if an instance is permanently deleted and I add another book detail, it doesn't use the ID of the one that was deleted.
    – nelson
    Commented Feb 20, 2015 at 13:21
  • If the items you are referencing are rows in one of the databases, such as posts, then you can use the post ID in the manner described here. The ID is assigned as the post is added to the post table and is guaranteed to be unique.
    – KenB
    Commented Feb 20, 2015 at 14:32
  • WordPress developers aren't automatically generating shortcodes when posts are created or edited. The shortcode already exists but it requires attributes about the post you're editing, for example the post ID or one of the post custom fields, like the ISBN number, so once those values are generated, the developer displays what the shortcode would look like with the attributes: [book ID="1" ISBN="1234567789"] Commented Feb 20, 2015 at 14:42
  • @RachelCarden and Ken . Alright. Let me try and work it out and then I will get back to you once I have something concrete
    – nelson
    Commented Feb 20, 2015 at 16:47
  • 1
    @nelson Check out the second example on the add_shortcode() page of the codex. The $atts variable represents the attributes you passed to your shortcode. So lets say you place "[book id="3" ISBN="12345"]" in your content. "id" and "ISBN" are array keys that will be stored in $atts so then you can use them in your function code. You can use them however you like. They do not have to be printed to the page. Commented Feb 25, 2015 at 4:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.