Skip to content

Instantly share code, notes, and snippets.

@amekusa
Last active July 27, 2016 06:44
Show Gist options
  • Select an option

  • Save amekusa/38eab238b4eece375bcffe5bdfbbad46 to your computer and use it in GitHub Desktop.

Select an option

Save amekusa/38eab238b4eece375bcffe5bdfbbad46 to your computer and use it in GitHub Desktop.

WordPress TIPs

The earliest hook that conditional tags work in is "wp"

Example:

add_action('wp', function () {
	if (is_singular()) echo 'This page is singular';
});

But you can do the same thing in "pre_get_posts" hook (earlier than "wp") like this:

add_action('pre_get_posts', function ($Q) {
	if (!$Q->is_main_query()) return;
	if ($Q->is_singular) echo 'This page is singular';
});

https://codex.wordpress.org/Conditional_Tags

How to switch theme programmatically

$fn = function ($Theme) {
	echo "Current theme is $Theme";
	return 'my-theme'; // Switch theme to 'my-theme'
};
add_filter('stylesheet', $fn, 11);
add_filter('template', $fn, 11);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment