Conditional Page Template not working

You need to run this code in an action hook rather than directly in the functions.php file. Take a look at the Action Reference page in the Codex and determine at which point you want this script to run. Whatever action you choose will end up looking something like this: add_filter( ‘template_include’, ‘check_page_template’, 99 ); … Read more

Unregister Nav Menu with fallback?

You can filter ‘wp_nav_menu_args’, set an invalid theme_location and the magic function __return_false as fallback: add_filter( ‘wp_nav_menu_args’, ‘override_parent_args’ ); function override_parent_args( $args ) { if ( ‘header-menu’ === $args[‘theme_location’] ) { $args[‘fallback_cb’] = ‘__return_false’; $args[‘theme_location’] = ‘ticky_tacky’; } return $args; }

Displaying a random user with a shortcode

By default WordPress allow only order ASC and DESC. However, you can use WP_User_Query and the action pre_user_query to adjust the query (that is passed by reference) just like you want. This is efficient because you get only one user, not all. function my_user_by_rand( $ua ) { // remove the action to run only once … Read more

how to get and display logged in user’s recently read posts

For that you first need to SAVE when someone logged in reads a post. You can do this for instance within an action like get_header. add_action(‘get_header’,’save_single_posts_to_usermeta’); function save_single_posts_to_usermeta(){ if(is_single() && is_user_logged_in()){ $user_id = get_current_user_id(); $read_posts = get_user_meta($user_id,’_read_posts’,true); $most_recent_read_post = get_user_meta($user_id,’_most_recent_post’,true); if(!is_array($read_posts)){ $read_posts = array(); } if(!isset($read_posts[get_the_ID()]){ $read_posts[get_the_ID()] = 1; } else { $read_posts[get_the_ID()] = $read_posts[get_the_ID()]+1; … Read more

Most efficient way to get custom database records from 20 buttons and 20 tables?

I suppose the easiest way would be to have the action or other parameters in the button itself, i.e. <div class=”mybutton” data-action=”get_quote” data-type=”literary”>Get a literary quote</div> <div class=”mybutton” data-action=”get_quote” data-type=”political”>Get a political quote</div> data-foo=”bar” attributes can easily be accessed in jQuery with .data(“foo”), so you could change the jQuery around a bit to listen to … Read more