• Hello, I need a wordpress shortcode. The shortcode will work as follows: [get-button id=”1″] will return a template when used. I will use this shortcode in certain places on my page. For example, under a heading after the content, and again under a heading after the image. But the ids I call will be different everywhere. Like [get-button id=”4″]. What I want here is to collect all the ids and send a request to an API, then add my buttons to the places where I added the shortcode with the response returned from the API. How can I do that? For each button, I can send a request to an API and return a template based on the response. However, I will use at least 10 button shortcodes on my page, so I do not want to request the API 10 times. Thank you in advance for your answers.

Viewing 1 replies (of 1 total)
  • Moderator threadi

    (@threadi)

    Your request is less about a shortcode, you rather need a complete concept to realise your idea. I would suggest the following procedure:

    Use Custom Post Types (CPT) to save the data from each API request as data records. To do this, you must first create the CPT: https://developer.ww.wp.xz.cn/reference/functions/register_post_type/
    Then you have to implement the API request, at first it is probably enough if you build a button in the backend that queries all API data when clicked. For the query, I would recommend using WordPress’ own functions for HTTP requests, e.g. https://developer.ww.wp.xz.cn/reference/functions/wp_remote_get/ (or post, depending on what the API requires). You then create the queried data as data records of the CPT, i.e. via: https://developer.ww.wp.xz.cn/reference/functions/wp_insert_post/

    Make sure not to save existing API data again but to update it.

    As a result, you should then have one data record of your CPT for each API request.

    Then define the shortcode. The ID as a parameter then corresponds to an entry in the CPT. You use it to access the data to be displayed, which is stored there, and output it using a template you have designed.

    That would be my suggestion for the procedure.

Viewing 1 replies (of 1 total)

The topic ‘Create WordPress ShortCode’ is closed to new replies.