• Hello,
    Apologies if this is in the wrong forum section.

    I am trying to automatically refresh a function for a number calculation, I’ve added a shortcode to this function for ease of adding to a page. I want this function to update after X amount of seconds rather than reloading entire page.

    The current way I’m trying this is using .load(‘/file.php’) wrapped in a set interval function. This then loads a php file containing echo do_shortcode(‘[shortcodename]’);
    The refresh function works great when loading in the .php file containing basic text as a test, however adding the do_shortcode function flags using fatal error: undefined function do_shortcode.

    What am I missing to make sure do_shortcode function is recognised ?

    Thanks in advance! Still a novice when it comes to wordpress

Viewing 2 replies - 1 through 2 (of 2 total)
  • @jakeransom1504

    If I’m understanding this correctly you are using a jQuery .load() call to reference a PHP file but in that file you want to run do_shortcode().

    It seems quite likely that your ‘file.php’ is operating outside of WordPress so you need to find a way in include all of the WordPress functions you require.

    do_shortcode() is located in wp-includes/shortcodes.php so in your file.php file you may need something like this before your first call to do_shortcode():

    if ( ! function_exists( 'do_shortcode' ) ) {
    	require 'wp-includes/shortcodes.php';
    }
    Moderator bcworkz

    (@bcworkz)

    Requiring shortcodes.php might work in this instance, but generally speaking it’s poor practice. Many WP files are dependent upon many others and resolving dependencies is often difficult. IMO you really ought to load the entire WP environment if you want to use WP resources at all. There are really only a few ways to properly do this. Either make a custom page template out of your file, add a WP page that uses it, and request that page; or send requests through admin-ajax.php or admin-post.php.

    admin-ajax.php of course requires JS Ajax techniques on the requesting page. admin-post.php is similar in that you add an action to have your PHP execute, but no JS is required. Despite its name, GET request will also work. I think admin-post.php is really useful for executing custom code, yet it’s little documented. There’s really not that much to it. Reading the file’s source might be enough of an explanation.

    It may seem like overkill in some cases to load all of WP just to use a particular function. That may well be. Consider if your code really needs WP resources. If you can get by without WP resources without too much effort then you should do so. Otherwise load the whole thing, it’s not as an onerous load on servers as it might seem.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Using do_shortcode()’ is closed to new replies.