What’s the earliest point I can get the queried object ID?

I’m writing a supplement plugin to WordPress Mobile Pack because I would like to use the default theme on pages that are assigned a certain page template. My plugin will only deal with the mobile switcher portion of that plugin, so the other components can be ignored (I think).

Looking at the source for wpmp_switcher.php, all the add_action and add_filter calls of relevance are not in a function — they are executed when the file is included as so:

add_action('init', 'wpmp_switcher_init');
add_action('admin_menu', 'wpmp_switcher_admin_menu');
add_action('wp_footer', 'wpmp_switcher_wp_footer');
add_filter('stylesheet', 'wpmp_switcher_stylesheet');
add_filter('template', 'wpmp_switcher_template');
add_filter('option_home', 'wpmp_switcher_option_home_siteurl');
add_filter('option_siteurl', 'wpmp_switcher_option_home_siteurl');

I have removed the actions/filters associated with what make the WPMP plugin tick by using the plugins_loaded hook:

function remove_all_wpmp_switchers() {
  remove_action('init', 'wpmp_switcher_init');
  remove_action('admin_menu', 'wpmp_switcher_admin_menu');
  remove_action('wp_footer', 'wpmp_switcher_wp_footer');
  remove_filter('stylesheet', 'wpmp_switcher_stylesheet');
  remove_filter('template', 'wpmp_switcher_template');
  remove_filter('option_home', 'wpmp_switcher_option_home_siteurl');
  remove_filter('option_siteurl', 'wpmp_switcher_option_home_siteurl');
}
add_action('plugins_loaded', 'remove_all_wpmp_switchers');

So far, so good.

Now I would like to use the following code to add back the actions/filters when my template is not being used:

function wpmp_switcher_exclusions_init(WP_Query $wp_query) {
  $template_file_name = get_post_meta($wp_query->get_queried_object_id(),
                                      '_wp_page_template', true);
  if ($template_file_name != 'my-responsive-template.php') {
    wpmp_switcher_init();
    add_action('admin_menu', 'wpmp_switcher_admin_menu');
    add_action('wp_footer', 'wpmp_switcher_wp_footer');
    add_filter('stylesheet', 'wpmp_switcher_stylesheet');
    add_filter('template', 'wpmp_switcher_template');
    add_filter('option_home', 'wpmp_switcher_option_home_siteurl');
    add_filter('option_siteurl', 'wpmp_switcher_option_home_siteurl');
  }
}
add_action('parse_query', 'wpmp_switcher_exclusions_init');

The problem is that, even as early as the parse_query hook is, the wpmp_switcher_init function is called too late. The functions.php files for both the desktop and mobile templates attempt to load, causing fatal errors due to re-defined functions.

If the template can be retrieved earlier than this, I’m not sure how to do it. It looks like I need to somehow run my code using the setup_theme hook. If I can figure out how to do this, I will likely only need the one function in my plugin since setup_theme is called before init.

Any help with this is VERY appreciated!

2 Answers
2

A more compact and WordPress-ish way, using url_to_postid():

function wpmp_switcher_exclusions_init() {
  $pid = url_to_postid( home_url( add_query_arg( array() ) ) );
  if (
    (int) $pid > 0 &&
    get_post_meta( $pid, '_wp_page_template', TRUE ) === 'my-responsive-template.php'
   ) {
    remove_action('init', 'wpmp_switcher_init');
    remove_action('admin_menu', 'wpmp_switcher_admin_menu');
    remove_action('wp_footer', 'wpmp_switcher_wp_footer');
    remove_filter('stylesheet', 'wpmp_switcher_stylesheet');
    remove_filter('template', 'wpmp_switcher_template');
    remove_filter('option_home', 'wpmp_switcher_option_home_siteurl');
    remove_filter('option_siteurl', 'wpmp_switcher_option_home_siteurl');
  }
}

add_action( 'setup_theme', 'wpmp_switcher_exclusions_init' );

Leave a Comment