Run shortcode inside AJAX request
-
I’m using this plugin and it is really useful!
If I load a page normally the shortcode is interpreted as if I load the page with an ajax request not display anything.
How can I fix the problem?
Thanks in advance
-
Hi,
thanks for your post, and sorry for the trouble.
The reason for this most likely is that TablePress only registers the Shortcode on the frontend (i.e. when
is_admin()isfalsein the code). The AJAX request probably goes throughadmin-ajax.php, so that the Shortcode is not registered.
To change that, please try adding this to the “functions.php” of your theme:add_action( 'init', 'load_tablepress_in_the_admin', 11 ) ); function load_tablepress_in_the_admin() { if ( is_admin() && defined( 'DOING_AJAX' ) && DOING_AJAX ) { TablePress::$controller = TablePress::load_controller( 'admin_ajax' ); } }Regards,
TobiasHi Tobias.
Thanks for your quick response.
I added the function you gave me but unfortunately the ajax request returns the shortcode: [table id=1 /]
I have tried different solutions but unfortunately I did not get any results.
Any idea?
Thank you again for your reply!
Hi,
ok, that’s strange.
Have you tried other Shortcodes, to find out if this is maybe some other problem? For example, it might be possible that the AJAX request is simply not evaluating Shortcodes because they are not run throughdo_shortcodes()(which is usually tied to thethe_contentfilter that is evaluated during a regular page view). This is likely the case if the AJAX request simply returns$post->post_contentfrom the database.Regards,
TobiasYes, I have tried other shortcodes. If the shortcodes are included in the theme are interpreted.
I tried to install other plugins and inserting the shortcode get the same result
Hi,
with “get the same result”, you mean that the Shortcode is not interpreted, but shown as plain text?
Then, this is likely an issue in the theme, and you might want to contact the theme developers.Regards,
TobiasHi.
Only plugins’s shortcodes are displayed as text. The shortcodes’s theme are interpreted correctly.
The theme I’m using is the wordpress default theme
Hi,
ok, and how is that AJAX request working then? Are you using a plugin for that?
Regards,
TobiasHi.
Thanks for your patience!
I installed a fresh copy of wordpress. I’m using twentyfourteen theme.
This is the code entered in the functions.php file: http://pastebin.com/w7BBgS32
In the functions.php file I added a simple shortcode.
This is the javascript code: http://pastebin.com/xSnupVCZ
I’ve installed tablepress plugin. I created a test table
I created a test page. I entered my shortcode and shortcode generated by plugin
When I load page my shortcode is interpreted but the plugin’s shortcode no.
Need more details?
Thanks
Hi,
ah, I just noted a mistake in my code… Sorry.
Please try replacingTablePress::$controller = TablePress::load_controller( 'admin_ajax' );with
TablePress::load_controller( 'frontend' );exactly like that.
Regards,
TobiasThank you! Now it works!
There is still one small thing: your plugin load javascript in the footer. With ajax javascript are not loaded.
Is there a way to load javascript with the ajax requests?
Thank you very much! You are great!
Hi,
great to hear that this works! 🙂
And the JavaScript is indeed a problem here, but I don’t really see a solution for that. Injecting JavaScript as the result of an AJAX call is tricky (because of security issues that are implied), and I don’t know a standard solution here, sorry.
Regards,
TobiasThanks for your help!
For javascript I’ll try to find a solution.
Have a nice day
Hi,
no problem, you are very welcome! 🙂 Always glad when I can help!
Best wishes,
TobiasP.S.: In case you haven’t, please rate TablePress here in the plugin directory. Thanks!
The topic ‘Run shortcode inside AJAX request’ is closed to new replies.