Forums

Best way to dynamically load with Wordpress (5 posts)

  1. delayedinsanity
    Member
    Posted 4 months ago #

    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.

  2. MichaelH
    moderator
    Posted 4 months ago #

    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');
  3. delayedinsanity
    Member
    Posted 4 months ago #

    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?

  4. MichaelH
    moderator
    Posted 4 months ago #

    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.

  5. delayedinsanity
    Member
    Posted 4 months ago #

    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. :)

Reply

You must log in to post.

About this Topic