• Hi! I’m using Ajax Load More to create an infinite scroll of posts and, in each one, I want to use a news ticker (Ditty News Ticker)… And I got a few problems. The first post works fine. When I scroll and ajax injects the other ones, it does not.

    First things first: the problem is not EXACTLY with shortcodes, it is with plugins that can be run using shortcodes; and it happens only on the scroll; directly loaded pages works fine. Shortcodes that are registered in my functions.php WORKS. Plugins that works with shortcodes, does not.

    This is the function I am using to call the ticker.

    <?php
        if (function_exists('ditty_news_ticker'))
        { ditty_news_ticker( 1487 ); }
        ?>

    The problem is: the plugin does not seem to get loaded when the ajax content is loaded. Using this code as a test, you can see that it literally says that the function does not exist.

    <?php
    	if (function_exists('ditty_news_ticker')) {
      		echo 'ditty_news_ticker() exists! Yay!';
      		ditty_news_ticker( 1487 );
    	} else {
      		echo 'ditty_news_ticker() does not exist';
    	}
    	?>

    Using the shortcode (inside the_content or outside, using do_shortcode), all I get is a plaintext version of the shortcode. I tried that filter that is on FAQ and nothing happened, all the same.

    With Shortcodes Ultimate, another plugin, it’s even more bizarre: the shortcode IS executed, but the CSS does not work. It creates the DIVs that are supposed to create columns, but it doesn’t load the CSS that does this.

    All of this is being loaded within the_content and/or the repeater templates.
    Other plugins (like coauthors, for example) works great.

    So, that’s what we have. For some reason, plugins+shortcodes doesn’t work when the ajax inject content for the second time.

    Searching, I found this: https://www.ads-software.com/support/topic/run-shortcode-inside-ajax-request

    This is the most similar issue I found… Maybe that’s a way?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    I agree with the plugin author of your linked forum topic, the problem is an AJAX request is seen by WP as an admin request, and some things just don’t happen with admin requests, like shortcodes registered. It’s nothing specific that WP core is doing AFAIK, more likely plugins are trying to be efficient by not needlessly loading code for back end requests where presumably the code is not needed.

    Unfortunately for you, they are needed. In general, there is no convenient “load_frontend” function like that plugin author suggests for his plugin. Getting shortcodes to work in AJAX in a clean manner, safe from plugin updates would not be pretty. You say Shortcodes Ultimate causes all of your shortcodes to be expanded, but the CSS is not loaded. Getting CSS loaded is a lot easier than getting shortcodes expanded cleanly, so I suggest you stick with this plugin and address the CSS.

    The same admin thing is happening with CSS. Plugins try to be efficient by not loading CSS for admin requests. Commendable, but working against your particular needs. What you can do is enqueue all possible CSS from plugins when any infinite scroll page first loads. Not very efficient, but it’ll do the job. To identify the needed CSS, create a test page that contains all of the shortcodes you want to use. Compare the CSS loaded here against CSS loaded on an infinite scroll page. Create a custom plugin or child theme that enqueues the missing CSS for any infinite scroll page.

    Thread Starter borbs

    (@borbs)

    @bcworkz

    Yeah, the CSS part is easy to overcome. But in the case of the other one, a PHP function?

    Looking into both plugins code, I found the wp_register_, script and style, that both use to, well, register the function and the CSS.

    I’m no programming expert, but I can tell that this makes WordPress know about the plugin, and that’s what makes it work the first time. Is there anyway for me to make the plugin register AGAIN, so WordPress can load it again and again and again?

    Anyways, thank you for your clarifying answer. Really appreciated ??

    Moderator bcworkz

    (@bcworkz)

    What you are imagining happens isn’t really correct even though it accurately explains the symptoms. The plugins are actually loaded for AJAX requests, but WP does not fire the action used to output scripts and CSS because doing so for an AJAX request is useless. AJAX is much too late to load scripts and CSS, it must be done when the page first loads.

    As for the missing PHP function, I couldn’t say without seeing the related code in context, but I’m guessing your code that checks for the function is running too soon, before WP is stable and all plugins are loaded. In the context of your code, the ticker plugin has not yet been loaded, but that does not mean it will not be loaded.

    If you manage to get all other shortcodes working but the ticker still does not work, there is some other issue at play, but it’s not because the plugin did not load. Focus first on getting the other shortcodes working. Deal with the ticker as a separate unrelated issue.

    Thread Starter borbs

    (@borbs)

    You know, a few minutes before I saw your reply, the news ticker dev told me that, testing, he noticed the plugins were loaded, BUT not the external files it needed to load — just like you said.

    For testing reasons, I tried to load those files via functions.php and, guess what? It worked. It doesn’t show anything, since the JS is not being fired again, but, well, we finally could pinpoint exactly the problem, that could help me solve this.

    So just let me get this straight: what I have to do is figure out a way to make the ajax plugin to load after everything is set? Or it’s something that the ajax plugin must do by itself?

    Moderator bcworkz

    (@bcworkz)

    The entire WP environment is essentially one big block of code composed of many pieces. It doesn’t matter what code does what, as long as it gets done somewhere. You are limited by what values are available where, so getting something done is not always that simple, but in theory your plugin could load another plugin’s external files.

    What needs to happen is any external file that may be needed by a shortcode needs to be enqueued before the initial page output so the proper meta tags can appear in the head section. It doesn’t matter whose code does this, as long as it gets done.

    Triggering javascript is another issue. Much javascript executes on page load. If it needs to run after an AJAX request as well, the AJAX handler will need to explicitly call the needed functions since the code that initially ran on page load may not have had the proper resources yet to do its task.

    In fact, code running on page load even though related HTML is not yet in place could cause script errors. Some JS may need to be altered to not run on page load, just load and be available.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Plugins/shortcodes not running with ajax’ is closed to new replies.