• Mark

    (@delayedinsanity)


    Just out of curiousity, what’s everybody’s personal opinion on the best way to dynamically load objects within a WordPress site?

    There’s already a hefty amount of information being loaded every time a page is accessed, and while I’m sure not only my server but WordPress itself can handle tacking on quite a bit more before it started to visibly lag, I would prefer to design my site with the thought in mind that if it’s not immediately necessary, it shouldn’t be resident in memory.

    My first attempt seemed to me to be the logical choice, but it failed immediately;

    if (is_page('slug')) {
    	include (TEMPLATEPATH.'/lib/codex/theme/theme_codex.php');
    	add_shortcode('theme_codex', array($theme_codex, 'shortcode_func'));
    }

    To make a long story short I’m building a database driven codex of available themes for my MU site. I figured using is_page to make sure I was where the code was necessary before loading it would work right out of the box, but it doesn’t. Not even with plain old is_page().

    While I go try to think of the next most obvious answer, I was wondering what your methods were.

Viewing 4 replies - 1 through 4 (of 4 total)
  • These both work for me in a page template.

    echo  (is_page('about')) ? 'This is the about page.' : 'This is not the about page.';
    echo 'This is the value of the page query_var: ' . get_query_var('page');
    Thread Starter Mark

    (@delayedinsanity)

    Yeah the problem seems to be that while the conditional functions are available from within functions.php they can’t seem to figure out what page is being loaded and therefore ultimately return false.

    At what stage is functions.php being loaded that it’s unaware of what page we’re actually on?

    I put this in the WordPress Default theme’s function.php:

    function my_conditionals() {
    echo  (is_page('about')) ? 'this is the about page' : 'this is not the about page';
    }

    And in wp-content/themes/default/page.php

    my_conditionals();

    And it worked when I hit any page.

    Thread Starter Mark

    (@delayedinsanity)

    That’s a snazzy solution, but still doesn’t seem to work with add_shortcode().

    function init_codex ()
    {
    	if (is_page('theme-codex')) {
    		include (TEMPLATEPATH.'/lib/codex/theme/theme_codex.php');
    		add_shortcode('theme_codex', array($kats_theme_codex, 'shortcode_func'));
    	}
    }

    I even tried this function as a plugin, and no go there either. It won’t parse the shortcode if it’s added this way.

    Looks like you can’t use the built in functions to do this, which is okay. The alternate solution is fairly eloquent in it’s simplicity though I suppose;

    if (strpos($_SERVER['REQUEST_URI'], 'theme-codex')) {
    	include (TEMPLATEPATH.'/lib/codex/theme/theme_codex.php');
    	add_shortcode('theme_codex', array($kats_theme_codex, 'shortcode_func'));
    }

    Which seems to work just fine. 🙂

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Best way to dynamically load with WordPress’ is closed to new replies.