• When I manually enter a shortcode into a column text object in the editor and then refresh the page my shortcode works perfectly as expected. However, when I programatically set the text of the same column text object using JQuery with the SAME shortcode string the shortcode does not execute and is merely echoed to the screen.

    The code below allows a user to move across a table and select a row then detect the contents of a cell on that row. The cell contents are then added to a string variable ($trendingstock) to create the shortcode. The newly created shortcode string is then inserted into the column text object (.trendingstockchart.text). The shortcode appears as a correctly formated shortcode but does not execute. It is merely echoes to the column text object.

    Anyone have thoughts on how can I get the shortcode to execute programatically?

    jQuery(document).ready(function($){
        $( '.tablepress-id-7' ).on( 'click', 'tr', function() {   
        var currentRow=$(this).closest("tr");          // get closest row number clicked
        var $symbol=currentRow.find("td:eq(0)").text();        // get symbol in first column of current row
        var $trendingstock = '[finviz ticker= ' +$symbol +']';      // build a shortcode string with symbol selected
        alert("The symbol selected was: " + $symbol + " and the shortcode is " + $trendingstock );     // for debug purposes only
        $('.trendingstockchart').text ($trendingstock);      // set column text object value to shortcode string
        <strong>$('.trendingstockchart').refresh();       // refresh to cause the shortcode to execute --- FAILS HERE !!</strong>
        });
    });

    Thanks,
    Gary

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    The shortcode is executed at the PHP level, not in the browser. When you add a shortcode via jQuery, it’s being added in the browser. Your approach using jQuery to build the shortcode is not going to work. You need to figure a way to do this via PHP *before* the page contents are sent to the browser.

    Thread Starter gamattox

    (@gamattox)

    Hi Steven,

    Your answer is rather hard to accept. You are saying WordPress is unable to create dynamic content that uses ShortCodes. So shortcodes are essentially just a way to hardcode functionality and cannot be changed interactively?

    Have you considered an AJAX solution that refreshes only the DIV that contains the modified ShortCode? Do you have any experience with such a solution? What about a plugin solution or a knowledgebase of code snippet solutions I could review? Seems like there is a solution somewhere, I just need some thoughts on how to approach a solution that has worked for others.

    I need to set the parameter of a shortcode based on a user’s selection from a table. Surely there is a code snippet out there to do this?

    Thanks for your thoughts,
    Gary

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Shortcodes are now more or less the “old way” of doing things as they’re being replaced by blocks, so I don’t expect much in the way of additional support for shortcodes.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Shortcode echoes but doesn’t execute’ is closed to new replies.