WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Access to shortcode values in separate ajax function call (3 posts)

  1. Thimo Grauerholz
    Member
    Posted 1 year ago #

    Hi,

    I'm writing a plugin which has to access a different api using authentication. So I need the login creditials provided in the shortcode of the plugin.
    The ajax call has to authenticate at the webapi itself with the provided login creditials.

    How to do this without saving the information in a session cookie or in the ajax file call (url) string? (The ajax call is in the same class)
    Do I have to read the shortcode again (how?) or is there another solution?

    Your help is highly appreciated
    Thimo

  2. bcworkz
    Member
    Posted 1 year ago #

    You can execute the shortcode again with do_shortcode(). I would think you could setup the authentication call as part of the shortcode handler function, but I'm not privy to what your process is. You could store credentials in user_meta or options, encrypting data as needed. Another possibility is the Transients API if you do not want to leave a permanent record, though it's permanent if it's part of the shortcode attributes in any case.

    As far as getting the credentials to to jQuery to do an AJAX call, you really only have two choices. Echo the variable assignments inside <script> tags, or use wp_localize_script().

  3. Thimo Grauerholz
    Member
    Posted 1 year ago #

    Last night I got my first working solution. I read the shortcode again...

    // get the page id
    $pageData = get_page(intval($_GET['pageid']));
    // get the page content
    $pageContent = $pageData->post_content;
    // parse the shortcode again in content
    $sc = shortcode_parse_atts(str_replace("[SHORTCODE",'',str_replace("]","",$pageContent)));

Topic Closed

This topic has been closed to new replies.

About this Topic